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>
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 );
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'
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 so for me it works with that dot. 所以对我来说它可以与该点一起使用。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.