简体   繁体   中英

Lumen file upload

I have the following code for the file upload :

$picName = $request->file('cnicFrontUrl')->getClientOriginalName();
$picName = Carbon::now() . $picName;
$destinationPath = '/uploads/user_files/cnic';
$request->file('cnicFrontUrl')->move($destinationPath, $picName);

In my public folder i have uploads/user_files/cnic .

The exception i receives :

{
  "message": "Could not move the file \"D:\\xampp\\tmp\\php2D1C.tmp\" to \"uploads/user_files/cnic\\2017-05-22 09:06:15cars.png\" ()",
  "status_code": 500
}

Whats missing here ?

Try this

        $picName = $request->file('image')->getClientOriginalName();
        $picName = uniqid() . '_' . $picName;
        $path = 'uploads' . DIRECTORY_SEPARATOR . 'user_files' . DIRECTORY_SEPARATOR . 'cnic' . DIRECTORY_SEPARATOR;
        $destinationPath = public_path($path); // upload path
        File::makeDirectory($destinationPath, 0777, true, true);
        $request->file('image')->move($destinationPath, $picName);

We can not set file name like this

2017-05-22 09:06:15cars.png

So use uniqid() function for unique file name of image

"uploads/user_files/cnic\\2017-05-22 09:06:15cars.png\\"

The issue is the destination slashes. Directory separator on windows is \\

Also, : is not allowed in file name

use DIRECTORY_SEPARATOR instead of / or \\, this will automatically get independent directory separator for your platform.

example

<?php

// will output "../public/logo.png" in unix or "..\\public\\logo.png"
$path = ".." . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "logo.png"
echo $path;

Reference : php.net - Predefined Constants

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM