简体   繁体   English

如何在不覆盖目标文件的情况下复制文件?

[英]How to copy a file without overwriting the destination file?

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? 我要么想要它是img1.png,img2.png,img3.png等等。或者img(日期,时间).png,img(日期,时间).png等。这是可能的,如果是的话,如何我这样做吗?

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; 您可能需要使用更高版本的PHP进行匿名函数回调; I tested with 5.3.10 and everything worked just fine. 我用5.3.10进行了测试,一切正常。

<?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 使用此代码这将在文件名之前添加时间

You can use rename(). 您可以使用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 );
$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... uniquid为您提供了一个很难覆盖的唯一ID ...

also i would make folders for each month or related to the id of the image 我也会为每个月制作文件夹或与图像的ID相关

like 喜欢

mkdir(ceil($imgId / 1000), 0777);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM