Fishcakes Posted April 7, 2021 Share Posted April 7, 2021 So far I have managed to create an upload process which uploads a picture, updates the database on file location and then tries to upload the db a 2nd time to update the Thumbnails file location (i tried updating the thumbnails location in one go and for some reason this causes failure) But the main problem is that it doesn't upload some files Here is my upload.php <?php include 'dbconnect.php'; $statusMsg = ''; $Title = $conn -> real_escape_string($_POST['Title']) ; $BodyText = $conn -> real_escape_string($_POST['ThreadBody']) ; // File upload path $targetDir = "upload/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); $Thumbnail = "upload/Thumbnails/'$fileName'"; if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf', "webm", "mp4"); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; $targetFilePathArg = escapeshellarg($targetFilePath); $output=null; $retval=null; //exec("convert $targetFilePathArg -resize 300x200 ./upload/Thumbnails/'$fileName'", $output, $retval); exec("convert $targetFilePathArg -resize 200x200 $Thumbnail", $output, $retval); echo "REturned with status $retval and output:\n" ; if ($retval == null) { echo "Retval is null\n" ; echo "Thumbnail equals $Thumbnail\n" ; } }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, mp4, webm & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } //Update SQL db by setting the thumbnail column to equal $Thumbnail $update = $conn->query("update Threads set thumbnail = '$Thumbnail' where filename = '$fileName'"); if($update){ $statusMsg = "Updated the thumbnail to sql correctly."; echo $statusMsg ; } else { echo "\n Failed to update Thumbnail. Thumbnail equals $Thumbnail" ; } // Display status message echo $statusMsg; ?> And this does work on most files however it is not working on a 9.9mb png file which is named "test.png" I tested on another 3.3 mb gif file and that failed too? For some reason it returns the following Updated the thumbnail to sql correctly.Updated the thumbnail to sql correctly. Whereas on the files it works on it returns REturned with status 0 and output: Retval is null Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif' Failed to update Thumbnail. Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif'The file rainbow-trh-stache.gif has been uploaded successfully. Any idea on why this is? Quote Link to comment https://forums.phpfreaks.com/topic/312437-trying-to-figure-out-why-my-uploadphp-files-is-rejecting-some-files/ Share on other sites More sharing options...
Barand Posted April 7, 2021 Share Posted April 7, 2021 Are there any clues in $_FILES['file']['error'] ? Quote Link to comment https://forums.phpfreaks.com/topic/312437-trying-to-figure-out-why-my-uploadphp-files-is-rejecting-some-files/#findComment-1585641 Share on other sites More sharing options...
Fishcakes Posted April 7, 2021 Author Share Posted April 7, 2021 I input the error function here to see what error it's returning //Update SQL db by setting the thumbnail column to equal $Thumbnail $update = $conn->query("update Threads set thumbnail = '$Thumbnail' where filename = '$fileName'"); if($update){ $statusMsg = "Updated the thumbnail to sql correctly."; echo $statusMsg . "<br> </br>"; echo "Error : " . $_FILES['file']['error'] . "<br>"; } and the output I get is Updated the thumbnail to sql correctly. Error : Updated the thumbnail to sql correctly. Which is weird as it definitely has not updated sql correctly or uploaded the thumbnail Quote Link to comment https://forums.phpfreaks.com/topic/312437-trying-to-figure-out-why-my-uploadphp-files-is-rejecting-some-files/#findComment-1585642 Share on other sites More sharing options...
Fishcakes Posted April 7, 2021 Author Share Posted April 7, 2021 10 minutes ago, Codemafia said: Figure it out. We're not going to develop your entire website. Quote Link to comment https://forums.phpfreaks.com/topic/312437-trying-to-figure-out-why-my-uploadphp-files-is-rejecting-some-files/#findComment-1585645 Share on other sites More sharing options...
Barand Posted April 7, 2021 Share Posted April 7, 2021 $_FILES['file']['error'] shouldn't be blank. Even if no file is selected for upload it will be "4", and "0" if there is no error. Post your input form code. Quote Link to comment https://forums.phpfreaks.com/topic/312437-trying-to-figure-out-why-my-uploadphp-files-is-rejecting-some-files/#findComment-1585646 Share on other sites More sharing options...
Barand Posted April 7, 2021 Share Posted April 7, 2021 Those single quotes shouldn't be there $Thumbnail = "upload/Thumbnails/'$fileName'"; ^ ^ Quote Link to comment https://forums.phpfreaks.com/topic/312437-trying-to-figure-out-why-my-uploadphp-files-is-rejecting-some-files/#findComment-1585647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.