解析错误,期待“T_STRING”或“T_VARIABLE”或“T_NUM_STRING”

时间:2009-03-12 16:42:07

标签: php parsing

我需要一双清新的眼睛。我从那个说它有效的人那里得到了这段代码。

这是错误:

PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Inetpub\wwwroot\2am\employment\send-email-form.php on line 19 

第19行是GetUploadedFileInfo()的第一行。我也得到第20行和第21行的错误。它显然与$file_info数组有关,但这里有什么不对吗?

<?php 
$max_allowed_file_size = "100"; // this is size in KB 
list($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file) = GetUploadedFileInfo();
if(!Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size)) {
   exit();
}
LoadUploadedFile($name_of_uploaded_file);
$path_of_uploaded_file = "uploads/" . $name_of_uploaded_file;

   include_once('Mail.php');
   include_once('mime.php');

ComposeMail($path_of_uploaded_file);


//////////////////// Functions ////////////////////////

function GetUploadedFileInfo() {
   $file_info[] = basename($_FILES['uploaded_file']['name']);
   $file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1);
   $file_info[] = $_FILES["uploaded_file"]["size"]/1024;
   return $file_info;
}


function Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size) {
   if($size_of_uploaded_file>$max_allowed_file_size ) {
       echo "Size of file is greater than" . $max_allowed_file_size . " KB. <a href='attachment_email_form.html'>Click Here to upload a smaller sized file.</a>";
      return false;
   }
$allowed_extension = array("jpg", "jpeg", "gif", "bmp");
for($i=0; $i<sizeof($allowed_extension); $i++) { 
   $allowed_extension[$i] = strtoupper($allowed_extension[$i]); 
}
$type_of_uploaded_file = strtoupper($type_of_uploaded_file);
if(!(in_array(strtoupper($type_of_uploaded_file),$allowed_extension))) {
   echo "You have uploaded a file with an extension of " . $type_of_uploaded_file . " . This type is not allowed. Please upload a file with allowed image extensions like jpg, jpeg, bmp, gif. <a href='attachment_email_form.html'>Click Here to upload a file with allowed extension.</a>";
   return false;
}
   return true;
}


function LoadUploadedFile($name_of_uploaded_file) {
   move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "uploads/" . $name_of_uploaded_file);
   return true;
}


function ComposeMail($name_of_uploaded_file) {
   $name = $_POST['name'];
   $user_message = $_POST['message'];
   $to = "RecipietnEmail@hotmail.com";
   $subject="An email with attachement is sent";
   $from = "SenderEmail@hotmail.com";
   $text = "A user " . $name . "has sent you this message and an attachment: " . $user_message;
   $message = new Mail_mime(); 
   $message->setTXTBody($text); 
   $message->addAttachment($name_of_uploaded_file);
   $body = $message->get();
   $extraheaders = array("From"=>$from, "Subject"=>$subject);
   $headers = $message->headers($extraheaders);
   $mail = Mail::factory("mail");
   $mail->send($to, $headers, $body);
   echo "Your Email with attachment was sent.";
}
?>

6 个答案:

答案 0 :(得分:4)

叫我疯了,但是我尝试了这个并没有得到解析错误 - 你确定你正在查看正确的文件(或正确的版本)吗?

编辑:那么我唯一能想到的就是单引号正在关闭一个在其他地方保持打开的字符串。

答案 1 :(得分:2)

$ file_info 不是函数,它是变量(数组)。

您发布的代码没有任何错误。我将其复制/粘贴到php文件并运行lint: php -l <​​/ strong>并报告:在test.php中未检测到语法错误

答案 2 :(得分:0)

错误消息看起来好像是$file_info[]中的索引。您使用的是古老版本的PHP,它无法识别“[]”语法是否附加到数组的末尾?

答案 3 :(得分:0)

您收到的错误表明括号或引号不匹配,但语法看起来很好。我可以毫无错误地运行该功能:


$ php -a
Interactive shell

php > function GetUploadedFileInfo() {
php {    $file_info[] = basename($_FILES['uploaded_file']['name']);
php {    $file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1);
php {    $file_info[] = $_FILES["uploaded_file"]["size"]/1024;
php {    return $file_info;
php { }
php > print_r(GetUploadedFileInfo());
Array
(
    [0] => 
    [1] => 
    [2] => 0
)

@PhiLho php允许您动态创建数组变量。但是,由于未正确初始化,因此在变量在其他位置初始化的情况下可能会出现意外行为。但由于变量是在函数内部隔离而不是全局访问的,因此在这种情况下它很好。


$ php -a
Interactive shell

php > $a = array();
php > $a[] = 'Hello, world!';
php > print_r($a);
Array
(
    [0] => Hello, world!
)
php > $b[] = 'Hello, SO!';
php > print_r($b);
Array
(
    [0] => Hello, SO!
)

此外,您运行的是哪个版本的PHP?我的例子使用PHP 5.2.8-pl2-gentoo。

答案 4 :(得分:0)

浏览器是否直接请求send-email-form.php,还是访问其他包含或要求send-email-form.php的其他PHP文件?

如果是另一个脚本包含它的情况,请检查在此文件之前包含的任何PHP代码中或主文件中的任何内联PHP代码中没有不匹配的字符(parens,quotes,braces等)。

答案 5 :(得分:0)

我找到了问题。

不确定这是完成了什么,但我将第19行推到第20行,在18行函数GetUploadedFileInfo(){

之后留下一个空行