在 Laravel 中出现 "move_uploaded_file(): failed to open stream: Permission denied" 错误

Getting "move_uploaded_file(): failed to open stream: Permission denied" error in Laravel

当我提交表单时,我可以看到文件名、大小等,但我无法上传它,因为它一直说权限被拒绝。我将 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 在浏览器和终端中截取了错误消息的屏幕截图,但我还不能 post 图片 :/

我想提交的原始表格不是带有操作、方法的表格...我用 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 函数 dodajKategorije2() {
        如果(isset($_POST['username'])){ $username = $_POST['username'];}

        回显计数($_FILES)."<br />"。 print_r($_FILES);

        如果(isset($_FILES['profileImg'])){
            $size= intval($_FILES['profileImg']['size'],10);
           如果($大小 > 10485760){
                return "file size: ".$_FILES['profileImg']['size'];
            } 别的 {
                move_uploaded_file($_FILES['profileImg']['tmp_name'][0] ,"/slika.jpg");
                return "ok";
            }
         }
         否则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>

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

unable to create '/upload' directory

我将目的地设置为 './upload' 带点 '.'

它仍然说

file was not uploaded due to an unknown error

但是当我(不小心)检查时 public 文件夹文件在那里...

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

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

//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 );