Question

I'm trying to create file upload with php. But $_FILES['photo']['name'] returns nothing. Then I googled lot and find out about the var_dump method. When I run it. It returns following array.

array (size=5)
  'name' => string '' (length=0)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 4
  'size' => int 0

name, type and tmp_name is blank.

Here is the full code. (PHP)

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

     var_dump($_FILES['photo']);
}

HTML

<form class="bs-docs-example" action="upload.php" method="post" enctype="multipart/form-data">
        <fieldset>
            <legend>File Upload</legend>

            <label><input type="file" name="photo" size="25" /></label>
            <label><input type="file" name="photo" size="25" /></label>
            <label><input type="file" name="photo" size="25" /></label>

            <label><button type="submit" name="add_video" value="Submit" class="btn">Submit</button></label>

        </fieldset>
</form>

Any suggestions. Thanks in Advance.

Was it helpful?

Solution

When you have multiple form elements with the same name, they'll override each other. PHP is only respecting the last input with that name, which is probably blank, and is ignoring the earlier one you filled in.

You can look at the PHP manual on multiple file uploads, but the upshot is that you need to have [] at the end of the input name if you want PHP to "see" all of them and organize them into an array for you. Otherwise, have only one input, or just pick different names for each.

OTHER TIPS

        <label><input type="file" name="photo" size="25" /></label>
        <label><input type="file" name="photo" size="25" /></label>
        <label><input type="file" name="photo" size="25" /></label>

Three elements with same name. Thats probably why

Try

 var_dump($_POST);

To see if you have anything posted returning, but yes having 3 elements of the same name is an issue.

<label><input type="file" name="photo1" size="25" /></label>
<label><input type="file" name="photo2" size="25" /></label>
<label><input type="file" name="photo3" size="25" /></label>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top