chappolino Posted December 1, 2008 Share Posted December 1, 2008 I am a noobie and having a common issue of replacing spaces in filename when i upload it. Fortunately i found a way to replace spaces with underscores when moving file to a directory. However when filename is added to a database the spaces are still there. Here's what i got it will probably make more sense than me writing here <form method="post" enctype="multipart/form-data"> <table width="210" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <td width="246"> <input name="userfile" class="cccp" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> <?php $fileName = $_FILES['userfile']['name']; $Dir = 'resumes/'; $filePath = $Dir . $fileName; if (file_exists($filePath)) { echo "The file $filePath already exists. We appologize, please change the filename"; exit; } else { echo "The file $filePath is uploaded"; } ?> <?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; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $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>"; } ?> <? $fileName = $uploadDir . $fileName; $file_Name2 = $uploadDir . $file_Name2; $file_name2 = preg_replace('/\s/', '_', $fileName); $trazz = rename ($fileName, $file_name2); ?> Link to comment https://forums.phpfreaks.com/topic/135068-remove-spaces-from-file-name-before-repository-to-the-db/ Share on other sites More sharing options...
.josh Posted December 2, 2008 Share Posted December 2, 2008 You did a preg_preplace on the $file_name2 and renamed it in the directory but you never did it to $filename in your query. Link to comment https://forums.phpfreaks.com/topic/135068-remove-spaces-from-file-name-before-repository-to-the-db/#findComment-703523 Share on other sites More sharing options...
chappolino Posted December 2, 2008 Author Share Posted December 2, 2008 yes, thank you. Could you possibly show me an example of preg_replace in the query? Link to comment https://forums.phpfreaks.com/topic/135068-remove-spaces-from-file-name-before-repository-to-the-db/#findComment-703599 Share on other sites More sharing options...
.josh Posted December 2, 2008 Share Posted December 2, 2008 umm..you said your preg_replace already worked on your file. Just do the same thing to the $filename before you run the query... Link to comment https://forums.phpfreaks.com/topic/135068-remove-spaces-from-file-name-before-repository-to-the-db/#findComment-703601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.