Page 1 of 2

unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 5:44 am
by timoteo
I have tried several variations of a file upload script but all give me an unexpected T_STRING error with $_FILES['userfile']['tmp_name']. Does anyone know why this might be?
2 examples that give me exactly the same problem

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
        <TITLE>Upload Files</TITLE>
</HEAD>

<BODY>
<H1>File Uploader</H1>
<HR>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="fupload.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" name="Submit" value="Send File" />
</form>

<?php
if(isset($_POST['Submit'])){
$nwfile = "files/". basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $nwfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";
}
?>
</BODY>
</HTML>
or from my own trial page:

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); 
mysql_select_db($database_recommendingpeople, $recommendingpeople);?>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(isset($_POST['upload']) && $_FILES['userfile']['size']>0)
 { 
 $fileName = $_FILES['userfile']['name'];
 $tmpName  = $_FILES['userfile']['tmp_name'];
 $fileSize = $_FILES['userfile']['size'];
 $fileType = $_FILES['userfile']['type'];

 $fp      = fopen($tmpName, 'r');
 $content = fread($fp, filesize($tmpName));
$content = addslashes($content);
 fclose($fp);

 if(!get_magic_quotes_gpc())
 {
     $fileName = addslashes($fileName);
 }

 include 'library/config.php';
 include 'library/opendb.php';
mysql_select_db($database_recommendingpeople, $recommendingpeople);
 $query = "INSERT INTO userphoto (name, size, type, content)".
 "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

 mysql_query($query) or die('Error, query failed'); 
 include 'library/closedb.php';

 echo "<br>File $fileName uploaded<br>";
 } 
else { echo "there is no post"; }
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form method="post" enctype="multipart/form-data">
 <table width="350" border="0" cellpadding="1"  cellspacing="1" class="box">
 <tr> 
 <td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
 <input name="userfile" type="file" id="userfile"> 
 </td>
 <td width="80"><input name="upload" type="submit"  class="box" id="upload" value=" Upload "></td>
 </tr>
 </table>
 </form>
</body>
</html>

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 7:49 am
by Weirdan
Post the error message in its entirety. It seems you're overlooking something and the error message may shed some light on it.

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 10:04 am
by timoteo
Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/ya.php on line 9
thankyou for taking a look at this

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 10:29 am
by Weirdan
The page you posted here posting.php?mode=reply&f=1&t=127508#pr644651 as 'your own trial page' is syntactically correct (for PHP5 anyway).

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 10:35 am
by timoteo
yes that is what I have been told. I can't see anything wrong with it but that same page gives the error I posted. What could it be? (spooky music...)

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 11:48 am
by Weirdan
What happens if you copy the code from your post here, paste it into new file and try to run that? What is your PHP version?

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 12:05 pm
by timoteo
I'm using php 5.2.11 on a remote server. This code that I pasted is the entire page - I already took out the rest of the html of my page to leave this bare bones problem code. This is the error I got with this code as I posted stand alone. If I paste the code as is I get the error. Both times the error points to where I am using 'tmp_name'. That's what leads me to think that it maybe something to do with my file system or paths if there is nothing wrong with my php - hence the thread title. But I don't know how it should be - right now it is default. Or maybe I am completely wrong and it is something else - I don't want to mess up my files unnecessarily. But if the page couldn't find 'tmp_name' file it would show a file not found error rather than a syntax error. However what I don't get is that it is all in an if {} clause, so if there is no post it should read the else {} which works fine - If I delete all the file stuff the page displays fine. Any ideas?

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 12:18 pm
by s992
What do you get if you print_r($_FILES)?

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 12:25 pm
by timoteo
Array()

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 2:09 pm
by s992
After uploading the file, it shows an empty array?

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 2:15 pm
by timoteo
I can not get that far as I can not display the upload page due to the error.
Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/ya.php on line 9

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 2:16 pm
by timoteo
line 9 is

Code: Select all

 $tmpName  = $_FILES['userfile']['tmp_name'];
in my 'trial' page quoted at the top of this thread

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 2:18 pm
by s992
Put the print_r($_FILES) directly above $fileName = $_FILES['userfile']['name'];

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Tue Feb 08, 2011 2:59 pm
by timoteo
Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/ya.php on line 11
this is the code:

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); 
mysql_select_db($database_recommendingpeople, $recommendingpeople);?>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(isset($_POST['upload']) && $_FILES['userfile']['size']>0)

 { 
  print_r($_FILES);
 $fileName = $_FILES['userfile']['name'];
 $tmpName  = $_FILES['userfile']['tmp_name'];
 $fileSize = $_FILES['userfile']['size'];
 $fileType = $_FILES['userfile']['type'];

 $fp      = fopen($tmpName, 'r');
 $content = fread($fp, filesize($tmpName));
$content = addslashes($content);
 fclose($fp);

 if(!get_magic_quotes_gpc())
 {
     $fileName = addslashes($fileName);
 }

 include 'library/config.php';
 include 'library/opendb.php';
mysql_select_db($database_recommendingpeople, $recommendingpeople);
 $query = "INSERT INTO userphoto (name, size, type, content)".
 "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

 mysql_query($query) or die('Error, query failed'); 
 include 'library/closedb.php';

 echo "<br>File $fileName uploaded<br>";
 } 
else { echo "there is no post"; }
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form method="post" enctype="multipart/form-data">
 <table width="350" border="0" cellpadding="1"  cellspacing="1" class="box">
 <tr> 
 <td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
 <input name="userfile" type="file" id="userfile"> 
 </td>
 <td width="80"><input name="upload" type="submit"  class="box" id="upload" value=" Upload "></td>
 </tr>
 </table>
 </form>
</body>
</html>
thankyou for your help

Re: unexpected T_STRING $_FILES['userfile']['tmp_nam

Posted: Wed Feb 09, 2011 8:24 am
by timoteo
back to matters in hand: this seems to be the source of all problems:

Code: Select all

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size']>0){
print_r($_FILES['userfile']);
  $fileName = $_FILES['userfile']['name'];
  echo  $fileName;

}
?>
gives
Parse error: syntax error, unexpected T_VARIABLE in /home/biomagn1/public_html/recommendingpeople.com/Untitled-2.php on line 4
I have now figured out tmp_name, name etc are populated and fine.:
Array ( [name] => LOGO5.GIF [type] => image/gif [tmp_name] => /tmp/php1fXM4c [error] => 0 [size] => 47776 )