nvidia Posted June 9, 2010 Share Posted June 9, 2010 Hi, i have created a form used to upload .jpg files onto my server under: www\shanghai2010\images. I am trying to validate the file that i upload already exists or not and if not, add the image file to the server. However, i have a .jpg image to test against in my www\shanghai2010\images\ folder but when i upload a file with the same name as the one in my server it ALWAYS goes into my success message: if(isset($_POST['action']) and $_POST['action'] == 'upload') { $description = mysqli_real_escape_string($link, $_POST['description']); $category = mysqli_real_escape_string($link, $_POST['category']); $file = ($_FILES['photo']['name']); $target = 'www/shanghai2010/images/'; $target .= basename($_FILES['photo']['name']); // Checkk if a file was actually uploaded if(!is_uploaded_file($_FILES['photo']['tmp_name']) ) { $error = 'Error, there was no file uploaded'; include 'error.html.php'; exit(); } //$dirname = "C:\\wamp\\www\\Shanghai_2010\\images"; //$dh = opendir($dirname) or die("couldn't open directory"); if(!isset($_FILES['photo'])) { include 'imageuploadform.html.php'; } else { if(file_exists($target)) { $error = 'This file already exists on the server'; include 'error.html.php'; exit(); } else { echo ($_FILES['photo']['tmp_name']); // move file to server echo 'success this file does not exist on server'; } } } ?> I've checked the file permissions on my windows machine and they are fine, but i don't understand why whenever i try to upload any .jpg file EVEN if it has the same name as the one in my server, it always goes into my success message. I believe therefore there is something wrong with the line: if(file_exists($target)) because php does not go into that section. While doing some googling, came across this code function recursive_file_exists($filename, $directory) { try { /*** loop through the files in directory ***/ foreach(new recursiveIteratorIterator( new recursiveDirectoryIterator($directory)) as $file) { /*** if the file is found ***/ if( $directory.'/'.$filename == $file ) { return true; } } /*** if the file is not found ***/ return false; } catch(Exception $e) { /*** if the directory does not exist or the directory or a sub directory does not have sufficent permissions return false ***/ return false; } //} // if(recursive_file_exists('imageuploadform.html.php', '/www/shanghai2010/')) { echo 'file found'; } else { echo 'File Not Found'; } which would check to see if a file exists in a directory. So i tested it aginast my file and everytime it would echo File Not Found. My php file called index.php resides in the folder shanghai2010, and ofcourse there is a folder called images with my single .jpg image used to test against. Please help if you can guys. I need expert opinon on this Link to comment https://forums.phpfreaks.com/topic/204261-problem-with-_files-validation/ Share on other sites More sharing options...
pdkv2 Posted June 9, 2010 Share Posted June 9, 2010 check your file path "/www/shanghai2010/" try using entire file path. in case of shared hosting the file path will have some more directories before /www/shanghai2010/ ex /xxxx/www/shanghai2010/ Link to comment https://forums.phpfreaks.com/topic/204261-problem-with-_files-validation/#findComment-1069855 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.