Image upload

I’m dying in troubleshooting in this thing. I did it already in the other page, I keep on comparing but I can’t find the problem. It is just a simple image upload…

html


<form action = "image_act.php" method = "post" target = "stud_iframe" >
		<label for="file">Filename:</label>
		<input type="file" name="images" id="images" required/>
		<input type = 'submit' name = 'submit' value = "Submit" />
		<br/>
			
</form>	
		<iframe class = "stud_frame" name = "stud_iframe" src="image_act.php" style="background-color: #dfdfdf"></iframe>
		


image_act.php


if (isset($_POST['submit']))
{
echo $img_name = addslashes($_FILES['images']['name']);
echo $tmp_name = $_FILES['images']['tmp_name'];
echo $img_size = $_FILES['images']['size'];
echo $img_type = $_FILES['images']['type'];
echo $max_size = 2097152;
echo $extension = strtolower(substr($img_name, strpos($img_name, '.') + 1));
echo $img_error = $_FILES['image']['error'];
}

I can’t see any result after printing this all …

Remember to add the following attribute to your form or file input fields will be ignored.

enctype=“multipart/form-data”

This line

echo $img_name = addslashes($_FILES['images']['name']);

does not display the value of $img_name. What it does is display the result of the = operation, which is TRUE.
Try

$img_name = addslashes($_FILES['images']['name']);
echo $img_name;

The same goes for the other echoes in the code you posted.

I recommend using var_dump for debug output, you will get a more insight in to what data you are actually getting and what type it is.
What guido2004 said is also correct. I think you also need the right enctype in your form attributes.

If you try this:

echo $a = 'Hello World!';

then the result on screen will be “Hello World!”, what happens here is that:
1.) ‘Hello World’ is assigned to $a
2.) content of $a is displayed on screen

The problem in the form is missing enctype which should be:

enctype="multipart/form-data"

For debugging purposes you should use something like this:

echo '<pre>'; var_dump($_POST); echo '</pre>';
echo '<pre>'; var_dump($_FILES); echo '</pre>';

I don’t recommend using this:

isset($_POST['submit'])

‘submit’ is a key of $_POST array so in my opinion you should use function array_key_exists.

You’re absolutely right. My bad.

$_FILES[‘image’][‘error’];

Is the var name ‘images’ or ‘image’?

The first thing to do is check for an error number upload error codes

Relying on isset($_POST[‘submit’]) used to be prone to failure too, as in some browsers the user hitting the Enter key submits the form but not the submit button value IIRC.

Something like:


if( $_FILES['images']['error'] > 0 ) {
// fail, redirect, exit

}else{
// at least you should actually have a file to handle from here

}

… might be a better way to go.