user6519911
user6519911

Reputation:

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 ?

Upvotes: 0

Views: 18067

Answers (3)

jbe
jbe

Reputation: 1782

"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

Upvotes: 1

Roy Ryando
Roy Ryando

Reputation: 1288

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

Upvotes: 1

Dharmesh Rakholia
Dharmesh Rakholia

Reputation: 1238

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

Upvotes: 7

Related Questions