使用 Laravel 上传文件时出现未知错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26017385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:09:28  来源:igfitidea点击:

Unknown error when uploading file using laravel

phpfile-uploadlaravel

提问by vonc

When I try to upload an image file to the server, randomly it gives error and sometimes it works fine with no problems with the same file.

当我尝试将图像文件上传到服务器时,它会随机出现错误,有时它可以正常工作,同一个文件没有问题。

Symfony \ Component \ HttpFoundation \ File \ Exception \ FileException 
The file "xxxxxx.jpg" was not uploaded due to an unknown error.

This is my code:

这是我的代码:

if (Input::hasFile('thumbnail_image')){
  // Upload Photo
  $thumbnailImage = Input::file('thumbnail_image');
  $filename = 'thumbnail.'.Input::file('thumbnail_image')->getClientOriginalExtension();
  $destinationPath = 'training_product/article_id_'. $id .'/thumbnail_image';
  $databasePath = '/adidas/public/training_product/article_id_'. $id.'/thumbnail_image';
  $uploadFile = Input::file('thumbnail_image')->move($destinationPath, $filename);
  $product->thumbnail_images_path = $databasePath.'/'.$filename;
  $product->save();
}

回答by Victor

What are your file permission on your destination folder? Try changing it to 777(assuming that you are using a linux server: sudo chmod -R 777 adidas/public/training_product.

您对目标文件夹的文件权限是多少?尝试将其更改为777(假设您使用的是 linux 服务器:sudo chmod -R 777 adidas/public/training_product.

I've modified your code slightly and it works for me.

我稍微修改了你的代码,它对我有用。

```

``

 if (Input::hasFile('thumbnail_image')){
      // Upload Photo
      $product = new Product;
      $thumbnailImage = Input::file('thumbnail_image');
      $ext = $thumbnailImage->guessExtension();
      $filename = 'thumbnail_'.  time()  . $ext;
      $destinationPath = 'training_product/article_id_'. $id .'/thumbnail_image';
      $databasePath = '/adidas/public/training_product/article_id_'. $id.'/thumbnail_image/';
      $thumbnailImage->move($destinationPath, $filename);
      $product->thumbnail_images_path = $databasePath . $filename;
      $product->save();
    }

```

``

I've Changed getClientOriginalExtension()to guessExtension(), instantiated a new $product, as I didn't see you doing. Also made sure the folder permissions would allow me to upload files.

我已更改getClientOriginalExtension()guessExtension(),实例化了一个新的$product,因为我没有看到您在做什么。还确保文件夹权限允许我上传文件。

Hope it helps.

希望能帮助到你。