【问题标题】:Getting "move_uploaded_file(): failed to open stream: Permission denied" error in Laravel在 Laravel 中获取“move_uploaded_file(): failed to open stream: Permission denied”错误
【发布时间】:2015-05-21 17:27:29
【问题描述】:

提交表单后,我可以看到文件名、大小等,但我无法上传,因为它一直说权限被拒绝。我将 myapp/storage 的所有者更改为 www-data:www-data 并做到了

php artisan cache:clear 

chmod -R 777 myapp/storage 

这里建议:'Failed to open stream: Permission denied' error - Laravel,但没有帮助。

我的表单是用<form> <input> 标签创建的,而不是{{ Form }} 元素,所以我用move_uploaded_file() 而不是Input::file()->move() 上传它。

我在浏览器和终端中使用 ls -l 截取了错误消息,但我还不能发布图片:/

我要提交的原始表单不是带有操作、方法的表单...我使用 ajax 提交它,但由于这不起作用,我制作了一个只有 2 个输入的小表单来查看发生了什么...... .它说权限被拒绝

编辑 2: 错误信息:

Whoops, looks like something went wrong. 1/1 ErrorException in AdminFunkcije.php line 121: move_uploaded_file(/slika.jpg): failed to open stream: Permission denied in AdminFunkcije.php line 121 at HandleExceptions->handleError('2', 'move_uploaded_file(/slika.jpg): failed to open stream: Permission denied', '/home/tamara/hexdoo/app/Http/Controllers/AdminFunkcije.php', '121', array('username' => 'lalal', 'size' => '1')) at move_uploaded_file('/tmp/phpCLtMt3', '/slika.jpg') in AdminFunkcije.php line 121 at AdminFunkcije->dodajKategorije2() at call_user_func_array(array(object(AdminFunkcije), 'dodajKategorije2'), array()) in Controller.php line 246 at Controller->callAction('dodajKategorije2', array()) in ControllerDispatcher.php line 162 at ControllerDispatcher->call(object(AdminFunkcije), object(Route), 'dodajKategorije2') in ControllerDispatcher.php line 107 at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101 at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108 at ControllerDispatcher->callWithinStack(object(AdminFunkcije), object(Route), object(Request), 'dodajKategorije2') in ControllerDispatcher.php line 67 at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\AdminFunkcije', 'dodajKategorije2') in Route.php line 198 at Route->runWithCustomDispatcher(object(Request)) in Route.php line 131 at Route->run(object(Request)) in Router.php line 691 at Router->Illuminate\Routing\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101 at Pipeline->then(object(Closure)) in Router.php line 693 at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 660 at Router->dispatchToRoute(object(Request)) in Router.php line 618 at Router->dispatch(object(Request)) in Kernel.php line 210 at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55 at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61 at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40 at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101 at Pipeline->then(object(Closure)) in Kernel.php line 111 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84 at Kernel->handle(object(Request)) in index.php line 53 at require_once('/home/tamara/hexdoo/public/index.php') in server.php line 21

PHP



    public function dodajKategorije2() {
        if(isset($_POST['username'])) { $username = $_POST['username'];}

        echo count($_FILES)."
". print_r($_FILES); if(isset($_FILES['profileImg'])) { $size= intval($_FILES['profileImg']['size'],10); if($size > 10485760) { return "file size: ".$_FILES['profileImg']['size']; } else { move_uploaded_file($_FILES['profileImg']['tmp_name'][0] ,"/slika.jpg"); return "ok"; } } else return "no file "; }

表格

<form id="data" method="POST" action="admin/dodaj_kategorije" enctype="multipart/form-data"> User Name: <input type="text" name="username" value=""><br /> Profile Image: <input name="profileImg[]" type="file" /><br /> <input type="submit" value="Submit"> </form>

【问题讨论】:

  • (如果您想在 Stack Overflow 问题中添加 HTML/XML 标签,只需将其包含在反引号 (inline code) 中或缩进四个空格(用于块)。
  • "我的表单是用
    标签创建的,而不是 {{ Form }} 元素,所以我用 move_uploaded_file() 而不是 Input::file() 上传它- >移动()。”没关系。使用Input::file()-&gt;move()。它适用于原始 HTML 表单。
  • 你有错误日志吗,我想分析一下
  • 由于无法发布图片,能否提供错误文本?
  • 我用错误文本更新了问题...它应该将该文件上传到公用文件夹中,对吗?应该为其他文件夹设置哪个所有者?我只更改了存储文件夹的所有者和权限。

标签: php file-upload laravel-5 permission-denied


【解决方案1】:

最好使用移动功能将文件上传到公共文件夹。这样您就可以更轻松地从应用程序中的任何位置访问该文件。

//For access the file
$file = $request->file('image');

//Display File Name

echo 'File Name: '.$file->getClientOriginalName();
  echo '<br>';

  //Display File Extension
  echo 'File Extension: '.$file->getClientOriginalExtension();
  echo '<br>';

  //Display File Real Path
  echo 'File Real Path: '.$file->getRealPath();
  echo '<br>';

  //Display File Size
  echo 'File Size: '.$file->getSize();
  echo '<br>';

  //Display File Mime Type
  echo 'File Mime Type: '.$file->getMimeType();

  //Move Uploaded File
  $destinationPath = 'uploads';
  $file->move($destinationPath,$file->getClientOriginalName());

上面的移动功能会上传文件的真实名称(原始文件名)。如果你想自定义它,你可以使用 uniqid() 函数来生成一个唯一的文件名,

//Move Uploaded File
$up_name = uniqid();
$destinationPath = 'uploads';
$file->move($destinationPath, $up_name );

【讨论】:

    【解决方案2】:

    我终于解决了这个问题。 我将 www-data:www-data 设置为文件夹存储的所有者,但它一直在说

    unable to create '/upload' directory

    我将目的地设置为 './upload' 带有点“。”

    它仍然说

    file was not uploaded due to an unknown error

    但是当我(不小心)检查公用文件夹文件时...

    我不知道为什么它说它由于错误而没有上传,但它每次都会上传文件所以对我来说它适用于那个点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 2021-09-04
      • 2015-11-05
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      相关资源
      最近更新 更多