Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to upload a file, I want to test if the file is uploaded, so trying to put $_FILES in alert message, but it returns blank!

What I have tried:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">

  <?php
  if ($_POST[submit]) {
  $uploaddir = 'Meetings/';
  $uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);
  echo "<script>alert('$uploadfile')</script>";
}
  ?>

</form>
</body>
</html>
Posted
Updated 3-Feb-21 12:49pm
v3

1 solution

The array $_FILES includes information about each uploaded file
$_FILES['yourfilename']['name']
$_FILES['yourfilename']['type']
$_FILES['yourfilename']['size']
$_FILES['yourfilename']['tmp_name']
$_FILES['yourfilename']['error']
The last one is what you need. Take a few minutes to read and understand PHP: POST method uploads - Manual[^]
 
Share this answer
 
Comments
Member 14819235 4-Feb-21 18:57pm    
Thank you Peter_in_2780, but the issue is, when I try to check the name of the uploaded file using echo "alert('$uploadfile')"; it returns "Meeting/", ignoring "basename($_FILES['fileToUpload']['name'])" this means my uploaded file name is not read by the script!
Any ideas?
Peter_in_2780 4-Feb-21 19:09pm    
You can use the same page to initiate the upload (the "form" stuff) and process the upload (handling $_POST and $_FILES) but it is much easier to see what is going on if you separate them. For example you can (for testing) put
var_dump($_FILES);
at the start of the processing page to see exactly what you are getting. In the whole process you need to understand what is happening where and when, on the client and host.
Member 14819235 6-Feb-21 16:26pm    
Can you please suggest a simple script to upload a photo, even without checking any image type or size, in a folder name "Meetings" on the server, just to ensure that it's loaded?
Note: Upload is ON in the php.ini file
Member 14819235 7-Feb-21 11:30am    
Ok, the error is my php file name, it's upload.php, it conflicts with something on the server, when I changed the file name, it worked!
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900