在Laravel中获取“move_uploaded_file():无法打开流:权限被拒绝”错误

时间:2015-03-18 18:50:53

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

当我的表单提交时,我可以看到文件名,大小等但我无法上传它,因为它一直说权限被拒绝。我将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

<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>

2 个答案:

答案 0 :(得分:1)

最好使用移动功能将文件上传到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 );

答案 1 :(得分:0)

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

unable to create '/upload' directory

我将目的地设定为 './upload' 带点&#39;。&#39;

它仍然说

file was not uploaded due to an unknown error

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

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