Solved

how to not overwrite image using php copy command

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

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 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