The filename for file to be uploaded is different from the one uploaded. Eg: the file to be uploaded is : DSS_Chapter_12 f f___h.pdf I renamed it in php code as : dss_chapter_12_f_f_h.pdf file uploaded name: dss_chapter_12 My code: $sourceFile = $_FILES["sourceFile"]["name"]; $target_path = "thesis/" $restrictChar=array("/","\\",":","?","\"","<",">","|",",","\.","*"," ","(",")","&","'"); $sourceFile = trim(strtolower($sourceFile)); $sourceFile = trim(str_replace($restrictChar,"_",basename( $sourceFile))); $sourceFile = FilterArray(basename($sourceFile)); $target_path .= basename($sourceFile); $sourceFileTemp =$_FILES['sourceFile']['tmp_name']; if(move_uploaded_file($sourceFileTemp, $target_path)) { if (is_uploaded_file($sourceFile)) { echo "File ". $sourceFile ." uploaded successfully.\n"; echo "Displaying contents\n"; } else { echo "error!"; } } function FilterArray($myArray) { for($j=1;$j<strlen($myArray);$j++) { $k=$j-1; if(($myArray[$j] == "_")&&($myArray[$k]=="_")) { $myArray[$k] =""; } } return $myArray; }
Try replacing spaces in filenames as _ $sourceFile = $_FILES["sourceFile"]["name"]; $target_path = "thesis/" $restrictChar = array("/", "\\", ":", "?", "\"", "<", ">", "|", ",", "\.", "*", " ", "(", ")", "&", "'", " "); $sourceFile = trim(strtolower($sourceFile)); $sourceFile = str_replace($restrictChar, "_", basename($sourceFile)); $sourceFile = FilterArray(basename($sourceFile)); $target_path .= basename($sourceFile); $sourceFileTemp = $_FILES['sourceFile']['tmp_name']; if(move_uploaded_file($sourceFileTemp, $target_path)) { if(is_uploaded_file($sourceFile)) { echo "File ". $sourceFile ." uploaded successfully.\n"; echo "Displaying contents\n"; } else { echo "error!"; } } function FilterArray($myArray) { for($j = 1; $j < strlen($myArray); $j++) { $k = $j-1; if(($myArray[$j] == "_") && ($myArray[$k]=="_")) { $myArray[$k] = ""; } } return $myArray; } PHP: