dokunbam's avatar

How to upload image with original name and time

I want to upload my images with the original name with time of upload

This is my upload code

 if($request->hasFile('image'))
        {
            $image = $request->file('image');
            $fileName = $request->image->getClientOriginalName();   
            $location = public_path('images/' . $fileName);
            Image::make($image)->resize(500, 400)->save($location);

           $article->image = $fileName;

            //$article->image = $request->filename;
        }

I want to have something like

most-popular-image-2-7-2018-24-23.jpg

The original name is first then followed by the date and time

0 likes
4 replies
lostdreamer_nl's avatar
Level 53

you could get away with:

 if($request->hasFile('image'))
        {
            $image = $request->file('image');
            $ext = '.'.$request->image->getClientOriginalExtension();
            $fileName = str_replace($ext, date('d-m-Y-H-i') . $ext, $request->image->getClientOriginalName());
            $location = public_path('images/' . $fileName);
            Image::make($image)->resize(500, 400)->save($location);

           $article->image = $fileName;

            //$article->image = $request->filename;
        }

This way you'll replace the current extension with the date + current extension. The only thing it would have problems with is files named like 'filename.jpg.jpg'

1 like
dokunbam's avatar

@lostdreamer_nl Yes I got 'The image failed to upload.' when I tried uploading 'filename.jpg.jpg

I guess there is no fix for that?

lostdreamer_nl's avatar

wtf? Trying to add a comment gives me a security message saying 'something' I did blocked me for security.... it could be some phrase I posted.....

Let's try in smaller blocks to see which one gives the error:

There's always a fix

Please or to participate in this conversation.