0

I have a problem here im trying to upload a file

first time it is moving the filename from temp it its respective directory,

but again i try ot upload the aa different file with the same name it should rename the first time uploaded file

with date_somefilename.csv and give the filename to its original state

for example a file test.csv ,im uploading it for first time it will upload to corresponding directory as

test.csv,when i upload a different csv file with same name test.csv

I need to get the

test.csv (latest uploaded file)

06222012130209_test.csv(First time uploaded file)

The code is below

$place_file = "$path/$upload_to/$file_name";     



if (!file_exists('uploads/'.$upload_to.'/'.$file_name)) 
 {

move_uploaded_file($tmp, $place_file);  


}else{

 move_uploaded_file($tmp, $place_file); 
 $arr1 = explode('.csv',$file_name);
  $todays_date =  date("mdYHis");
   $new_filename = $todays_date.'_'.$arr1[0].'.csv';
  echo  $str_cmd = "mv " . 'uploads/'.$upload_to.'/'.$file_name . " uploads/$upload_to/$new_filename";
   system($str_cmd, $retval); 
} 
mark rammmy
  • 1,478
  • 5
  • 27
  • 61

5 Answers5

2

See comments in code.

$place_file = "$path/$upload_to/$file_name";     

if (!file_exists($place_file)) {
    move_uploaded_file($tmp, $place_file);  
} else {
    // first rename
    $pathinfo = pathinfo($place_file);
    $todays_date = date("mdYHis");
    $new_filename = $pathinfo['dirname'].DIRECTORY_SEPARATOR.$todays_date.'_'.$pathinfo['basename'];
    rename($place_file, $new_filename)
    // and then move, not vice versa
    move_uploaded_file($tmp, $place_file); 
} 

DIRECTORY_SEPARATOR is php constant. Value is '/' or '\', depending of operation system.

pathinfo() is php function, that return information about path: dirname, basename, extension, filename.

Dador
  • 5,277
  • 1
  • 19
  • 23
1

What about...

$place_file = "$path/$upload_to/$file_name";     

if (file_exists($place_file)) {
   $place_file = date("mdYHis")."_".$file_name;
}

if (!move_uploaded_file($tmp, $place_file)) {
   echo "Could not move file";
   exit;
}
roycable
  • 301
  • 1
  • 9
1

I would not add a date to the file if it already exists. Instead I would just add a number to the end of it. Keep it simple.

$counter = 0;
do {
    // destination path path
    $destination = $path.'/'.$upload_to.'/';

    // get extension
    $file_ext = end(explode('.', $file_name));

    // add file_name without extension
    if (strlen($file_ext))
        $destination .= substr($file_name, 0, strlen($file_name)-strlen($file_ext)-1);

    // add counter
    if ($counter)
        $destination .= '_'.$counter;       

    // add extension
    if (strlen($file_ext))
        $destination .= $file_ext;

    $counter++;
while (file_exists($destination));

// move file
move_uploaded_file($tmp, $destination);
Vincent
  • 2,342
  • 1
  • 17
  • 22
0
$target = "uploads/$upload_to/$file_name";
if (file_exists($target)) {
    $pathinfo = pathinfo($target);
    $newName = "$pathinfo[dirname]/" . date('mdYHis') . "_$pathinfo[filename].$pathinfo[extension]";
    rename($target, $newName);
}
move_uploaded_file($tmp, $target); 

Beware though: Security threats with uploads.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
0

how about something like this?

<?php

$tmp = '/tmp/foo'; // whatever you got out of $_FILES
$desitnation = '/tmp/bar.xyz'; // wherever you want that file to be saved

if (file_exists($desitnation)) {
  $file = basename($destination)
  $dot = strrpos($file, '.');

  // rename existing file to contain its creation time
  // "/temp/bar.xyz" -> "/temp/bar.2012-12-12-12-12-12.xyz"
  $_destination = dirname($destination) . '/'
      . substr($file, 0, $dot + 1)
      . date('Y-m-d-H-i-s', filectime($destination))
      . substr($file, $dot);

  rename($destination, $_destination);
}

move_uploaded_file($tmp, $destination);
rodneyrehm
  • 13,442
  • 1
  • 40
  • 56