Laravel上传文件无法在目录

时间:2016-01-17 08:45:35

标签: php laravel file-upload laravel-5 digital-ocean

我正在我的系统中上传文件,它在本地工作,我使用的是Windows和xampp,但在托管使用Ubuntu堆栈时,我的文件没有被上传。我收到一个错误,它不能写在公共文件夹中的'system'目录中。这是我的代码

 $destinationPath = public_path().'/system'; // upload path
  $extension = Request::file('add_students')->getClientOriginalExtension(); // getting excel extension
  // dd($extension);
  $fileName = rand(11111,99999).'.'.$extension; // renameing excel file
  $file=Request::file('add_students')->move($destinationPath, $fileName); // uploading file to given path
  $path=$file->getRealPath();

在寻找解决方案之后我发现我应该使我的目录可写,所以我在putty中运行以下命令

chmod 770 /var/www/html/public/system

但即使在此之后我仍然收到错误Unable to write in the "/var/www/html/public/system" directory

我正在使用laravel 5而我的主机是数字海洋。感谢

3 个答案:

答案 0 :(得分:23)

@JLPuro所述的

chmod 755 /var/www/html/public/systemchown www-data:www-data /var/www/html/public/system效果很好。非常感谢你们。

答案 1 :(得分:1)

文件夹 sudo chmod 777./(folder name)

的权限

用于文件 sudo chmod 777 -R ./(file name)

答案 2 :(得分:0)

每当必须创建目录时,请使用以下命令:

use File;
use Illuminate\Foundation\Bus\DispatchesJobs;

在控制器中使用此

if (!file_exists(public_path().'system')) {            
                if(File::makeDirectory(public_path().'system',0777,true)){                
                  }
                }

在您的函数中使用此