警告:在PHP中为foreach()提供了无效的参数 [英] Warning: Invalid argument supplied for foreach() in php
问题描述
<?php $ b我是新来的PHP,我试着在网上查找东西,并写下了这段代码。 $ b函数mail_attachment($ files,$ path,$ mailto,$ subject,$ message){
$ uid = md5(uniqid(time()));
$ header =MIME-Version:1.0 \r\\\
;
$ header。=Content-Type:multipart / mixed; boundary = \。$ uid。\\r\\\
\r\\\
;
$ header。=这是一个MIME格式的多部分消息。\ r \ n;
$ header。=Content-type:text / plain; charset = iso-8859-1 \r\\\
;
$ header。=Content-Transfer-Encoding:7bit \\\\\\\\\;
$ header。= $ message。\r\\\
\r\\\
;
foreach($ files为$ filename){
$ file = $ path。$ filename;
$ name = basename($ file);
$ file_size = filesize($ file);
$ handle = fopen($ file,r);
$ content = fread($ handle,$ file_size);
fclose($ handle);
$ content = chunk_split(base64_encode($ content));
$ header。= - 。$ uid。\r\\\
;
$ header。=Content-Type:application / octet-stream; name = \。$ filename。\\r\\\
; //在这里使用不同的内容类型
$ header。=Content-Transfer-Encoding:base64 \r\\\
;
$ header。=Content-Disposition:attachment; filename = \。$ filename。\\r\\\
\r\\\
;
$ header。= $ content。\r\\\
\r\\\
;
}
$ header。= - 。$ uid。 - ;
if(@mail($ mailto,$ subject,$ message,$ header)){
echomail send ... OK;
} else {
echomail send ... ERROR!;
$ b //在这里输入你的电子邮件地址
$ mailto =abc@mail.com;
$ subject =Form Details;
$名字= $ _POST ['firstName'];
$ LastName = $ _POST ['lastName'];
$ EmailAdress = $ _POST ['emailAddress'];
$ ContactNumber = $ _POST ['contactNumber'];
$ ApartmentType = $ _POST ['apartmentType'];
$ ApartmentLocation = $ _POST ['apartmentLocation'];
$ CheckIn = $ _POST ['checkIn'];
$ CheckOut = $ _POST ['checkOut'];
$ NumberOfAdults = $ _POST ['numberOfAdults'];
$ NumberOfChildren = $ _POST ['numberOfChildren'];
$ TermsAndConditions = $ _ POST ['termsAndConditions'];
$ b $ required = array('firstName','lastName','emailAddress','contactNumber','apartmentType','apartmentLocation','checkIn','checkOut','numberOfAdults','儿童人数');
$ error = false;
$ b $ foreach($ required as $ field){
if(empty($ _ POST [$ field])){
$ error = true;
if($ error){
echo所有字段都是必需的。
出口;
}
if($ TermsAndConditions =='disagree'){
echo请同意条款和条件;
出口;
}
$ message =名字:\ t $名字\\\
\\\
。
姓氏:\ t $ LastName\\\
\\\
。
电子邮件地址:\ t $ EmailAddress \\\
\\\
。
联络号码:\ $ $联络号码\ n \ n。
公寓类型:\ $ $ ApartmentType \\\
\\\
。
公寓位置:\ $ $ ApartmentLocation \\\
\\\
。
签入:\ $ $ CheckIn \\\
\\\
。
签出:\ $ CheckOut \\\
\\\
。
成人人数:\ $ $ NumberOfAdults \\\
\\\
。
子女人数:\ t $ NumberOfChildren \\\
\\\
;
$ uploaddir ='./';
$ x = 0;
foreach($ _FILES [documents] [error] as $ key => $ error)
{
if($ error == UPLOAD_ERR_OK){
$ tmp_name = $ _FILES [documents] [tmp_name] [$ key];
$ name = basename($ _ FILES [pictures] [name] [$ key]);
$文件[$ x] = $ name;
$ x ++;
move_uploaded_file($ tmp_name,$ uploaddir。$ name);
}
}
$ path = $ _SERVER ['DOCUMENT_ROOT'];
mail_attachment($ files,$ path,$ mailto,$ subject,$ message);
?>
这两个foreach都出错:
警告:为88行上的/home/a4824849/public_html/PhpFile.php中的foreach()提供的无效参数
警告:为foreach提供的无效参数()in /home/a4824849/public_html/PhpFile.php on line 12
有可能是错的?
varname
。无效的参数仅仅意味着你没有提供一个有效的数组。 对于l.12,这只是表示你没有把数组作为第一个arg传递给你函数,这是由于你的错误行88.
我会假设你的问题是你还没有尝试上传文件,因此 $ _FILES
未定义。根据我的经验,使用 $ _ FILES
超全局变量,您通常需要通过 enctype multi-part / formdata
。在 foreach之前调试并检查是否定义了
循环。作为一个额外的安全措施,你可能需要在 var_dump或print_r
if(isset($ _ FILES))
希望这有助于
I am working to upload a file and then send it to myself as an attachment. I am new with php, I tried lookign up on the web for stuff and wrote this code.
<?php
function mail_attachment($files, $path, $mailto, $subject, $message) {
$uid = md5(uniqid(time()));
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
foreach ($files as $filename) {
$file = $path.$filename;
$name = basename($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
}
$header .= "--".$uid."--";
if (@mail($mailto, $subject, $message, $header)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR!";
}
}
//Enter your email address here
$mailto="abc@mail.com";
$subject="Form Details";
$FirstName = $_POST['firstName'] ;
$LastName = $_POST['lastName'] ;
$EmailAdress= $_POST['emailAddress'] ;
$ContactNumber= $_POST['contactNumber'] ;
$ApartmentType= $_POST['apartmentType'] ;
$ApartmentLocation = $_POST['apartmentLocation'] ;
$CheckIn = $_POST['checkIn'] ;
$CheckOut = $_POST['checkOut'] ;
$NumberOfAdults = $_POST['numberOfAdults'] ;
$NumberOfChildren = $_POST['numberOfChildren'] ;
$TermsAndConditions =$_POST['termsAndConditions'];
$required = array('firstName','lastName','emailAddress','contactNumber','apartmentType','apartmentLocation','checkIn','checkOut','numberOfAdults','numberOfChildren');
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
exit;
}
if ( $TermsAndConditions=='disagree') {
echo "Please agree to the terms and conditions";
exit;
}
$message="First Name:\t $FirstName \n\n" .
"Last Name:\t $LastName\n\n".
"Email Address:\t $EmailAddress \n\n".
"Contact Number:\t $ContactNumber \n\n".
"Apartment Types:\t $ApartmentType \n\n".
"Apartment Location:\t $ApartmentLocation \n\n" .
"Check in:\t $CheckIn \n\n" .
"Check out:\t $CheckOut \n\n" .
"Number of Adults:\t $NumberOfAdults \n\n" .
"Number of Children:\t $NumberOfChildren \n\n";
$uploaddir = './';
$x=0;
foreach ($_FILES["documents"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["documents"]["tmp_name"][$key];
$name = basename($_FILES["pictures"]["name"][$key]);
$files[$x]=$name;
$x++;
move_uploaded_file($tmp_name, $uploaddir.$name);
}
}
$path = $_SERVER['DOCUMENT_ROOT'];
mail_attachment($files, $path, $mailto, $subject, $message);
?>
I get an error for both the foreach:
Warning: Invalid argument supplied for foreach() in /home/a4824849/public_html/PhpFile.php on line 88
Warning: Invalid argument supplied for foreach() in /home/a4824849/public_html/PhpFile.php on line 12
What could possibly be wrong?
Foreach
takes arrays and attributes each value of the array to the varname
that you define. Invalid Argument simply means that the you did not provide a valid array.
For l.12, it simply means that you did not pass an array as the first arg to your function, which is due to your error line 88.
I would assume that your problem is that you haven't yet tried uploading a file and thus $_FILES
is undefined. In my experience to use the $_FILES
superglobal variable you usually have to upload a file via a form of enctype multi-part/formdata
. To debug and check if it's defined do a var_dump or print_r
on $_FILES
before your foreach
loop. As an added measure of security you might want to wrap said loop and your call to your mail_attachment function in an if (isset($_FILES))
Hope this helps
这篇关于警告:在PHP中为foreach()提供了无效的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!