jmacdiarmid's avatar

issues uploading image files in Laravel 9

I'm doing a test image upload using this tutorial (https://readerstacks.com/how-to-upload-image-with-preview-in-laravel-9-with-example/). It's saving all of the data from the form (article title, poster's email address, and path to image), however the actual image is not created in the designated symlink folder as if the folder can't be found. I created a symlink as suggested in the tutorial and ran the php artisan storage:link command. and the result indicated that the default link was created but could not find the "uploads\images" file. However, after the error it showed:

$ php artisan storage:link

INFO  The [C:\laragon\www\foodorder\public\storage] link has been connected to [C:\laragon\www\foodorder\storage\app/public].

The system cannot find the file specified.
INFO  The [C:\laragon\www\foodorder\public\uploads] link has been connected to [C:\laragon\www\foodorder\storage\app/uploads].

$ php artisan storage:link
ERROR  The [C:\laragon\www\foodorder\public\storage] link already exists.
ERROR  The [C:\laragon\www\foodorder\public\uploads] link already exists.

One thing I noticed is phpstorm is showing a potential "null pointer exception" on the store method of $request->file('image')

Also, after adding the link to the filesystem.php in the app\config folder, the uploads folder isn't created when running the symlink command.

Thoughts? Am I missing something?

0 likes
6 replies
RayC's avatar

Do you have this in your form tag:

enctype="multipart/form-data"
1 like
jmacdiarmid's avatar

@RayC Another interesting bit of discovery I made this morning is when I changed the code statements that are supposed to do the file storage in the symlink from:

$file = $request->file('image')->store('uploads');

to

$fTmp = $request->file('image');
$file = $fTmp->store('public/storage');

an image file is written/created in "public/storage/storage. Also the potential null-pointer exception is removed from when separated into 2 statements as shown above:

$request->file('image')->store()
MohamedTammam's avatar

If you want to store into the public directory you need to specify it.

$file = $request->file('image')->store('public/uploads');

The store method will use storage/app to store the files. So you need to append the public directory to it.

PS: the symlink is pointing to storage/app/public

jmacdiarmid's avatar

I made the following change to my store method and all appears to be working fine.

        $article = new Article;
        if($request->file()) {
            $article->title = $request->title;
            $article->email = $request->email;
            $file = $request->file('image');
            $fileName = time().'_'.$file->getClientOriginalName();
            $filePath = $file->storeAs('uploads', $fileName, 'public');
            $article->image = '/storage/' . $filePath;
            $article->save();
        }
        return back()->with('success','You have successfully created the article with compressed image.');

Note: I'm using a custom form request (StoreArticleRequest) with the following:

  public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => "required",
            'email' => "required|email",
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
        ];
    }

    public function messages() {
        return [
          'title.required' => 'Title field is required.',
          'email.required' => 'Email field is required.',
          'image.required' => 'Image field is required and file must be a .jpeg,.jpg,.png, or .gif',
        ];
    }

Please or to participate in this conversation.