grgbikash05's avatar

what's wrong on this code

I am uploading image and saving it in the public images directory

if($request->hasFile('attachment')) {
        $image = $request->file('attachment');
        $name = time(). '.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('images');
        $image->move($destinationPath, $name);
        return 'Saved successfully';
    }

It shows error

Could not move the file "C:\xampp\tmp\php7D82.tmp" to        
    "C:\Users\Bikash\Desktop\blogtest\public\images31575953.jpg" 
   (move_uploaded_file(): Unable to move 'C:\xampp\tmp\php7D82.tmp' to 
   'C:\Users\Bikash\Desktop\blogtest\public\images31575953.jpg')

But when i remove "." this between that time() and $image->getClientOriginalExtension(), it works 123243324jpg but obviuosly we will be missing 123243324.jpg 'that dot between them'

0 likes
9 replies
Tray2's avatar

There seems to be a \ missing in the path. That might be part of the issue

I think this is the path you are aiming for 'C:\Users\Bikash\Desktop\blogtest\public\images\31575953.jpg'

$destinationPath = public_path('images') . DIRECTORY_SEPARATOR;

You are trying to move the image into public instead of public\images it seems.

grgbikash05's avatar

Could not move the file "C:\xampp\tmp\php89D0.tmp" to "C:\Users\Bikash\Desktop\blogtest\public\images\1531577791.jpg" (move_uploaded_file(): Unable to move 'C:\xampp\tmp\php89D0.tmp' to 'C:\Users\Bikash\Desktop\blogtest\public\images\1531577791.jpg')

error is like this

Tray2's avatar

Linux, windows or Mac?

Could be that you don't have the correct permission on the public/images directory

grgbikash05's avatar

windows.

It works when I remove dot in between them $name = time().$image->getClientOriginalExtension();

but it will be saved as 123456jpg

but we want 123456.jpg so I write $name = time(). '.'.$image->getClientOriginalExtension();

And this problem occurs

jlrdw's avatar

Please tell me you're joking


$name = time() . '.' . $image->getClientOriginalExtension();

But you are better off having some text prior to numbers in a file name. Just my 2 cents on that part.

grgbikash05's avatar

@jlrdw It should work, shouldn't it? How do you do this image moving to public/images directory.. Let me have your code..

@Cronix Need a solution from u man

jlrdw's avatar

The reference above in the documentation is the solution.

1 like

Please or to participate in this conversation.