Rocket Rocket - 2 months ago 8
HTML Question

HTML File Upload issue

possibly a stupid question but how do I upload files to a server using the browser ?

I'm sure I've used this before ! But when I run it nothing appears to happen.

No errors are shown and nothing is logged in the error_log

<?php
var_dump($_FILES);
echo $_FILES['uploadFile']['tmp_name'];

?>

<html>
<head>
<title>File Upload Form</title>
</head>

<body>
This form allows you to upload a file to the server.<br>
<form action="test.php" method="post"><br>

Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload File">
</form>


</body>
</html>


What am I doing wrong ?

Answer

When you submit a form, the data you are sending to be encoded somehow to be put in the HTTP request.

By default, this uses the application/x-www-form-urlencoded algorithm which does not support file uploads. You need to use multipart/form-data instead.

<form action="test.php" method="post" enctype="multipart/form-data">