Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Rename File name if Exists Append Number to End

I'm trying to rename the file name of an image when it's uploaded if it exists, say if my file name is test.jpg and it already exists I want to rename it as test1.jpg and then test2.jpg and so on. With the code I've written its changing my file name like so test1.jpg and then test12.jpg any advice on fixing this would be great thank!

PHP

$name = $_FILES['picture']['name'];
$actual_name = pathinfo($name,PATHINFO_FILENAME);
$extension = pathinfo($name, PATHINFO_EXTENSION);

$i = 1;
while(file_exists('tmp/'.$actual_name.".".$extension))
{           
    $actual_name = (string)$actual_name.$i;
    $name = $actual_name.".".$extension;
    $i++;
}
like image 638
user2201765 Avatar asked Apr 21 '13 21:04

user2201765


3 Answers

Here's a minor modification that I think should do what you want:

$actual_name = pathinfo($name,PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo($name, PATHINFO_EXTENSION);

$i = 1;
while(file_exists('tmp/'.$actual_name.".".$extension))
{           
    $actual_name = (string)$original_name.$i;
    $name = $actual_name.".".$extension;
    $i++;
}
like image 78
Jason Avatar answered Oct 14 '22 07:10

Jason


Inspired from @Jason answer, i created a function which i deemed shorter and more readable filename format.

function newName($path, $filename) {
    $res = "$path/$filename";
    if (!file_exists($res)) return $res;
    $fnameNoExt = pathinfo($filename,PATHINFO_FILENAME);
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    $i = 1;
    while(file_exists("$path/$fnameNoExt ($i).$ext")) $i++;
    return "$path/$fnameNoExt ($i).$ext";
}

Example:

$name = "foo.bar";
$path = 'C:/Users/hp/Desktop/ikreports';
for ($i=1; $i<=10; $i++) {
  $newName = newName($path, $name);
  file_put_contents($newName, 'asdf');
}

New version (2022):

function newName2($fullpath) {
  $path = dirname($fullpath);
  if (!file_exists($fullpath)) return $fullpath;
  $fnameNoExt = pathinfo($fullpath,PATHINFO_FILENAME);
  $ext = pathinfo($fullpath, PATHINFO_EXTENSION);

  $i = 1;
  while(file_exists("$path/$fnameNoExt ($i).$ext")) $i++;
  return "$path/$fnameNoExt ($i).$ext";
}

Usage:

for ($i=1; $i<=10; $i++) {
  $newName = newName2($fullpath);
  file_put_contents($newName, 'asdf');
}
like image 24
Fandi Susanto Avatar answered Oct 14 '22 06:10

Fandi Susanto


There are several ways for renaming image in PHP before uploading to the server. appending timestamp, unique id, image dimensions plus random number etc.You can see them all here

First, Check if the image filename exists in the hosted image folder otherwise upload it. The while loop checks if the image file name exists and appends a unique id as shown below ...

function rename_appending_unique_id($source, $tempfile){

    $target_path ='uploads-unique-id/'.$source;
     while(file_exists($target_path)){
        $fileName = uniqid().'-'.$source;
        $target_path = ('uploads-unique-id/'.$fileName);
    }

    move_uploaded_file($tempfile, $target_path);

}

if(isset($_FILES['upload']['name'])){

    $sourcefile= $_FILES['upload']['name'];
    tempfile= $_FILES['upload']['tmp_name'];

    rename_appending_unique_id($sourcefile, $tempfile);

}

Check more image renaming tactics

like image 1
Daniel Nyamasyo Avatar answered Oct 14 '22 06:10

Daniel Nyamasyo