Welcome Guest, Not a member yet? Register   Sign In
Replace image if it already exists while uploading
#1

Hi,
I am having a problem with uploading images. Upload works but it renames the image and creates a new file with a new name if the image is already in the folder. If "image_name.jpg" already exists in the folder, then a copy is created with the name "image_name_1.jpg".

How can I make CI overwrite the existing image?

This is the code that does the upload.
PHP Code:
$img->store($filepath$filename); 
Reply
#2

you must have heard the adage prevention is better than cure ? what I do with an upload via a form is use of "file_exists" . I use file name from that obtained via navigation to image file. if image already exists I feed back to form ; image already exists. So it stops dead if image already exists before processing is done and for CI4 example
Code:
$file->move('blogImages',$name);
// is evoked
CMS CI4     I use Arch Linux by the way 

Reply
#3

(01-23-2023, 01:23 PM)captain-sensible Wrote: you must have heard the adage prevention is better than cure ? what I do with an upload  via a form is use of "file_exists" . I use file name from that obtained  via navigation to image file. if image already exists I feed back to form ; image already exists. So it stops dead if image already exists before processing is done and for CI4  example
Code:
$file->move('blogImages',$name);
// is evoked
Thanks for the reply Captain-sensible but that solution will not work for me, because I actually need the existing image to be overwritten. They share the same name but the new image is different in content. If there is no way for CI to do it, then I guess I will do part of what you suggested, that is to check if it exists and delete it before uploading the new one. I was thinking there may be a way to tell CI to just overwrite it, which I did not see in the online manual nor anywhere else but I thought I'd ask.
Reply
#4

I had something similar and finally decided all I really needed was to know the name of the new file so it could be stored in the database. At some point I'll create a script to remove images not listed in the db.
Here's my code:
Code:
            $imgLargeFilename = null;
            if ($this->request->getFile('newLargeImage')) {
                // define validation rule
                $largeImageValidationRule = [
                    'newLargeImage' => [
                        'label' => "Large Image",
                        'rules' => 'uploaded[newLargeImage]'
                            . '|is_image[newLargeImage]'
                            . '|mime_in[newLargeImage,image/jpg,image/jpeg,image/gif,image/png]',
                    ],
                ];
                if ($this->validate($largeImageValidationRule)) {
                    // move the uploaded image to public directory
                    $imgLarge = $this->request->getFile('newLargeImage');
                    $imgLargeFilename = $imgLarge->getName();
                    // by default move() will not overwrite an existing file
                    $result = $imgLarge->move($_ENV['paths.eccoImageDirFullPath']);
                    if (!$result) {
                        $err = $imgLarge->getError();
                        $errStr = $imgLarge->getErrorString();
                        log_message('debug'. "Image move error: $err [$errStr]");
                    } else {
                        // the file might have been renamed to avoid overwriting an existing file
                        $imgLargeFilename = $imgLarge->getName();
                    }
                }
            } else {
                log_message('debug', "No newLargeImage");
            }

At this point $imgLargeFilename is either NULL or a new filename to be saved in the db. I suppose you could rename() it on the filesystem to overwrite the original file. I haven't tried that.
Reply
#5

In case anyone is interested, I think I found a solution. If I use $img->move() method instead of $img->store() then I can pass "true" to the move() method as the third parameter which tells it to overwrite if it exists.

Instead of
PHP Code:
$path $img->store($filepath$filename); 

I now use this
PHP Code:
$path $img->move($filepath$filenametrue); 

store() does not accept that 3rd parameter although it seems it actually uses the move() under the hood.
Reply
#6

don't know if this of any use, but in my cms i need to get rid of images, when a blog is deleted for that , i need to know image name, once have that i use.
Code:
unlink(ROOTPATH.'public/blogImages/'.$image);

So another approach is.. if image already exists with same name, as the one you need to upload , simply get rid of it and , upload as normal ?
CMS CI4     I use Arch Linux by the way 

Reply
#7

I replaced
Quote:$img->store($filepath, $filename)
with
Quote:$img->move($filepath, $filename, true)
which solved the issue. The third parameter move() method accepts tells it to overwrite the file if it exists.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB