Laravel 上传文件无法写入目录
Laravel uploading file Unable to write in directory
我正在我的系统中上传文件,它在使用 windows 和 xampp 的地方工作,但是在使用 Ubuntu 堆栈的地方托管时,我的文件没有被上传.我得到一个错误,它不能写入 public 文件夹中的 '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,我的主机是数字海洋。谢谢
chmod 755 /var/www/html/public/system
和 chown www-data:www-data /var/www/html/public/system
正如@JLPuro 所说的那样完美。非常感谢大家。
文件夹的权限sudo chmod 777./(folder name)
for 文件 sudo chmod 777 -R ./(file name)
每当你必须创建一个目录时使用这个:
use File;
use Illuminate\Foundation\Bus\DispatchesJobs;
在控制器中使用它
if (!file_exists(public_path().'system')) {
if(File::makeDirectory(public_path().'system',0777,true)){
}
}
在你的函数中使用它
如果你使用的是Centos-7 那么使用
sudo find directory_name -type f -exec chmod 644 {} \;
sudo find directory_name -type d -exec chmod 775 {} \;
sudo chown -R apache:apache var/www/html/directory_name
chcon -R -t httpd_sys_rw_content_t directory_name
其中 directory_name 是您项目的主目录。
我正在我的系统中上传文件,它在使用 windows 和 xampp 的地方工作,但是在使用 Ubuntu 堆栈的地方托管时,我的文件没有被上传.我得到一个错误,它不能写入 public 文件夹中的 '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,我的主机是数字海洋。谢谢
chmod 755 /var/www/html/public/system
和 chown www-data:www-data /var/www/html/public/system
正如@JLPuro 所说的那样完美。非常感谢大家。
文件夹的权限sudo chmod 777./(folder name)
for 文件 sudo chmod 777 -R ./(file name)
每当你必须创建一个目录时使用这个:
use File;
use Illuminate\Foundation\Bus\DispatchesJobs;
在控制器中使用它
if (!file_exists(public_path().'system')) {
if(File::makeDirectory(public_path().'system',0777,true)){
}
}
在你的函数中使用它
如果你使用的是Centos-7 那么使用
sudo find directory_name -type f -exec chmod 644 {} \;
sudo find directory_name -type d -exec chmod 775 {} \;
sudo chown -R apache:apache var/www/html/directory_name
chcon -R -t httpd_sys_rw_content_t directory_name
其中 directory_name 是您项目的主目录。