I am trying to copy a file to another directory, but to prevent overwriting a file inside it with the same name, I check it first and if a file with the same name already exists, then I want to rename the file from, lets say file.jpg
to file(1).jpg
and copy it. I do not succeed in renaming it "on the fly".
This is what I have so far:
$src_file_url = $_POST['copyfile']; //eg uploads/name/file.jpg
$fileName = basename($src_file_url);
$new_dest = $_POST['copyfile-destination'];
/* New file name and path for this file */
$dest_file = 'uploads/'.$UserID.'/'.$new_dest.'/'.$fileName;
if (file_exists($dest_file)) {
rename($src_file_url, $fileName.'(1)');
copy( $src_file_url, $dest_file );
exit;
}