Unspecified error with a file upload loop

Web Technologies Web Development 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I'm using a form and a loop to upload multiple image files directly to the file server, but I'm getting a false result with the move_uploaded_file function. Upload Form:

Select the files you would like to upload.

The files will be uploaded to a folder named '".$_SESSION['filename']."'.

Multiple file uploading loop (uploadform.php: if (isset($_POST["submit"])){ foreach ($_FILES['fileToUpload']['name'] as $i => $name) { $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"][$i]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); if (strlen($_FILES['fileToUpload']['name'][$i]) > 1) { if (move_uploaded_file($_FILES["fileToUpload"]["name"][$i], $target_file)) { echo "The file'". basename($_FILES["fileToUpload"]["name"][$i]). "' has been uploaded to the server."; } else { echo "
*** ERROR ***
"; echo "Current File basename: ".basename($_FILES["fileToUpload"]["name"][$i])."
"; echo "Current File non-basename: ".$_FILES["fileToUpload"]["name"][$i]."
"; echo "Current File Path: ".$target_file."
"; echo "Image file type: ".$imageFileType."
"; } $count++; } } } When uploading one or multiple files with the form, it goes to the else statement echoing the "ERROR" string. The Apache Error Log comes up blank, so I have no clue what's wrong with the code. I tried echoing the variables used in the loop ($_FILES["fileToUpload"]["name"][$i], $target_file and $imageFileType) but these seem to be fine.

Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ I would put entire foreach loop in try-catch block and see what, if any Exception occurs: try{ // your foreach loop here: } catch(\Exception $e) { echo $e->getMessage(); }

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Web Technologies Links