php - file_exists not behaving properly -
i writing script upload file part of php project. if file exists same name, instead of moving file anyway , overwriting whatever there, using header() refresh page , add message rename file before uploading.
however, not working. program moves file on regardless if name taken. have tested if(file_exists($_server['document_root'] . "/uploads/" . $_files['uploadfile']['name']))
, seems work independently not within structure of program. can't figure out goes wrong though.
here code
<?php include ('includes/openfile.php'); if(isset($_files['uploadfile'])) { $allowed = array('image/jpg', 'image/jpeg', 'image/png', 'text/plain'); // validate file type if(in_array($_files['uploadfile']['type'], $allowed)) { //check if file exists current file name in order prevent overwrite -- prevents overwrite .htaccess attack protecting user files if(file_exists($_server['document_root'] . "/uploads/" . $_files['uploadfile']['name'])) { // file must have unique name header("location: file-upload.php?msg=" . urlencode(base64_encode("please rename file."))); } else { // file has unique name, move uploads directory move_uploaded_file($_files['uploadfile']['tmp_name'], "../uploads/" . $_files['uploadfile']['name']); } } else { header("location: file-upload.php?msg=" . urlencode(base64_encode("please upload .jpg, .jpeg, .png, or .txt file type"))); } // check file error if($_files['uploadfile']['error'] > 0) { echo "<h1 class='jumbotron error'>there error because </h1>"; switch ($_files['uploadfile']['error']) { case 1: echo "<h2 class='jumbtron'>file exceeds upload max file size</h2>"; break; case 2: echo "<h2 class='jumbtron'>file exeeds max file size in html form</h2>"; break; case 3: echo "<h2 class='jumbtron'>file partially uploadeded</h2>"; break; case 4: echo "<h2 class='jumbtron'>no file uploaded</h2>"; break; case 6: echo "<h2 class='jumbtron'>no temporary folder available</h2>"; break; case 7: echo "<h2 class='jumbtron'>unable write disk</h2>"; break; case 8: echo "<h2 class='jumbtron'>file upload stopped</h2>"; break; default: echo "<h2 class='jumbtron'>a system error has occured</h2>"; break; } } else { # todo: encrypt uploaded file // delete tmp file if(file_exists($_files['uploadfile']['tmp_name']) && is_file($_files['uploadfile']['tmp_name'])) { unlink($_files['uploadfile']['tmp_name']); } // let user know file succesfully uploaded header("location: file-upload.php?success=" . urlencode(base64_encode("file uploaded successfully!"))); } } include ('includes/closefile.php'); ?>
thanks help!
Comments
Post a Comment