I've been battling with a script to upload photos for user comments. Needless to say, I'm new to PHP. I have it so it confirms the file is an image and then continues to check its size. It's all working except when the image exceeds the size I've set. It does absolutely nothing. Here's my code:
ini_set("display_errors",1);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
$date = date('m-d-Y_h.i.s_a', time());
$target_path = "/home/SOME_USER/WEB/SOME_SITE/comment/uploading/uploads/";
$filename = $date. "_" .basename( $_FILES['uploadedfile']['name']);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$test = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
$newName = bin2hex($test);
$target_path = $target_path."". "$newName.".$ext;
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
$mime_type = mime_content_type($_FILES['uploadedfile']['tmp_name']);
$allowed_file_types = ['image/png', 'image/jpeg', 'image/gif'];
if (! in_array($mime_type, $allowed_file_types)) {
$url = "./index.php" . "?&message=Not an image";
header('Location: '.$url);
} else {
echo ('made it to this part ?');
return proceed();
}
}
function proceed() {
global $target_path;
global $newName;
global $ext;
echo 'made it here. . .';
if(isset($_FILES['uploadedfile']['name'])) {
if($_FILES['uploadedfile']['size'] > 3145728) { //3mb)
echo ('made it here #1');
$url = "./index.php" . "?&message=Too big !";
header('Location: '.$url);
} else {
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
$url = "./index.php" . "?&message=".$newName.".".$ext."";
header('Location: '.$url);
} else{
echo('Made it here');
$url = "./index.php" . "?&message=Too big !";
header('Location: '.$url);
}
}
}
}
I know the code is probably pretty ugly and could be written a lot better, and I'll work on that when I get it working properly, but I'm stuck here. Again, everything is working except when the image is too large and it does absolutely nothing on the upload script. I've added some mile markers for debugging like I would in Python but it doesn't seem to help in PHP.
Solved: In my form
I had the file size limited to the same exact size as in my handler script. Changing the value in the form
to just one byte larger fixed it.