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

陌路散爱 提交于 2019-12-05 11:11:06

问题


Possible Duplicate:
Clone + Rename file with 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.


回答1:


Just use the copy method: 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";
}



回答2:


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

<?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!



来源:https://stackoverflow.com/questions/11442779/copy-rename-a-file-to-the-same-directory-without-deleting-the-original-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!