Solved

how to not overwrite image using php copy command

Posted on 2010-08-30
3
448 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

19 Experts available now in Live!

Get 1:1 Help Now