PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a simple upload script installed in a website. The code checks file type and size before uploading the file to the appropriate directory. Problem I'm having is the script works perfectly well when I test it but returns an error when a client uses it even though their file meets to the requirements. The client emailed me the file that was rejected and I uploaded the same file through the same script without it being rejected. Code is:
<?php
$target_path = "../images/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$limit_size=5000000;
$file_size = $_FILES['uploadedfile']['size'];
if (
(
($_FILES['uploadedfile']['type'] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
)
&& (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
&& ($file_size <= $limit_size)
)
{
echo "<p>The file ". basename( $_FILES['uploadedfile']['name']).
" has been successfully uploaded.</p>";
}
else{
echo "<p><b>There was an error uploading the file,</b> please try again! Make sure your file is under 5 Mbs and is in one of the following formats: jpeg, gif or png.</p>";
}
?>
Why does this work from some computers and not others? Please help. Thanks.
The problem is that you are checking the type browser told you the file is, and it can be different from browser to browser (check your code with IE, Firefox and Opera). Your browser reports 'image/jpeg', but client's browser reports something different.
If you are uploading images, the proper way to check the type is the getimagesize() function