29, May 2012

Multiple image upload file size/type checking - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Multiple image upload file size/type checking
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Multiple image upload file size/type checking  (Read 2387 times)
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/10241).'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'] > && $_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.
Code:
<?php

define('UPLOAD_DIR''upload_test/');
define ('MAX_FILE_SIZE'2097152);


while(list($key,$value) = each($_FILES['images']['name']))
{
$max number_format(MAX_FILE_SIZE/10241).'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'] > && $_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: Male
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;
}
}

with this:

Code:
<?php
$typeOK 
in_array($_FILES['images']['type'], $permitted);


Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=8975
Tags : php Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Multiple image upload file size/type checking
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 29, 2012, 01:10:03 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.