pratiksh404 /
adminetic
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Pratiksh\Adminetic\Http\Controllers\Admin; |
||||
| 4 | |||||
| 5 | use App\Http\Controllers\Controller; |
||||
|
0 ignored issues
–
show
|
|||||
| 6 | use Illuminate\Http\Request; |
||||
| 7 | |||||
| 8 | class EditorUploadController extends Controller |
||||
| 9 | { |
||||
| 10 | // public function upload(Request $request) |
||||
| 11 | // { |
||||
| 12 | // if ($request->hasFile('upload')) { |
||||
| 13 | // $originName = $request->file('upload')->getClientOriginalName(); |
||||
| 14 | // $fileName = pathinfo($originName, PATHINFO_FILENAME); |
||||
| 15 | // $extension = $request->file('upload')->getClientOriginalExtension(); |
||||
| 16 | // $fileName = $fileName.'_'.time().'.'.$extension; |
||||
| 17 | |||||
| 18 | // $request->file('upload')->move(public_path('storage/editorimages'), $fileName); |
||||
| 19 | |||||
| 20 | // $CKEditorFuncNum = $request->input('CKEditorFuncNum'); |
||||
| 21 | // $url = asset('storage/editorimages/'.$fileName); |
||||
| 22 | // $msg = 'Image uploaded successfully'; |
||||
| 23 | // $response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>"; |
||||
| 24 | |||||
| 25 | // @header('Content-type: text/html; charset=utf-8'); |
||||
| 26 | |||||
| 27 | // return $response; |
||||
| 28 | |||||
| 29 | // return response()->json(['uploaded' => 1, 'fileName' => $fileName, 'url' => $url]); |
||||
| 30 | // } |
||||
| 31 | // } |
||||
| 32 | public function upload(Request $request) |
||||
| 33 | { |
||||
| 34 | // Ensure a file was uploaded |
||||
| 35 | if (! $request->hasFile('upload')) { |
||||
| 36 | return response()->json(['uploaded' => 0, 'error' => ['message' => 'No file was uploaded.']], 400); |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | $file = $request->file('upload'); |
||||
| 40 | |||||
| 41 | // Validate file type to ensure it's an image |
||||
| 42 | $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif']; |
||||
| 43 | $extension = $file->getClientOriginalExtension(); |
||||
| 44 | if (! in_array(strtolower($extension), $allowedExtensions)) { |
||||
| 45 | return response()->json(['uploaded' => 0, 'error' => ['message' => 'Invalid file type.']], 400); |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | // Generate a unique file name |
||||
| 49 | $originName = $file->getClientOriginalName(); |
||||
| 50 | $fileName = pathinfo($originName, PATHINFO_FILENAME).'_'.time().'.'.$extension; |
||||
|
0 ignored issues
–
show
Are you sure
pathinfo($originName, Pr...dmin\PATHINFO_FILENAME) of type array|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 51 | |||||
| 52 | try { |
||||
| 53 | // Ensure the upload directory exists and has correct permissions |
||||
| 54 | $uploadDir = public_path('storage/editorimages'); |
||||
| 55 | if (! is_dir($uploadDir)) { |
||||
| 56 | mkdir($uploadDir, 0775, true); // Create directory if it doesn't exist |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | // Move the file to the upload directory |
||||
| 60 | $file->move($uploadDir, $fileName); |
||||
| 61 | |||||
| 62 | // Build the URL for the uploaded file |
||||
| 63 | $fileUrl = asset('storage/editorimages/'.$fileName); |
||||
| 64 | |||||
| 65 | // Check if the request contains the CKEditor function number |
||||
| 66 | if ($request->has('CKEditorFuncNum')) { |
||||
| 67 | $CKEditorFuncNum = $request->input('CKEditorFuncNum'); |
||||
| 68 | $msg = 'Image uploaded successfully'; |
||||
| 69 | |||||
| 70 | // Return the JavaScript response for 'form' upload method |
||||
| 71 | return response("<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$fileUrl', '$msg');</script>")->header('Content-type', 'text/html; charset=utf-8'); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | // Return JSON response for 'xhr' upload method |
||||
| 75 | return response()->json(['uploaded' => 1, 'fileName' => $fileName, 'url' => $fileUrl]); |
||||
| 76 | } catch (\Exception $e) { |
||||
| 77 | return response()->json(['uploaded' => 0, 'error' => ['message' => 'Upload failed: '.$e->getMessage()]], 500); |
||||
| 78 | } |
||||
| 79 | } |
||||
| 80 | } |
||||
| 81 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths