eldan88 Posted March 7, 2013 Share Posted March 7, 2013 Hey, I have written some code below to see if a file exists before uploading the file into the server, and if it does exists then assign the message to the variable $message and the echo it out if $message is not empty. However it doesn't seem to work. I'm not sure what I am doing incorrectly here. The code is under line 19 and 20. $upload_errors = array( UPLOAD_ERR_OK => "No Errors.", UPLOAD_ERR_INI_SIZE => "Large than upload_max_filesize", UPLOAD_ERR_FORM_SIZE => "Larger than from MAX_FILE_SIZE", UPLOAD_ERR_PARTIAL => "Partial Upload", UPLOAD_ERR_NO_FILE => "No File", UPLOAD_ERR_CANT_WRITE => "Can't write to the disk", UPLOAD_ERR_EXTENSION => "File upload stopped by extension"); if(isset($_POST['submit'])) { $tmp_file = $_FILES['file_upload']['tmp_name']; $target_file = basename($_FILES['file_upload']['name']); $upload_dir = "uploads"; $new = file_exists(basename($_FILES['file_upload']['name'])) ? false:true; // Set this to false if the file already exists if($new)// test to see if the file is a new file, and if it is then do the following otherwise diplay message { if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) { $message = "File uploaded successfully."; } else { $error = $_FILES['file_upload']['error']; $message = $upload_errors[$error]; } } } else { $message = "File could not be written because it already exists"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php if(!empty($message)) { echo "<p>{$message}</p>"; } ?> <form action="uploads.php" enctype="multipart/form-data" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> <input type="file" name="file_upload" /> <input type="submit" name="submit" value="upload" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/275383-need-help-checking-if-a-file-exists-before-uploading/ Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 (edited) Because you are only passing a file name to the function file_exists(). You need to tell it "where" to look. Your comment states: // Set this to false if the file already exists So, I assume you should be looking in the folder where you store these files after upload, i.e. $upload_dir Edited March 7, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/275383-need-help-checking-if-a-file-exists-before-uploading/#findComment-1417314 Share on other sites More sharing options...
P5system Posted March 8, 2013 Share Posted March 8, 2013 You have to chek the file in directory, so pass directory path as well Quote Link to comment https://forums.phpfreaks.com/topic/275383-need-help-checking-if-a-file-exists-before-uploading/#findComment-1417436 Share on other sites More sharing options...
eldan88 Posted March 10, 2013 Author Share Posted March 10, 2013 Because you are only passing a file name to the function file_exists(). You need to tell it "where" to look. Your comment states: // Set this to false if the file already exists So, I assume you should be looking in the folder where you store these files after upload, i.e. $upload_dir Oh i see what you are saying! Makes a lot of sense now. Thank you for pointing that out! Quote Link to comment https://forums.phpfreaks.com/topic/275383-need-help-checking-if-a-file-exists-before-uploading/#findComment-1417861 Share on other sites More sharing options...
eldan88 Posted March 10, 2013 Author Share Posted March 10, 2013 You have to chek the file in directory, so pass directory path as well Will do Thank you P5system. Quote Link to comment https://forums.phpfreaks.com/topic/275383-need-help-checking-if-a-file-exists-before-uploading/#findComment-1417862 Share on other sites More sharing options...
P5system Posted March 12, 2013 Share Posted March 12, 2013 Sure Let me know if there is any further queries eldan88 Quote Link to comment https://forums.phpfreaks.com/topic/275383-need-help-checking-if-a-file-exists-before-uploading/#findComment-1418098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.