Solved

how to not overwrite image using php copy command

Posted on 2010-08-30
3
474 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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…
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 …

740 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