Page 1 of 1

file_exists- possible to check before file is loaded?

Posted: Fri May 26, 2006 12:47 pm
by Luke
I have a (horrible) file management system that allows users to upload files, but when you do, it won't tell you that the file already exists (if it does) until it has loaded the entire file into memory (or wherever it loads it). Is there a way to check before it loads this? Here's what I've done, but it doesn't work...

Code: Select all

$ERROR_FUNCTION = false;
    // File Size in bytes (change this value to fit your need)
    Echo "Biggest File Size=" . $_SESSION['Size_Bytes'] . "</td></tr>";
    Echo "Upload Dir=" . $UPLOAD_DIR . "</td></tr>";
    If ($_SESSION['File_Extension'] != "") {
        Echo "<tr><td class='content'>Extensions allowed= " . $_SESSION['File_Extension'] . "</td></tr>";
    }
    // check if the directory exists or not with the server path added
    $REAL_UPLOAD_DIR = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRNAME($_SESSION['Server_Path'])) . DIRECTORY_SEPARATOR . $UPLOAD_DIR;
    If (!IS_DIR($REAL_UPLOAD_DIR)) {
        Echo "<tr><td class='content'><strong>The directory where to upload doesn't exist, please verify.. </strong></td></tr>";
        $ERROR_FUNCTION = true;
    } Else {
        // check if the directory is writable.
        If (!IS_WRITEABLE($REAL_UPLOAD_DIR)) {
            Echo "<tr><td class='content'><strong>The directory where to upload is NOT writable, please put the write attribute permissions on </strong></td></tr>";
            $ERROR_FUNCTION = true;
        } Else {
			// $Filename will hold the value of the file name submitted from the form.
			$FILENAME = $_FILES['filetoupload']['name'];
			// Check if file is Already EXISTS.
			If (File_Exists($REAL_UPLOAD_DIR . DIRECTORY_SEPARATOR . $FILENAME)) {
				Echo "<tr><td class='content'><strong>Sorry but the file named <b>" . $FILENAME . "</b> already exists in the server, please change the filename before UPLOAD</strong></td></tr>";
				$ERROR_FUNCTION = true;
			} Else {			// check if no file selected.
				If (!IS_UPLOADED_FILE($_FILES['filetoupload']['tmp_name'])) {
					Echo "<tr><td class='content'><strong>Error: Please select a file to upload!. </strong></td></tr>";
					$ERROR_FUNCTION = true;
				} Else {
					// Get the Size of the File
					$SIZE = $_FILES['filetoupload']['size'];
					// Make sure that file size is correct
					If ($SIZE > $_SESSION['Size_Bytes']) {
						$KB = $_SESSION['Size_Bytes'] / 150000000000;
						Echo "<tr><td class='content'><strong>File Too Large. File must BE LESS THAN <b>$KB</b> KB. </strong></td></tr>";
						$ERROR_FUNCTION = true;
					} Else {
						// check file extension
						If (($_SESSION['File_Extension'] != "") && (!IS_FILE_TO_DISPLAY($_FILES['filetoupload']['name']) != false)) {
							Echo "<tr><td class='content'><strong>Wrong file extension. </strong></td></tr>";
							$ERROR_FUNCTION = true;
						} Else {
							// Move the File to the Directory choosen + the server path determined
							// move_uploaded_file('filename','destination') Moves afile to a new location.
							If (!MOVE_UPLOADED_FILE($_FILES['filetoupload']['tmp_name'], $REAL_UPLOAD_DIR . DIRECTORY_SEPARATOR . $FILENAME)) {
								// Print error msg.
								Echo "<tr><td class='content'><strong>There was a problem moving your file. </strong></td></tr>";
								$ERROR_FUNCTION = true;
							} Else {
								// tell the user that the file has been uploaded.
								Echo "<tr><td class='content'><strong>File SUCCESSFULLY uploaded! </strong></td></tr>";
							}
						}
					}
				}
			}
		}
    }
I thought that checking if the file exists before it puts the file where it belongs would work, but I guess it actually uploads it to a temp directory first or something?

Posted: Sat May 27, 2006 2:16 am
by RobertGonzalez
If the upload dir is not dynamic, try hardcoding the path into the script. Then maybe have it exit if the file exists (instead of echo something). Another thing to check for is the naming of the file (spaces/capitalization/etc) as different server OS's handle these different. All of this is speculation. I have not tried it, just throwing out ideas.