PHP POST method not posting “type=file” variable

PHP POST method not posting “type=file” variable

I am updating some old PHP to move away from global variables and use $_GET, $_POST, etc (among plenty of other things).  I bumped into an issue where my image upload form was not passing type=file variables to $_POST. The form looks like so:

<div class="block">
<label>upload photo</label> 
	<input type="file" name="userpic">
</div>

I couldn’t get the value via the following:

$userpic = $_POST['userpic'];

W3Schools to the rescue.

By using the global PHP $_FILES array you can upload files from a client computer to the remote server. The first parameter is the form’s input name and the second index can be either “name”, “type”, “size”, “tmp_name” or “error”.

The answer is therefore:

$userpic = $_FILES['userpic']['name'];

Hope this helps someone!

Mike250

I'm an Australian Chief Analytics Officer passionate about data science, visual insights, and all things sport—particularly cricket. An adventurer at heart, I’ve gone from abseiling cliffs to snorkeling in crystal-clear waters, sleeping in the wilds of Africa, and exploring destinations worldwide, with my latest trip taking me to Bali. When I'm not diving into data or analytics, I'm spending time with my three sons and two daughters, attempting to hit sixes for my local cricket club, reviewing chicken schnitzels or honing my craft around a coffee machine.

Related Posts
Leave a comment

Your email address will not be published. Required fields are marked *