Google dot what?
Posts: 2
20 credits Members referred : 0
« on: Jul 25, 2009, 05:11:03 am »
The script I have worked before I put in the type/size restriction but when I insert the code to check for size and type it gives me the error that the size/type are incorrect and doesn't upload any of the files. Can anyone tell me where to put the loops for checking the size/type into the multiple upload script?
This is the code I was inserting to check the file type/size I want it to check each individual file for the type/size. This one works for a single image upload only.
Code:
<?php define ('MAX_FILE_SIZE', 2097152);
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; // create an array of permitted MIME types $permitted = array('images/gif','images/jpeg','images/pjpeg','images/png'); // begin by assuming the file is unacceptable $sizeOK = false; $typeOK = false; // check that file is within the permitted size if ($_FILES['images']['size'] > 0 && $_FILES['images']['size'] <= MAX_FILE_SIZE) { $sizeOK = true; } // check that file is of a permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['images']['type']) { $typeOK = true; break; } } if ($sizeOK && $typeOK) { $filename = $value; $now = date('Y-m-d-His'); $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line $query_rsCWGetCustomerData = "SELECT cst_Email FROM tbl_customers WHERE cst_ID = '" . $_SESSION["customerID"] . "'"; $rsCWGetCustomerData = $cartweaver->db->executeQuery($query_rsCWGetCustomerData, "rsCWGetCustomerData"); $row_rsCWGetCustomerData = $cartweaver->db->db_fetch_assoc($rsCWGetCustomerData); $username = $row_rsCWGetCustomerData['cst_Email']; $_SESSION['username']=$username; //if the subfolder doesnt exist yet, create it if (!is_dir(UPLOAD_DIR.$username)) { mkdir(UPLOAD_DIR.$username); } $add = 'upload_test/' . $username.'/'.$now.$filename; copy($_FILES['images']['tmp_name'][$key], $add); chmod("$add",0777); $_SESSION['filename']=$filename;
} else { $result = " cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, jpeg, png."; }
this is the where I put it but it doesn't work. I have tried it in various places above and below the while statement but all with the same result. This one works for multiple uploads as long as you leave out the size/type restrictions.
while(list($key,$value) = each($_FILES['images']['name'])) { $max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; // create an array of permitted MIME types $permitted = array('images/gif','images/jpeg','images/pjpeg','images/png'); // begin by assuming the file is unacceptable $sizeOK = false; $typeOK = false; // check that file is within the permitted size if ($_FILES['images']['size'] > 0 && $_FILES['images']['size'] <= MAX_FILE_SIZE) { $sizeOK = true;
} // check that file is of a permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['images']['type']) { $typeOK = true; break; } } if ($sizeOK && $typeOK) { if(!empty($value)) { $filename = $value; $now = date('Y-m-d-His'); $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line $query_rsCWGetCustomerData = "SELECT cst_Email FROM tbl_customers WHERE cst_ID = '" . $_SESSION["customerID"] . "'"; $rsCWGetCustomerData = $cartweaver->db->executeQuery($query_rsCWGetCustomerData, "rsCWGetCustomerData"); $row_rsCWGetCustomerData = $cartweaver->db->db_fetch_assoc($rsCWGetCustomerData); $username = $row_rsCWGetCustomerData['cst_Email']; $_SESSION['username']=$username; //if the subfolder doesnt exist yet, create it if (!is_dir(UPLOAD_DIR.$username)) { mkdir(UPLOAD_DIR.$username); } $add = 'upload_test/' . $username.'/'.$now.$filename; copy($_FILES['images']['tmp_name'][$key], $add); chmod("$add",0777); $_SESSION['filename']=$filename; } } else { $result = " cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, jpeg, png."; } } ?>
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #1 on: Jul 28, 2009, 12:30:07 am »
Inside the loop you should use $key and $value instead of $_FILES['images']
BTW you can replace this :
Code:
<?php foreach ($permitted as $type) { if ($type == $_FILES['images']['type']) { $typeOK = true; break; } }