Jump to content

Rename file if picture already exists


delxy

Recommended Posts

Hi there,

Need some help with this code :

I'm using this one for upload

function Upload_GenFilename($filename, $tag, $filecounter = NULL)

  {

    $filedate = bqSearch;

 

    mt_srand();

    $randomnumber = mt_rand(1, 20);

 

 

    if(stristr($filename,$tag))

    {

      $filename = substr($filename,0,strlen($filename)-strlen($tag)-1);

    }

 

    //Neuer Filename

    if($filecounter == NULL)

    {

      $newfilename = $filename.'_'.$filedate.'.'.$tag;

    }

 

    else

    {

      $newfilename = $filename.'_'.$randomnumber.'_'.$filedate;

    }

    return $newfilename;

 

If a user decided to delete a picture ,for example _7_picture.jpg and upload a new one, the picture should be renamed with the same title as the deleted one or a free one in this range 1 - 20.

So i need to replace this one :

 

//if picture already exists

        if(file_exists(JPath::clean(JPATH_ROOT.DS.$config->jg_pathimages.$catpath.$newfilename)))

        {

          $debugoutput .= JText::_('JGS_ALERT_SAME_PICTURE_ALREADY_EXIST');

          continue;

        }

with :

I need help here :)

Many thanks in advance

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/190447-rename-file-if-picture-already-exists/
Share on other sites

This is how I do it, with a code structure different to yours, but you should be able to adapt

 

 if(file_exists($uploadpath . $_FILES['ad_image']['name']))
                                        $ad_image = substr($_FILES['ad_image']['name'], 0, -strlen($extension)) . uniqid('') . '.' . $extension;
                                else
                                        $ad_image = $_FILES['ad_image']['name'];

Ok the full code around that segment is as follows

 

$allowed_image_types=array('jpg'=>'image/jpeg','jpeg'=>'image/jpeg','jpe'=>'image/jpeg','gif'=>'image/gif','png'=>'image/png');
                $extension = strtolower(substr(strrchr($_FILES['ad_image']['name'], '.'), 1));
                if(in_array($extension, array_keys($allowed_image_types)) && !empty($_FILES['ad_image']['type']) && in_array($_FILES['ad_image']['type'], $allowed_image_types))
                {
                        if(file_exists($uploadpath . $_FILES['ad_image']['name']))
                                $ad_image = substr($_FILES['ad_image']['name'], 0, -strlen($extension)) . uniqid('') . '.' . $extension;
                        else
                                $ad_image = $_FILES['ad_image']['name'];
                        if(!move_uploaded_file($_FILES['ad_image']['tmp_name'], $uploadpath . $ad_image))
                                $msg .= 'Failed to move ad images. Check permissions<br />';
                }
                else
                {
                        $msg .= 'You may only use JPEG, GIF, or PNG image types.<br />';
                }

 

So firstly we get the extension of the file to see if we are allowed to upload it, and we check the mime type.

 

Then assuming all is ok, we check if the filename exists in the upload directory.

If it does we add a random string between the original filename and the extension, and use the new name to name the file we are uploading, and of course use the same name to store in the database later

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.