1
votes

I am trying to use Laravel 5's Storage::disk()->putFile() method to upload images to my server. When I use php artisan serve locally, the image uploads successfully to the local disk. When I upload to the server side, it says the file does not exist. I have tried setting the max post and upload size to 20MB, using php artisan storage:link, and using s3 and public instead of the local disk but nothing has seemed to work.

Here is my method:

    public function api_create(Request $request)
{
    try {
        $requestData = $request->all();
        $uniqueFileID = Storage::disk('local')->putFile('product_media', new File($requestData["imageCode"]));
        $requestData["imageCode"] = $uniqueFileID;
        Image::create($requestData);
        return "true";
    } catch (Exception $exception) {
        return "false";
    }

}

Here is my error:

local.ERROR: Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "/Users/drewgallagher/Downloads/download.png" does not exist in /home/52064-43928.cloudwaysapps.com/wrdddnmtvz/public_html/vendor/symfony/http-foundation/File/File.php:37
2
I usually get File does not exist when my storage folder permissions are wrong. The instructions from user "Bgies" on this page is what I use to fix my permission issues: laracasts.com/discuss/channels/general-discussion/…. Files to 644, and folders to 755Hollings
@Drew Gallagher I think folder permission issueAddWeb Solution Pvt Ltd

2 Answers

0
votes

My issue ended up being that when I was using PostMan for testing and when I was running my frontend request, I was only sending the file path, and not the file with the right header. That is why the backend wasn't getting the right file param.

0
votes

You should try this may be work for you:

public function api_create(Request $request)
{
  try {
      $requestData = $request->all();
      $uploadedFile = public_path() . '/uploads/' . $requestData["imageCode"];

      if(file_exists($uploadedFile)) {
        $uniqueFileID = Storage::disk('local')->putFile('product_media', file_get_contents($uploadedFile));
        $requestData["imageCode"] = $uniqueFileID;
        Image::create($requestData);
        return "true";
      } else{
        return "false";
      }
  } catch (Exception $exception) {
      return "false";
  }
}