php 将文件复制并重命名到同一目录而不删除原始文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11442779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:31:56  来源:igfitidea点击:

Copy & rename a file to the same directory without deleting the original file

phpfilefile-uploadfile-storage

提问by Graham

Possible Duplicate:
Clone + Rename file with PHP

可能的重复:
使用 PHP 克隆 + 重命名文件

This should be pretty easy. I wan't to copy & rename images that already exist on the server while still retaining the original image.

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

Here's the original image location:

这是原始图像位置:

images/
   folder/
       one.jpg

This is what I want:

这就是我要的:

images/
   folder/
       one.jpg 
       one_thumb.jpg

How can I achieve this? You can see I'm not just simply renaming an existing file / image. I want to copy it and rename it to the same directory.

我怎样才能做到这一点?您可以看到我不仅仅是简单地重命名现有文件/图像。我想复制它并将其重命名为同一目录。

回答by Sybio

Just use the copy method: http://php.net/manual/en/function.copy.php

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

Ex:

前任:

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

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

回答by Andrew

PHP has a function, copybuilt-in that can do this. Here's an example:

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

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

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

The function returns a boolean indicating whether the copy was successful. It's as simple as that!

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