Jump to content

Rename if file exists on upload


NathanLedet

Recommended Posts

You could use a while loop. Split the extension from the file name then append an integer and the extension

 

I didn't test this but i think you'll get the general idea:

 

<?php
// This should keep checking until the file doesn't exists
while( file_exists($YourFile) {
  $Count += 1;
  $YourImageFile = $FileName . $Count . $Extension;
}
?>

Link to comment
Share on other sites

This may be long, but it should work...

 

$file = 'location/of/your/file.jpg';

$pos = strrpos($file,'.');
$ext = substr($file,$pos); 
$dir = strrpos($file,'/');
$dr  = substr($file,0,($dir+1)); 

$arr = explode('/',$file);
$fName = trim($arr[(count($arr) - 1)],$ext);


$exist = FALSE;
$i = 0;
while(!$exist){
     if(file_exists($file.$i)||file_exists($file)){
          continue;
     }else{
          $exist = TRUE;
          $file = $dr.$fName.$i.$ext;
     }
     $i++;
}

echo $file;
// Upload Script Here

Link to comment
Share on other sites

  • 2 months later...

i mean, i can't get either of them to work... what am i doing wrong...

 

i made this:

 

where should i paste in the code?

 

<?php

 

$path = "C:\web\www-root\www.eurogiro.com\html\joomla\upload";//Sti til mappen filen skal uploades i!

$filename = "coc.xls";//Filens navn

 

 

 

if(count($_POST)>0)//Der bliver postet til scriptet

{

    $file = $_FILES['file'];

   

   

   

    if(move_uploaded_file($file['tmp_name'], $path."/".$filename))

    {

        echo 'The file has been succesfully uploaded';

    }

    else

    {

        echo 'The file wasnt uploaded. Please try again';

    }

}

else

{

$form = '<form enctype="multipart/form-data" action="" method="post">

<input type="file" name="file" value=""><br>

<input type="submit" name="submit" value="Upload">

</form>';

echo $form;

}

echo "</body>

</html>";

?>

 

Link to comment
Share on other sites

Check if the file exists before uploading, if exists rename with the new generated name. I guess this should work

$file_path = $path."/".$filename
if (file_exists($filepath)) {
   /*generate the $new_file_path, with the code given you before*/
   rename($filepath,$new_file_path);
}

if(move_uploaded_file($file['tmp_name'], $path."/".$filename))
    {
        echo 'The file has been succesfully uploaded';
    }
    else
    {
        echo 'The file wasnt uploaded. Please try again';
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.