Solved

how to not overwrite image using php copy command

Posted on 2010-08-30
3
443 Views
Last Modified: 2012-05-10
Hello,
I'm using the below snippet of php to copy a file from one directory to another, but if the $file already exists in the $upload_directory_to it just overwrites it.  I would rather it keep the original and rename it filename(1) or something like that.  The reason being that I'm inserting a record in my DB each time as even though the $file has the same name, it may be a different file.  Any recommendations?
if (!copy($upload_directory_from.$file, $upload_directory_to.$file)) {
    echo "failed to copy $file...\n";
}

Open in new window

0
Comment
Question by:cgray1223
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Snarfles
ID: 33564436
Try this
if (file_exists($upload_directory_to.$file)) {

    $file = $file."2";

//or rename it however you like in this section

}

Open in new window

0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33564440
Hmm for your filename we need to put it before the extension...
if (file_exists($upload_directory_to.$file)) {



$info = pathinfo($file);

$file_name =  basename($file,'.'.$info['extension']);

$file = $file_name."_2".$info['extension'];

}

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33566167
Getting the paths sorted out is fairly important, but if you've got good code for that you can do something like this.
// NAME OF THE NEW PATH/TO/FILE
$my_new = $upload_directory_to.$file;

// IF IT ALREADY EXISTS
if (file_exists($upload_directory_to.$file))
{
    // CREATE A TIMESTAMPED BACKUP COPY
    $now    = date("Y-m-d\THis'");
    $my_bak = $my_new . '.' . $now . '.bak';
    if (!copy($my_new, $my_bak))
    {
        echo "<strong>Attempted Backup Failed for $my_new!</strong>\n";
    }
}

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now