0

When my form is submitted I can see file name,size etc but I can't upload it because it keeps saying permission denied. I changed owner of myapp/storage to www-data:www-data and did

php artisan cache:clear 

chmod -R 777 myapp/storage 

as suggested here: 'Failed to open stream: Permission denied' error - Laravel, but it didn't help.

My form is created with <form> <input> tags, not with {{ Form }} elements, so I'm uploading it with move_uploaded_file() and not Input::file()->move().

I took screenshots of error message in browser and terminal with ls -l but I can't post images yet :/

Original form that I want to submit isn't form with action, method...I submit it with ajax, but since that wasn't working I made a small form with just 2 inputs to see what's going on...and it says permission denied

EDIT 2: error message:



    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>

Community
  • 1
  • 1
tachkarella
  • 1
  • 1
  • 4
  • (If you want to add HTML/XML tags to a Stack Overflow question, just wrap it in backticks (`inline code`) or indent four spaces (for a block)). – halfer Mar 18 '15 at 19:22
  • "My form is created with
    tags, not with {{ Form }} elements, so I'm uploading it with move_uploaded_file() and not Input::file()->move()." Doesn't matter. Use `Input::file()->move()`. It works fine with raw HTML forms.
    – ceejayoz Mar 18 '15 at 19:30
  • Do you have the error log, I would like to analyze it – Acácio Veit Schneider Mar 18 '15 at 19:35
  • Can you provide the error text since you can't post the images? – Kelly Kiernan Mar 18 '15 at 20:31
  • I've updated question with error text...it should upload that file in public folder,right? which owner should be set for other folders? I changed only storage folder owner and permissions. – tachkarella Mar 18 '15 at 20:54
  • that **echo print_r($_FILES) ** gave me this: Array ( [profileImg] => Array ( [name] => Array ( [0] => 6zx1y8.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => /tmp/php6vOcbE ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 90735 ) ) ) 1 – tachkarella Mar 18 '15 at 21:12
  • @ceejayoz That's nice :) It didn't work before because of unknown Input class...I fixed that, now with Input::file()->move error text is: - Unable to create the "/upload" directory - and if I leave $destination as '/' it says - MethodNotAllowedHttpException in RouteCollection.php line 207: in RouteCollection.php line 207 at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 194 at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 142 and I removed [] so only one file is uploaded. – tachkarella Mar 18 '15 at 21:53
  • @AcácioVeitSchneider do you think reason this is happening could be bad installation? I installed laravel with composer on ubuntu 14, maybe I overlooked something? Before this I was getting error TokenMissmatch exception on POST submit and GET was working fine...I tried to add token in hidden input and then submit it with form but it didn't work, so I just commented line that verifies token in Kernel.php Could it be this is happening because of that? – tachkarella Mar 19 '15 at 08:41

2 Answers2

1

It is better to use move function for upload the file to Public folder. Then it will be easier for you to access the file from anywhere in your application.

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

Above move function will upload the file with its real name(original file name). if you want to customize it you can use a uniqid() function for generate a unique file name,

//Move Uploaded File
$up_name = uniqid();
$destinationPath = 'uploads';
$file->move($destinationPath, $up_name );
coder618
  • 848
  • 1
  • 9
  • 12
0

I finally solved the problem. I had www-data:www-data set as owner of folder storage but it kept saying

unable to create '/upload' directory

I set destination to './upload' with the dot '.'

it still says

file was not uploaded due to an unknown error

but when I checked (accidentally) public folder file was there...

I don't know why it says it's not uploaded due to error but it does upload file every time so for me it works with that dot.

tachkarella
  • 1
  • 1
  • 4