0

I'm trying to upload files to my server but it doesn't work at all. Here is test code:

<?php
echo count($_FILES['upload']['name']);
?>

<!DOCTYPE html>
<html>
<body>

<form action="" method="POST" enctype="multipart/form-data">
    <input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
    <br>
    <input type="submit" value="Upload">
</form>

</body>
</html>

It always prints 0, file upload is enabled on my server.

13
  • <input name="upload" type="file" accept=".mp3" multiple="multiple" /> remove [] OR var_dump($_FILES['upload']) for multiple files Commented Mar 30, 2016 at 15:35
  • if you make echo count($_FILES['upload']) you'll obtain the correct result. It will be an array of elements with only one element. As @Jeremy suggest, remove the array [] notation to avoid send array instead of propper file (at least if you don't have to send multiple files at once) Commented Mar 30, 2016 at 15:36
  • This should be closed as offtopic > typographycal error. Commented Mar 30, 2016 at 15:36
  • It's still not working even with name="upload" Commented Mar 30, 2016 at 15:39
  • I just tested the code on my live server and it works, time to reinstall wamp.... Commented Mar 30, 2016 at 15:41

2 Answers 2

1

The problem is that you're not counting the $_FILES['upload'].

Simple fix for your problem
Use:

echo count($_FILES['upload']);

Instead of:

echo count($_FILES['upload']['name']);

Edit: Remove [] from the input's name.

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

2 Comments

Still not working, I think the code isn't the problem since it works on live server.
@user47823 I fixed one thing in the code. Try reinstalling your apache server's software (eg. Wamp), then look for the Apache config if it's still not working.
0
  <?php
  echo count($_FILES['upload']);//only this modified//
  ?>

  <!DOCTYPE html>
  <html>
  <body>
       <form action="" method="POST" enctype="multipart/form-data">
            <input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
            <br>
            <input type="submit" value="Upload">
  </form>

  </body>
  </html>

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.