Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - $_FILES array is empty [duplicate]

Yes, the enctype attribute is set. Other forms/form-hanlders work fine so the temp directory must be writable. I'm out of Ideas.

I checked the post values and $_POST['file'] exists and contains the name of the file.

Here is my form and the PHP that handles it. What am I missing?

<form action='orl_ftp.php' method='post' enctype='multipart/form-data'>
    <table>
        <tr>
            <td>Choose File: </td>
            <td><INPUT type='file' id='file' name='file'></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><INPUT type='submit' name='Submit' value='Process'></td>
        </tr>
    </table>
</form>

And the relevant PHP code. Note that the $_FILES array is set, it's just empty.

if(isset($_POST['Submit'])){
    $upload_results = "";
    if(!isset($_FILES)){$upload_results .= "No files uploaded"; }
    if($upload_results == ""){

        echo "<pre>";
        var_dump($_FILES);
        exit;

        // ...

    }
}
like image 511
I wrestled a bear once. Avatar asked Feb 21 '14 16:02

I wrestled a bear once.


2 Answers

You have multiple forms in the same script and so each of them needs the enctype='multipart/form-data'

Also, it doesn't look like you close the first form and doing <form ... /> is not valid html.

like image 165
elitechief21 Avatar answered Oct 10 '22 12:10

elitechief21


On line 101:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" />
                                                                 ^

This is causing the issue, the browser it keeping this form open and therefore the missing enctype is the issue. Remove or close this form properly.

Example:

<?php
if(isset($_POST['Submit'])){
    $upload_results = "";
    if(!isset($_FILES)){$upload_results .= "No files uploaded"; }
    if($upload_results == ""){
        echo "<pre>";
        var_dump($_FILES);
        exit;
    }
}
?>
<form action='' method="post" />
<form action='' method='post' enctype='multipart/form-data'>
    <table>
        <tr>
            <td>Choose File: </td>
            <td><INPUT type='file' id='file' name='file'></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><INPUT type='submit' name='Submit' value='Process'></td>
        </tr>
    </table>
</form>

This will not post any files.

like image 32
Prisoner Avatar answered Oct 10 '22 14:10

Prisoner