Welcome Guest, Not a member yet? Register   Sign In
File Upload Duplicate Filename Increment
#1

[eluser]bhensonweb[/eluser]
I have an app using the file upload class and one user has uploaded over 100 images having the same name. Since the image overwrite setting is set to false, an incrementing number is being added to the end. This is good, but it has stopped at 99 (myfile99.jpg). Any additional images which the user attempts to upload results in an error "The file you are trying to upload already exists".

Unfortunately uploading multiple duplicate files is necessary as that is how the system is built. In the mean-time, I have instructed them to modify the file name slightly, as a temporary solution. I plan to eventually consolidate all entries using this duplicate file. But for now I would prefer that the user does not encounter this error again.

Any advice is appreciated. Thanks.
#2

[eluser]Junaid Atique[/eluser]
Well my practice is that i use time function concatenated with image name. so try this instead to increment.
use some thing like this
Code:
$file_name = 'myfile'.time().'.jpg'
#3

[eluser]InsiteFX[/eluser]
The 99 is a limit set in the upload class!
Code:
// --------------------------------------------------------------------

    /**
     * Set the file name
     *
     * This function takes a filename/path as input and looks for the
     * existence of a file with the same name. If found, it will append a
     * number to the end of the filename to avoid overwriting a pre-existing file.
     *
     * @param    string
     * @param    string
     * @return    string
     */
    public function set_filename($path, $filename)
    {
        if ($this->encrypt_name == TRUE)
        {
            mt_srand();
            $filename = md5(uniqid(mt_rand())).$this->file_ext;
        }

        if ( ! file_exists($path.$filename))
        {
            return $filename;
        }

        $filename = str_replace($this->file_ext, '', $filename);

        $new_filename = '';
        for ($i = 1; $i < 100; $i++)
        {
            if ( ! file_exists($path.$filename.$i.$this->file_ext))
            {
                $new_filename = $filename.$i.$this->file_ext;
                break;
            }
        }

        if ($new_filename == '')
        {
            $this->set_error('upload_bad_filename');
            return FALSE;
        }
        else
        {
            return $new_filename;
        }
    }
See the for loop with 100, that is your problem! You would need to extend the upload library and modify that method.

InsiteFX
#4

[eluser]bhensonweb[/eluser]
Thanks for the comments. Based on the set_filename function code, I am assuming they set the 100 limit to minimize the number of loops that are happening. If there were 1000 loops each checking for the existence of a file on the server it might be inefficient...

I like the date concept. I am wondering if filename length limits might conflict with appending the date as seconds, if the filename is already long. Otherwise I guess I will try to make that extension which would append the time() instead.




Theme © iAndrew 2016 - Forum software by © MyBB