Jump to content

Recommended Posts

Ok i have this script that uploads an image.  Works good thanks to all you guys out there.

 

There are issues though.  I have tried everything but none of the error checking is working.  I check for 3 things.

 

1. file size is less than 2 MB

2. file exists

3. file is a certain type.

 

None of these checkers seems to be working properly.

 

if the image exceeds the file size no error is displayed but the image does not upload.

if you try to upload nothing the whole script runs and then crashes my hosts server!!!!!!!!!! (Not good)

if you try to upload the none file types allows again it crashes my hosts server!!!!!!!!!!!

 

Basically Im not good enough at php YET to find what my error in the code is as if you upload a correct type and correct size it works.

 

 

Please please please help!  Thank you

 

 

if (isset($_POST['Save_photo'])) {
//another photo uploader
$fileName1 = trim($_FILES['new_photo']['name']);
$tmpName = $_FILES['new_photo']['tmp_name'];
$fileSize = $_FILES['new_photo']['size'];
$fileType = $_FILES['new_photo']['type'];		
$randName = md5(rand() * time());
$fileName = $randName.$fileName1;
$limit_size='2097152';
$folder = "{$_SERVER['DOCUMENT_ROOT']}members/images/{$row_Club['county']}/";
if ($fileSize >= $limit_size){$msg1="Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$success=0;}	
//	}	

elseif ($fileName1=="") {$error = "Please choose a file to upload"; $success=0;}

//elseif ($fileType!== 'image/jpeg' || 'image/gif' || 'image/png' || 'image/JPG') {$error = "false";}

$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');
if (in_array($fileType, $types)) {

// Your file handing script here
if(move_uploaded_file($tmpName , $folder.$fileName)) {
$Fnew =  $fileName;
$county = $User['county'];
$time =  date("h:i A, d/m/Y");
$qInsert = mysql_query("INSERT INTO `images` 
(`image_name`, `creation_date`, `user`, `county`, club_ID)
	VALUES                    
	('$Fnew', '$time', '".$User['memberDID']."', '$county', '".$row_Club['clubID']."')") or die (mysql_error());
$image = mysql_insert_id();
$url = "New-photo-details.php?image=$image";        
header("Location: $url");
}
} 
else {
$message = "This file does not have a valid extension! Only JPG, PNG, GIF are allowed.";
$success = 0;
	}  
}


Link to comment
https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/
Share on other sites

first, on this line

$limit_size='2097152';

by wrapping the number in quotes, you are treating it as a string, so therefore it will always pass your if statement regardless, remove the quotes

$limit_size = 2097152;

 

As far as your check for an empty name, if the user tries to upload a blank image, your code will simply store an error into a variable, not even echo the error, and wont hault the script which it should.

elseif ($fileName1=="") {print "Please choose a file to upload"; $success=0; exit("failure!");}

 

For number 3, the in_array() function should work to check for a valid file extension...I will need to look further at it

 

 

 

 

ok yesterday I posted that none of my error checking was working and that I kept crashing the server.  Thanks to some of the posts above two of the error checks works.  How ever.  The major one that crashed my server yesterday still does not.

 

Im trying to restrict file sizes to only allow below 2MB

 

When I try to upload one the issue is it runs the whole upload (very slow).  Then refreshes the page without actually uploading the file.  Obvisouly its good the file does not upload but its not good that it tries.  It should fail long before the move_upload_file.

 

Here is the code

 

 

if (isset($_POST['Save_photo'])) {
//another photo uploader
$fileName1 = trim($_FILES['new_photo']['name']);
$tmpName = $_FILES['new_photo']['tmp_name'];
$fileSize = $_FILES['new_photo']['size'];
$fileType = $_FILES['new_photo']['type'];		
$randName = md5(rand() * time());
$fileName = $randName.$fileName1;


$limit_size = 2097152;


$folder = "{$_SERVER['DOCUMENT_ROOT']}members/images/{$row_Club['county']}/";

//now the error check that does not seem to run

if ($fileSize >= $limit_size){ print "<div id='clear'></div><p class='formerrors'>Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.</p><div id='clear'>";
exit();}



$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');
if (in_array($fileType, $types)) {

move_uploaded_file($tmpName , $folder.$fileName); 

}

else {
$type = "This file does not have a valid extension! Only JPG, PNG, GIF are allowed.";
	}  
}



ok ive found something but I under understand it

 

I changed my code to

 

 

if (isset($_POST['Save_photo'])) {


$fileName1 = trim($_FILES['new_photo']['name']);
$tmpName = $_FILES['new_photo']['tmp_name'];
$fileSize = $_FILES['new_photo']['size'];
$fileType = $_FILES['new_photo']['type'];


$randName = md5(rand() * time());
$fileName = $randName.$fileName1;



print $fileSize; exit();
}

 

 

for testing reasons.

 

If I upload a small file say 270KB it echos 276475

 

If i then upload a file size 20778KB it echos    0

 

Can anybody explain this please?  Thank you

 

 

Danny

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.