3

The script I made is.

<?php

$source_file = 'http://www.domain.tld/directory/img.png'; 
$dest_file = '/home/user/public_html/directory/directory/img.png'; 

copy($source_file, $dest_file);

?>

I need that image to not be delete and reuploaded every time the script is running. I would either want it to be img1.png, img2.png, img3.png, etc. Or img(Date,Time).png, img(Date,Time).png, etc. Is this possible and if so, how do I do this?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user1861331
  • 31
  • 1
  • 4

4 Answers4

7

If you're concerned with overwriting a file, you could just drop in a timestamp to ensure uniqueness:

$dest_file = '/home/user/public_html/directory/directory/img.png';

// /home/user/public_html/directory/directory/img1354386279.png
$dest_file = preg_replace("/\.[^\.]{3,4}$/i", time() . "$0", $dest_file);

Of if you wanted simpler numbers, you could take a slightly more tasking route and change the destination file name as long as a file with that name already exists:

$file = "http://i.imgur.com/Z92wU.png";
$dest = "nine-guy.png";

while (file_exists($dest)) {
    $dest = preg_replace_callback("/(\d+)?(\.[^\.]+)$/", function ($m) {
        return ($m[1] + 1) . $m[2];
    }, $dest);
}

copy($file, $dest);

You may need to be using a later version of PHP for the anonymous function callback; I tested with 5.3.10 and everything worked just fine.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • And where do I add that in the script, and what do I have to change other then the directory? – user1861331 Dec 01 '12 at 18:31
  • @user1861331 These are drop-in replacements. They will merely change the `$dest_file` path to a unique one for you. – Sampson Dec 01 '12 at 18:48
  • @user1861331 What is not working? I've updated my answer with a solution that I've tested and confirmed to work. – Sampson Dec 01 '12 at 20:45
  • No its working, I can't believe Ive been trying to fix this for like an hour, to release I spelled my username wrong... – user1861331 Dec 01 '12 at 21:13
  • +1 for the excellent idea "just drop in a timestamp to ensure uniqueness" ... might be all you need in certain cases. Thanks! – Mario Awad Oct 20 '14 at 14:30
0
<?php

$source_file = 'http://www.domain.tld/directory/img.png'; 
$dest_file = '/home/user/public_html/directory/directory/img.png'; 
if(!is_file($dest_file)){
copy($source_file, $dest_file);
}
else{
$fname = end(explode('/',$dest_file));
$fname = time().'-'.$fname;
$dest_file = dirname($dest_file).'/'.$fname;
copy($source_file,$dest_file);
}
?>

use this code This will add time before filename

KRUNAL DOSHI
  • 81
  • 1
  • 2
  • 10
  • This doesn't answer the OP. First time it copies, second time it renames, third time it will copy again because you renamed it and the original no longer exists, and the cycle will continue ad-infinitum... – Crisp Dec 01 '12 at 17:42
  • you can use rand() function replace it with time() – KRUNAL DOSHI Dec 01 '12 at 17:52
0

You can use rename().

For Example:

rename ("/var/www/files/file.txt", "/var/www/sites/file1.txt");

Or You can also use copy

$source_file = 'http://www.domain.tld/directory/img.png'; 
$dest_file = '/home/user/public_html/directory/directory/img.png'; 
if(!is_file($dest_file)){
copy($source_file, $dest_file);
}

Or if you want to add time it ,you can try like this.

 $source="http://www.domain.tld/directory/";
 $destn ="/home/user/public_html/directory/directory/";
 $filename="image.png";
 $ex_name = explode('.',$filename));
 $newname = $ex_name[0].'-'.time().$ex_name[1]; //where $ex_name[0] is filename and $ex_name[1] is extension.

 copy($source.filename,$destn.$newname );
Toretto
  • 4,721
  • 5
  • 27
  • 46
0
$source_file = 'http://www.domain.tld/directory/img.png'; 
$dest_file = '/home/user/public_html/directory/directory/img'.uniqid().'.png'; 
copy($source_file, $dest_file);

uniquid gives you a unique Id which is rarely possible to overwrite...

also i would make folders for each month or related to the id of the image

like

mkdir(ceil($imgId / 1000), 0777);
Mik
  • 1,705
  • 1
  • 14
  • 26
  • Warning: copy(...) [function.copy]: failed to open stream: No such file or directory in ... on line 6 – user1861331 Dec 01 '12 at 18:26
  • in this case your source_file doesnt exist or is not accessable.. check the permissions or maybe try another way of downloading the image first http://stackoverflow.com/questions/724391/saving-image-from-php-url-using-php to be sure that you dont overwrite the same file over and over again you can use uniqid() – Mik Dec 02 '12 at 21:12