Abhayadev S
Abhayadev S

Reputation: 37

Simple PHP for File upload form not working?

i am new to this one and in a learning phase. i created a form like below,

<html>
<body>    
<form enctype="multipart/form-data" action="/cgi-bin/FileUpload.php" method="POST">
<label for="file">Filename:</label>
<input type="file" name="ufile"/>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

and my php script looks like,

    #!/usr/bin/php
<?
header("Content-type:text/html");
echo "\n";
if (isset($HTTP_POST_VARS['submit'])){
    echo "HTTP_POST_VARS['submit'] is set" . "<br />";
}

if (empty($_POST)) {
    echo "Empty POST !" . "<br />";
}
if(isset($_POST['type'])){
    echo "POST['type'] is set to " . $_POST['type'] . "<br />";
}

if (isset($_SERVER['REQUEST_METHOD'])){
    echo "_SERVER['REQUEST_METHOD'] is set and REQUEST_METHOD = " . $_SERVER['REQUEST_METHOD'] . "<br />";
}

if (empty($_FILES)){
    echo "_FILES is empty ! " . "<br /> <br />";
}

the putput looks like,

Empty POST ! _SERVER['REQUEST_METHOD'] is set and REQUEST_METHOD = POST _FILES is empty !

the related items in php.ini is like below,

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = /tmp

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

; Maximum size of POST data that PHP will accept.
; http://php.net/post-max-size
post_max_size = 10M

now, why the _POST and _FILES are getting empty ?

Upvotes: 0

Views: 1221

Answers (2)

Kneel-Before-ZOD
Kneel-Before-ZOD

Reputation: 4221

In addition to the comments above, try var_dump($_POST) and var_dump($_FILES); that will dump every file and values posted to the page.
If it's not working, and your php.ini settings are as you wrote earlier, you can try using the ini_set() function on the action page. Use ini_set() to set the file_uploads and the other configuration settings. Ini_set() will not work for all the settings, but it should be enough to get you started.
Don't forget the dependable phpinfo() function also.

Upvotes: 0

Marc B
Marc B

Reputation: 360612

A few things:

1.

    #!/usr/bin/php
^^^^

The spaces before your shebang will be treated as output by the PHP interpreter. The shebang should appear at the very start of your file

2.

HTTP_POST_VARS is deprecated and should not be used anymore, unless you're on an ancient PHP version, in which case you really should upgrade.

3.

Put a phpinfo(); call into your script. It'll show both the global settings AND the local settings. It's possible that a config file (.htaccess, another .ini later in the loading chain, etc...) is overriding your settings and disabling file uploads. If the local column in the phpinfo output doesn't show the proper settings, you'll have to figure out where the override is occuring.

4.

Instead of doing "blahblah is empty" and the like, try doing var_dump($whatever), which'll show you the actual empty array, or its contents (type+size+data) if there is anything present.

Upvotes: 1

Related Questions