Search code examples
phpfilefile-uploadfile-storage

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


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.


Solution

  • 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";
    }