Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to not overwrite image using php copy command

Posted on 2010-08-30
3
Medium Priority
?
492 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

610 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