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

2

27

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.

Paestum answered 11/7, 2012 at 22:58 Comment(4)
This looks like just copying. What is being renamed exactly?Rudich
Copy it with copy(). Easy peasy.Cilo
with php's copy function maybe ....Bacteriostasis
php.net/manual/en/function.copy.php was the first result for "copy file php". Stop being lazy.Zennas
P
58

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";
}
Pilgarlic answered 11/7, 2012 at 23:2 Comment(0)
R
9

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!

Rozanne answered 11/7, 2012 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.