Forum Moderators: coopster

Message Too Old, No Replies

Uploading Files

over writing exisiting file

         

dreamcatcher

7:23 am on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

I`m using this uploader script that lets people upload stuff to my server. Only problem is it overwrites the existing file if a file with the same name is already uploaded.

Whats the best way to check if a file exists and display an error message if it does?

The script is written something like this:

if ($-POST['uploadfile'])

{

if ($file_size < $MAX_FILE_SIZE)

{

copy ($file, $site_path.$file_name);
mail("$your_email", "$email_subject", "$email_message", "From: $site_name <$your_email>\r\nReply-To: $site_name <$your_email>");
unlink($file);

}

Location("http://path/to/thanks/page.php");
exit;

}

Thank you!

mavherick

7:40 am on Apr 20, 2003 (gmt 0)

10+ Year Member



Simply use the is_file [php.net] function to test if the file already exist.

You would use it like this:

if(!is_file($site_path.$file_name))
{
copy ($file, $site_path.$file_name);
mail("$your_email", "$email_subject", "$email_message", "From: $site_name <$your_email>\r\nReply-To: $site_name <$your_email>");
unlink($file);
} else {
// output some error message of your choice
}

mavherick

dreamcatcher

5:13 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers mavherick, works like a charm!

:)