Warning:Variable passed to each() is not an array or object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
microskies
Forum Newbie
Posts: 4
Joined: Sat Jun 25, 2011 1:21 pm

Warning:Variable passed to each() is not an array or object

Post by microskies »

This comes up on lines 22 36 and 61

These lines all are:

Code: Select all

while(list($key,$value) = each($_FILES[images][name]))
The full code:

Code: Select all

    <?php
    require("/phpmailer/class.phpmailer.php");

    //Variables Declaration
    $name = "ign";
    $rname = "rname";
    $age = "age";
    $rank = "rank";
    $time = "how_long";
    $email_subject = "$rank Application From $ign";
    $Email_msg ="A visitor submitted the following :\n";
    $Email_to = "microskies@hotmail.co.uk"; // the one that recieves the email
    $email_from = "email";
    $dir = "uploads/$filename";
    chmod("uploads",0777);
    $attachments = array();
    //
    checkType();
    //
    //------Check TYPE------\\
    function checkType() {
    while(list($key,$value) = each($_FILES[images][type])){
    strtolower($value);
    if($value != "image/jpeg" AND $value != "image/pjpeg" AND $value != "image/png" AND $value != "image/gif" AND $value != "image/bmp" AND $value != "") {
    exit('Sorry , current format is <b>'.($value).'</b> ,only .jpeg, .jpg, .gif, .png and .bmp file types are allowed.') ;
    }
    }
    //
    checkSize();
    //
    }
    //-------END OF Check TYPE--------\\
    //
    //---CheckSizeFunction ---\\
    function checkSize(){
    while(list($key,$value) = each($_FILES[images][size]))
    {
    $maxSize = 5000000;
    if(!empty($value)){
    if ($value > $maxSize) {
    echo"Sorry this is a very big file .. max file size is $maxSize Bytes = 5 MB";
    exit();
    }
    else {
    $result = "File size is ok !<br>";
    //
    }
    //
    }
    //
    }
    uploadFile();
    //
    }
    //-------END OF Check Size--------\\
    //
    //==============upload File Function============\\
    //
    function uploadFile() {
    global $attachments;
    while(list($key,$value) = each($_FILES[images][name]))
    {
    //
    if(!empty($value))
    {
    $filename = $value;
    //the Array will be used later to attach the files and then remove them from server ! array_push($attachments, $filename);
    $dir = "uploads/$filename";
    chmod("uploads",0777);
    $success = copy($_FILES[images][tmp_name][$key], $dir);
    }
    //
    }
    //
    if ($success) {
    echo " Files Uploaded Successfully<BR>";
    SendIt();
    //
    }else {
    exit("Sorry the server was unable to upload the files...");
    }
    //
    }
    //
    //==== PHP Mailer With Attachment Func ====\\
    //
    function SendIt() {
    //
    global $attachments,$name,$Email_to,$Email_msg,$email_subject,$email_from;
    //
    $mail = new PHPMailer();
    $mail->IsSMTP();// send via SMTP
    $mail->Host = "smtp.1and1.com"; // SMTP servers
    $mail->SMTPAuth = false; // turn on/off SMTP authentication
    $mail->From = $email_from;
    $mail->FromName = $name;
    $mail->AddAddress($Email_to);
    $mail->AddReplyTo($email_from);
    $mail->WordWrap = 50;// set word wrap
    //now Attach all files submitted
    foreach($attachments as $key => $value) { //loop the Attachments to be added ...
    $mail->AddAttachment("uploads"."/".$value);
    }
    $mail->Body = $Email_msg."Name : ".$name."\n";
    //
    $mail->IsHTML(false);// send as HTML
    $mail->Subject = $email_subject;
    if(!$mail->Send())
    {
    echo "Message was not sent <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
    }
    //
    echo "Message has been sent";
    // after mail is sent with attachments , delete the images on server ...
    foreach($attachments as $key => $value) {//remove the uploaded files ..
    unlink("uploads"."/".$value);
    }
    //
    }
    //
    ?> 
I know and can see that this is an easy problem, but im a php noob, and didn;lt even make this script myself, so go easy on me ;)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Warning:Variable passed to each() is not an array or obj

Post by McInfo »

The script expects to receive an array of uploaded files. You might have accessed the script directly, or the form you submitted was not compatible with the script. See POST method uploads, especially Example #3.

Also, the array keys should be quoted because they are literal strings, not constants. PHP will replace undefined constants with the name (and trigger a notice), but don't rely that feature.

Code: Select all

$_FILES[images][name] # No
$_FILES['images']['name'] # Yes
Post Reply