chappolino Posted December 15, 2008 Share Posted December 15, 2008 Basically, before uploading i have pre_replace replace spaces with underscores. Then i have a function checking whether file exists in the upload directory. Problem:: --> when there are no spaces in filename and file already exists it does not get uploaded and displays err msg :'file exists'. When filename does contain spaces, and it already exists in the directory the function to check whether file exists fails resulting in duplicate record added and the file overwritten. Please help me to code the part that checks whether record & file already exists even if filename has spaces. I am a total noob, i would really appreciate anyone helping me out. <?php $uploadDir = 'resumes/'; if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $username = $_SESSION['username']; $filePath = $uploadDir . $fileName; if (file_exists($filePath)) { echo "<font color=\"red\">The file $filename exists. Please rename the file and upload</font>"; exit; } else { echo "The file $filename has been uploaded"; } $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "..."; exit; } if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileName = $uploadDir . $fileName; $file_Name2 = $uploadDir . $file_Name2; $file_name2 = preg_replace('/\s/', '_', $fileName); $trazz = rename ($fileName, $file_name2); $fileName = preg_replace('/\s/', '_', $fileName); $query = "INSERT INTO upload2 (name, size, type, path, username ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$username')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); echo "<br><font color=\"green\">*</font> Thank you. Your resume has been successfully uploaded."; echo "<br><font color=\"green\">*</font> One of our Career Services representatives will contact you shortly<br>"; {} ?> Link to comment https://forums.phpfreaks.com/topic/137097-check-if-record-exists-i-am-lost-pliz-help/ Share on other sites More sharing options...
bobbinsbro Posted December 15, 2008 Share Posted December 15, 2008 first of all, in the future please use the code tags. (the button with the # on it in the post editor). as for your problem: 1) change preg_replace() with str_replace(). i have seen it written all over the place that str_replace() is the preferred function for simple replaces (such as yours). 2) move the replace line up before the $filepath definition line like so: $fileName = str_replace(' ', '_', $fileName); $username = $_SESSION['username']; $filePath = $uploadDir . $filename; (note the use of code tags ). good day. Link to comment https://forums.phpfreaks.com/topic/137097-check-if-record-exists-i-am-lost-pliz-help/#findComment-716166 Share on other sites More sharing options...
genericnumber1 Posted December 15, 2008 Share Posted December 15, 2008 str_replace() is preferred because while regular expressions are powerful, they're slow compared to simple operations such as the one str_replace() performs. Link to comment https://forums.phpfreaks.com/topic/137097-check-if-record-exists-i-am-lost-pliz-help/#findComment-716237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.