tags:

views:

220

answers:

6

I need a fresh set of eyes on this. I got this code from someone who said it worked.

Here is the error:

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

Line 19 is the first line of GetUploadedFileInfo(). I get the error for line 20 and 21 also. Its obviously with the $file_info array but whats wrong here?

Thanks in advance.

<?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 = "[email protected]";
   $subject="An email with attachement is sent";
   $from = "[email protected]";
   $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.";
}
?>
+4  A: 

Call me crazy, but I tried this and didn't get a parse error - are you sure you're looking at the right file (or the right version of it)?

Edit: then the only thing I can think of is the single quotes are closing a string that's been left open somewhere else.

Greg
I just tried it as well, and I'm not getting a parse error.
St. John Johnson
yup, i've tried it a hundred times. still to no avail.
+2  A: 

$file_info is not a function, it's a variable (array).

The code you posted does not have any errors. I copy/pasted it to a php file and run lint: php -l and it reports: No syntax errors detected in test.php

Milan Babuškov
your right, was caught up in something else, def. not working for me. I could give you the html part to this but it will not work at all for me.
A: 

The error message looks like it is expecting an index in $file_info[]. Are you using an ancient version of PHP which doesn't recognize the "[]" syntax for appending to the end of an array?

Kip
I'm using the newest version of PHP.
could you comment out lines 19-21, and in their place put the following code and see if you still get any syntax error? $file_info[] = "hello"; $file_info[] = "world"; $file_info[] = "!";
Kip
just tried that and I'm still getting the same error.
A: 

The error you are receiving suggests a mismatched parenthesis or quotes, however the syntax looks fine. I am able to run that function without error:


$ 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 does allow you to dynamically create array variables. However since it is not being properly initialized, you may get unexpected behavior in cases where the variable was initialized elsewhere. But since the variable is isolated inside a function and not globally accessed, it is fine in this case.


$ 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!
)

Also, what version of PHP are you running? My examples use PHP 5.2.8-pl2-gentoo.

adam
newest version of php. I Installed it a couple weeks ago and so far it has been flawless. do I possibly have the uploads folder path wrong? would it matter if that var was not present in the folder?
One stupid trick I use is just a simple echo "hello world" at the top of the script or function call to verify that I'm debugging the right spot.
adam
A: 

Is the browser directly requesting send-email-form.php, or is it accessing some other PHP file which includes or requires send-email-form.php?

If it is the case that another script includes it, check that there are no unmatched characters (parens, quotes, braces, etc) in any PHP code included before this file, or in any inline PHP code in the main file.

Kip
no. The url www.2amgroup.com/employment/attachment_email_form.html is a test for a much MUCH larger form i have. but it uses the same php file with the addition of $body vars to include other info. check it for yourself. only 1 php file being called.
A: 

Well I figured out the problem.

Not sure what this accomplished but I pushed line 19 down to line 20 leaving a blank line after the 18 line of function GetUploadedFileInfo() {