Search the Community
Showing results for tags 'uploading files'.
-
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>