Jump to content

Need help checking if a file exists before uploading


eldan88

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.