1

This is my first time attempting to upload a file via PHP.

Here is my HTML:

<form role="form" action="api/upload.php" id="uploadForm" method="post" enctype="multipart/form-data">
   <input id="fileToUpload" name="fileToUpload" type="file" class="file" />
   <button type="submit" name="submit" id="submit">Upload</button>
</form>

Now here is the PHP script in reference "api/upload.php":

<?php
$target_dir = "files\\";

if(isset($_POST["submit"])) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
}

if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}

if ($uploadOk == 0) {
echo $uploadOk . "Sorry, your file was not uploaded.";
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {
   echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  } else{
     echo "Sorry, there was an error uploading your file.";
  }
} 
?>

This may be a logic error. I'm not sure. Regardless, I keep getting the message:

Sorry, there was an error uploading your file

When I echo out $uploadOk, it remains as 1. How can I find my error?

4
  • Clearly the move_uploaded_file() operation failed. What are the runtime values of the arguments to that function? Does the target directory exist? Can the PHP process write to it? Commented Aug 25, 2016 at 19:26
  • Your error messages are WRONG. your actual message should be "your file was uploaded, but there was a problem moving it". which means you should investigate WHY the move call failed. And note that your overall uplaod checking needs work too. You simply assume uploads never fail. There's a ['error'] parameter in $_FILES for a reason. check for data in $_FILES doesn't mean an upload succeeded. Failed uploads will STILL populate $_FILES. but unless ['error'] is 0 (aka UPLOAD_ERR_OK), you can't TRUST any of the other $_FILES information. Commented Aug 25, 2016 at 19:27
  • The form is on the main page. The PHP script is in a folder. I have another folder called 'files'. Commented Aug 25, 2016 at 19:29
  • Have you checked to make sure you are getting a string for $_FILES["fileToUpload"]["name"]? And are you sure you shouldn't be using $_FILES["fileToUpload"]["tmp_name"]? Commented Aug 25, 2016 at 19:35

1 Answer 1

2

Use $_FILES["fileToUpload"]["tmp_name"] instead of $_FILES["fileToUpload"]["name"]

Should be

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

Notes:-

$_FILES["fileToUpload"]["name"] is just the name of the uploaded file. $_FILES["fileToUpload"]["tmp_name"] is the temporary file that holds the content.

Hope this helps.

[Edit 1]

I was wrong about adding a value="submit" attribute to the button. name="submit" attribute is sufficient for the isset($_POST["submit")) check.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.