4

How do I check if file name exists, rename the file?

for example, I upload a image 1086_002.jpg if the file exists, rename the file as 1086_0021.jpg and save, if 1086_0021.jpg is exist, rename 1086_00211.jpg and save , if 1086_00211.jpg is exist, rename 1086_002111.jpg and save...

Here is my code, it only can do if 1086_002.jpg exist, rename the file as 1086_0021.jpg, maybe should do a foreach, but how?

//$fullpath = 'images/1086_002.jpg';

if(file_exists($fullpath)) {
    $newpieces = explode(".", $fullpath);
    $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
    $newpath = $frontpath.'1.'.end($newpieces);
}

file_put_contents($newpath, file_get_contents($_POST['upload']));
Rich
  • 5,603
  • 9
  • 39
  • 61
yuli chika
  • 9,053
  • 20
  • 75
  • 122
  • 1
    php file uploads do NOT go through $_POST. They go through $_FILES and have very different handling semantics than any other form fields. – Marc B Apr 04 '12 at 16:32

5 Answers5

9

Try something like:

$fullpath = 'images/1086_002.jpg';
$additional = '1';

while (file_exists($fullpath)) {
    $info = pathinfo($fullpath);
    $fullpath = $info['dirname'] . '/'
              . $info['filename'] . $additional
              . '.' . $info['extension'];
}
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • +1, I was intending to write something similar, but this is about as good as it can get. – Jon Apr 04 '12 at 16:36
  • 1
    @Baba how do you arrive at that conclusion? `$fullpath` is updated within the while loop (note the added `$additional` each time). The only way for an infinite loop here would be an infinite number of files. – cmbuckley Apr 04 '12 at 16:48
  • 1
    You are suppose to always increase $additional ??? eg. `$additional++` ... since `$additional` is just `1` and it does not change .. the script would `run till it times out` Thanks – Baba Apr 04 '12 at 16:51
  • 2
    `$additional` is appended *each time*, not incremented. Per the OP's request. Did you even try [running the code](http://codepad.org/KNuKds5d)? – cmbuckley Apr 05 '12 at 12:43
2

Why not just append a timestamp onto the filename? Then you won't have to worry about arbitrarily long filenames for files which have been uploaded many times.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
1

I hope this helps

$fullPath = "images/1086_002.jpg" ;
$fileInfo = pathinfo($fullPath);
list($prifix, $surfix) = explode("_",$fileInfo['filename']);
$x = intval($surfix);
$newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT)  . $fileInfo['extension'];
while(file_exists($newFile)) {
    $x++;
    $newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT)  . $fileInfo['extension'];
}

file_put_contents($newFile, file_get_contents($_POST['upload']));

I hope this Helps

Thanks

:)

Baba
  • 94,024
  • 28
  • 166
  • 217
1

I feel this would be better. It will help keep track of how many times a file with the same name was uploaded. It works in the same way like Windows OS renames files if it finds one with the same name.

How it works: If the media directory has a file named 002.jpg and you try to upload a file with the same name, it will be saved as 002(1).jpg Another attempt to upload the same file will save the new file as 002(2).jpg

Hope it helps.

$uploaded_filename_with_ext = $_FILES['uploaded_image']['name'];
$fullpath = 'media/' . $uploaded_filename_with_ext;
$file_info = pathinfo($fullpath);
$uploaded_filename = $file_info['filename'];

$count = 1;                 
while (file_exists($fullpath)) {
  $info = pathinfo($fullpath);
  $fullpath = $info['dirname'] . '/' . $uploaded_filename
  . '(' . $count++ . ')'
  . '.' . $info['extension'];
}
$image->save($fullpath);
NitinM
  • 11
  • 1
0

You can change your if statement to a while loop:

$newpath = $fullpath;
while(file_exists($newpath)) {
    $newpieces = explode(".", $fullpath);
    $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
    $newpath = $frontpath.'1.'.end($newpieces);
}

file_put_contents($newpath, file_get_contents($_POST['upload']));
jasonlfunk
  • 5,159
  • 4
  • 29
  • 39