Strange error on upload

I have a form which allows uploads. when I try to upload an image I get


Why do I get an error if it is an image?
heres my coide to handle the upload

$target_dir = "/home/users/web/b2054/moo.teamluke/PHP_Class/Assignment_4/uploads/";
$target_file = $target_dir . basename($_FILES["upload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
	
	// Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["upload"]["tmp_name"]);
    if($check == false) {
        echo '<div class="alert alert-danger" role="alert">File is not an image.</div>';
        $uploadOk = 0;
    }
	// Check if file already exists
	if (file_exists($target_file)) {
		echo '<div class="alert alert-danger" role="alert">Sorry, file already exists.</div>';
		$uploadOk = 0;
	}
	// Check file size
	if ($_FILES["upload"]["size"] > $_POST["MAX_FILE_SIZE"]) {
		echo '<div class="alert alert-danger" role="alert">Sorry, your file is too large.</div>';
		$uploadOk = 0;
	}
	// Allow certain file formats
	if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
		echo '<div class="alert alert-danger" role="alert">Sorry, only JPG, JPEG, PNG & GIF files are allowed.</div>';
		$uploadOk = 0;
	}
	// Check if $uploadOk is set to 0 by an error
	if ($uploadOk == 0) {
	  echo '<a href="index.php" class="btn btn-info btn-xs" role="button" style="margin:10px "><span class="glyphicon glyphicon-repeat"></span> Try again</a> ';
	// if everything is ok, try to upload file
	} else {
		if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) {
    echo "<h1>Image, <img src='uploads/".$_FILES['upload']['name']."' class='img-thumbnail'></h1>";
		} else {
			echo '<div class="alert alert-danger" role="alert">Sorry, there was an error uploading your file.</div>';
		}
	}

I see my mistake now, > should be < in the if

nope that wasn’t it.
What happens is that when I try to upload a file larger than 50Kb why do I get


and not “file is to large”

Thanks

You’re checking the return from getimagesize() to see whether it’s false, in order to display that error message:

    $check = getimagesize($_FILES["upload"]["tmp_name"]);
    if($check == false) {

But that function returns an array, or “on failure, false is returned” so for some reason, your call is failing. But then the var_dump you posted suggests that tmp_name is empty, so that’s probably it. Is there an upload file size limit on the server that prevents things happening over 50kb?

I don’t believe so, heres a screenshot of my php.ini file


So that doesn’t seem to be the problem
I do have this in my form

<input type="hidden" name="MAX_FILE_SIZE" value="50000"/>

I tried to upload an image thats like 86Kb and get


I dont see why

    $check = getimagesize($_FILES["upload"]["tmp_name"]);
    if($check == false) {
        echo '<div class="alert alert-danger" role="alert">File is not an image.</div>';
        $uploadOk = 0;
    }

is run and not

	if ($_FILES["upload"]["size"] > $_POST["MAX_FILE_SIZE"]) {
		echo '<div class="alert alert-danger" role="alert">Sorry, your file is too large.</div>';
		$uploadOk = 0;
	}

Because the MAX_FILE_SIZE tells the browser not to upload the file if it’s larger than that size (I think), and so when your code executes the file has not been uploaded, hence you get a false return when you try to get the imagesize, because tmp_name is empty.

Yep it looks like your right, but now I got another question…

If I take that code out of the form and try to upload the same file (88Kb) I get


But why does that even work if I have

	if ($_FILES["upload"]["size"] > $_POST["MAX_FILE_SIZE"]) {
		echo '<div class="alert alert-danger" role="alert">Sorry, your file is too large.</div>';
		$uploadOk = 0;
	}

Is MAX_FILE_SIZE in bytes or kilobytes?

How many bytes are in 88Kb?

isnt it in bytes

so 86 kilobytes = 86000 bytes

But how is that even being set in

http://php_class.teamluke.net/Assignment_4/

Your earlier post shows the value for MAX_FILE_SIZE as 50000

Because 86000 is greater than 50000 the “too large” message is correct.

If you don’t have the ability to change the ini file and restart the server, many settings can be altered by using ini_set() in the script.

http://php.net/manual/en/function.ini-set.php

But perhaps before trying that you should check to see if a small image works OK to see if there may be other problems besides this one?

yes,

	if ($_FILES["upload"]["size"] > 50000) {
		echo '<div class="alert alert-danger" role="alert">Sorry, your file is too large.</div>';
		$uploadOk = 0;
	}

It seems to work with small images


But on larger images

so it looks like the problem was that when I was using MAX_POST_SIZE (as a hidden input field in the form, a large file would simply return an error (making my if($_FILES[“upload”][“size”] > $_POST[“MAX_FILE_SIZE”] totally useless

Thanks all for your help.;…

In addition to using ini_set to tweak the size, if you are allowing very large files you may need to also tweak max_input_time

http://php.net/manual/en/info.configuration.php#ini.max-input-time

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.