Jump to content

File Upload Messing Up


Swarfega

Recommended Posts

Hi.

 

 

I've got an issue with a simple File (Image) Upload Script, it's the following:

 

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["profilepic"]["name"]));
if ((($_FILES["profilepic"]["type"] == "image/gif")
|| ($_FILES["profilepic"]["type"] == "image/jpeg")
|| ($_FILES["profilepic"]["type"] == "image/png")
|| ($_FILES["profilepic"]["type"] == "image/pjpeg"))
&& ($_FILES["profilepic"]["size"] < 2000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["profilepic"]["error"] > 0)
{
echo "Return Code: " . $_FILES["profilepic"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["profilepic"]["name"] . "<br>";
echo "Type: " . $_FILES["profilepic"]["type"] . "<br>";
echo "Size: " . ($_FILES["profilepic"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["profilepic"]["tmp_name"] . "<br>";

$dir = "images/profilepics/". $_SESSION['SESS_MEMBER_ID'];
if(!is_dir($dir)){
mkdir($dir);
}
move_uploaded_file($_FILES["profilepic"]["tmp_name"],
$dir . "/profile.png");
$newpicpath = $dir . "/profile.png";
}
}
else
{
echo "Invalid file";
}

 

 

Now the actual issue at hand is the following:

 

I've got 1 Live-Test Script and one not-Live Script to test this on, and the problem is, the script itself (Creating the Dir, Moving the Image) only works on the Non-Live Script (Test2.php). However, using the EXACT same code snippet from the Non-Live Script(Test2.php) in the Live Script (ModifyProfile.php), it returns with "Invalid file", regardless of using the same image on both the PHP's.

 

Anyone got a clue as to why this is?

Link to comment
Share on other sites

If the same code works on a different server, your form is likely okay.

 

Unfortunately, the upload code you are using is based on the w3schools example and it is crap. It tests for upload errors after it has tried to use some of the uploaded file information, so if there is an upload error, that code will indicate an invalid file and will never actually display the upload error code. You must test if the upload worked before you can use any of the , [type], [tmp_name], or [name] values.

 

There's a whole host of other problems with that code, such as not lumping together the type and size validation tests, because you will never know which condition caused the validation to fail (wouldn't it be a good idea to tell the visitor if the file was the wrong type OR that he uploaded a file that was too big, so that he could correct what he did), and when validating user supplied input, you should display the incorrect value that failed a test, along with the accepted values.

 

For debugging purposes, add the following code right before the code that you posted -

 

echo '<pre>',print_r($_FILES,true),'</pre>';

Link to comment
Share on other sites

Either uploads are not enabled on the server or the size of the file you are trying to upload is larger then the post_max_size setting, or you actually have something different about your form, such as nesting it inside of another form or the <form tag doesn't have the enctype attribute.

 

What does a phpinfo statement show for the file_uploads and post_max_size settings and what size of file did you try to upload?

Link to comment
Share on other sites

On a different note, is it possible to add a Function snippet into the code I posted in order to resize the Image without screwing up the pixels on a 'large' scale?

 

Currently I'm just limiting the picture within the border using height=x and width=x in the <img src> tag, which compresses the pixels quite a lot

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.