/    Sign up×
Community /Pin to ProfileBookmark

validating a multi-image upload

Hi All,
Im building a CMS where 3 images need to be uploaded. I’m trying to validate by checking for 2 things (at least for now). One, that it’s not empty and
Two, the image must be a .gif

3 images in an upload means a 2 dimensional array. I’ve accessed via 2 foreach loops but am trying to figure out the syntax isolate and check the individaul key => value pairs.

echoing the 2 foreach loops produces this…

Array
(
[add_acchdr_off] => Array (
[name] => accodion_header_off.gif
[type] => image/gif
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpGR9rW9
[error] => 0
[size] => 420681
)
[add_acchdr_on] => Array (
[name] => accodion_header_on.gif
[type] => image/png
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/php1B91mn
[error] => 0
[size] => 200434
)
[add_main_pic] => Array (
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)

i want to access the [error] and test if it’s 4 (means it’s empty), and also the [type] to see if it’s a .gif or not. how can i do that?

any help would be great, thanks!

my code so far is this,
$imgArr = array($add_acchdr_off, $add_acchdr_on, $add_main_pic);
foreach ($imgArr as $Img) {
foreach($Img as $key => $val) {
echo $key . ‘ ‘ . $val;
//put some val code here
}
}

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ondo915Jun 14.2012 — I know this doesn't answer your question, but do you have any links on how to go about uploading multiple images? I'm working on a 2 image uploader and I want it to upload each image in a separate locations. Help would be much appreciated!
Copy linkTweet thisAlerts:
@toptomatoauthorJun 15.2012 — Almost!, in fact i've answered my own question re: validating. Next i have to script how to move from temp to designated folder. I will post later if you want, let me know

First off, to understand the Super Global Files array, i would do this, it shows you everything

[code=php]if(isset($_POST['submit'])) {
echo '<pre>'.print_r($_FILES, true).'</pre>';
}[/code]


don't know if this is 100%, i've distilled from my project and cut out stuff not relevant to img uploads.

[code=php]function valImg($img, &$theErrArr) {
if($img['error'] == 4 ){
//echo 'Img is empty, please choose an image';
array_push($theErrArr, 'Img is empty, please choose an image before submitting');
}

if($img['type'] != 'image/gif') {
//echo '<p>Img type must a gif</p>';
array_push($theErrArr, 'Img type must a gif');
}
}

function display_errors($errArr) {
$count = count($errArr);
for($i=0;$i<$count;$i++) {
echo $errArr[$i] . '<br>';
}
}


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

//create image vars
$header_off = $_FILES['header_off'];
$header_on = $_FILES['header_on'];
$main_pic = $_FILES['main_pic'];

//I've chose to create array manually, but you could do with Super Globals //$_FILES[]
$imgArr = array($header_off, $header_on, $main_pic);

//the error array
$addErrArr = array();

foreach($imgArr as $indImg) {
valImg($indImg);
}

if(!empty($addErrArr) {
display_errors($addErrArr);
} else {
//Run Query, update tables, copy images to dir, etc. etc.
}

}
[/code]
×

Success!

Help @toptomato spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...