复制和重命名文件到同一个目录,而不删除原始文件[复制](Copy & rename a file t

2019-07-30 08:57发布

可能重复:
克隆+重命名与PHP文件

这应该是很容易的。 我wan't复制和重命名已经存在于服务器上,同时仍保留原始图像的图像。

这里的原始图像的位置:

images/
   folder/
       one.jpg

这就是我要的:

images/
   folder/
       one.jpg 
       one_thumb.jpg

我怎样才能做到这一点? 你可以看到我不只是简单地重命名现有的文件/图片。 我想复制和重命名为相同的目录。

Answer 1:

只需使用复制方法: http://php.net/manual/en/function.copy.php

例如:

<?php
$file = 'images/folder/one.jpg';
$newfile = 'Images/folder/one_thumb.jpg';

if (!copy($file, $newfile)) {
    echo "failed to copy";
}


Answer 2:

PHP有一个功能, 复制内置可以做到这一点。 下面是一个例子:

<?php
$file = 'one.jpg';
$newfile = 'one_thumb.jpg';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

该函数返回一个布尔值,指示复制是否成功。 就这么简单!



文章来源: Copy & rename a file to the same directory without deleting the original file [duplicate]