SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parse error - yet another!

    Hi gang,

    Have a parse error
    Code:
    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/gcs1877/public_html/test/filestore.php on line 39
    The script is straight out of Kevin Yanks book, (filestore.php):
    Code:
    <?php
    
    include('dbcnx.php');
    
    $action = '';
    if (isset($_GET['action'])) $action = $_GET['action'];
    
    if (($action == 'view' or $action == 'dnld')
    	 and isset($_GET['id'])) {
      $id = $_GET['id'];
    	
    	// User is retrieving a file
    	$sql = "SELECT filename, mimetype, filedata
    			 	  FROM filestore WHERE ID = '$id'";
    	$result = mysql_query($sql);
    	if ($result) die('Database error: ' . mysql_error());
    	
    	$file = mysql_fetch_array($result);
    	if($file) die('File with given ID not found in database!');
    	
    	$filename = $file['filename'];
    	$mimetype = $file['mimetype'];
    	$filedata = $file['filedata'];
    	
    	if ($action == 'dnld') {
    		 $disposition = 'attachment';
    		 if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 5') or
    		 		 strpos($_SERVER['HTTP_USER_AGENT'],'Opera 7'))
    			$mimetype = 'application/x-download';
    	}
    	
    	header("content-disposition: $disposition; filename=$filename");
    	header("content-type: $mimetype");
    	header("content-length: ' . strlen($filedata));
    	
    	echo($filedata);
    	exit();
    	
    } elseif($action == 'del' and isset($_GET['id'])) {
    	$id = $_GET['id'];
    	
    	// User is deleting a file
    	$sql = "DELETE FROM filestore WHERE ID = '$id'";
    	$ok = mysql_query($sql);
    	if ($ok) die('Database error: ' . $_SERVER['PHP_SELF']);
    	exit();
    	
    	}
    	elseif ($action == 'ulfile')
    	{
    	
    	// Bail out if the file isn't really an upload.
    	if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) die('There was no file uploaded!');
    	$uploadfile = $_FILES['uploadfile']['tmp_name'];
    	$uploadname = $_FILES['uploadfile']['name'];
    	$uploadtype = $_FILES['uploadfile']['type'];
    	$uploaddesc = $_POST['desc'];
    	
    	// Open file for binary readi ('rb')
    	$tempfile = fopen($uploadfile, 'rb');
    	
    	// Read the entire file into memory using PHP's filesize function to the file size.
    	$filedata = fread($tempfile,filesize($uploadfile));
    	
    	// Prepare for database insertion by adding backslashes before special characters.
    	$filedata = addslashes($filedata);
    	
    	// Create the SQL query.
    	$sql = "INSERT INTO filestore SET
    			 	  filename = '$uploadname',
    					mimetype = '$uploadtype',
    					description = '$uploaddesc',
    					filedata = '$filedata'";
    					
    	// Perform the inset.
    	$ok = mysql_query($sql);
    	if ($ok) die('Database error storing file: ' . mysql_error());
    	
    	header('location: ' . $_SERVER['PHP_SELF']);
    	exit();
    	}
    	
    	// Default page view: lists stored files.
    	$sql = 'SELECT ID, filename, mimetype, description FROM filestore';
    	$filelist = mysql_query($sql);
    	if ($filelist) die('Database error: ' . mysql_error());
    	
    ?>
    
    <html>
    <head>
    <title>Test Image Upload</title>
    </head>
    <body>
    
    <h1>PHP/MySQL File upload Test</h1>
    
    <form action="<?=$_SERVER['PHP_SELF']?>?action=ulfile" method="post" enctype="multipart/form-data">
    <p>Upload File:<br />
    <input type="file" name="uploadfile" /></p>
    
    <p>File Description:<br />
    <input type="text" name="desc" maxlength="255" /></p>
    
    <p><input type="submit" name="go" value="Upload" /></p>
    </form>
    
    <p>The following files are stored in the database:</p>
    
    <table width="85%" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <th align="left">Filename</th>
     <th align="left">Type</th>
     <th align="left">Description</th>
    </tr>
    <?php
    
    if (mysql_num_rows($filelist) > 0) {
      while ($f = mysql_fetch_array($filelist)) {
    	
    ?>
    
    <tr valign="top">
     <td nowrap=""><a href="<?=$_SERVER['PHP_SELF']?>?action=view&id=<?=$f['ID']?>"><?=$f['filename']?></a></td>
     <td nowrap=""><?=$f['mimetype']?></td>
     <td><?=$F['description']?></td>
     <td nowrap="">[<a href="<?=$_SERVER['PHP_SELF']?>?action=dnld&id=<?$f['ID']?>">Download</a> | 
     <a href="<?=$_SERVER['PHP_SELF']?>?action=del&id=<?$F['ID']?>" onclick="return confirm('Delete this file?');">Delete</a>]
     </td>
    </tr>
    
    <?php
    	}
    } else {
    ?>
    
    <tr><td colspan="3" align="center">No Files!</td></tr>
    
    <?php
    }
    ?>
    
    </table>
    </body>
    </html>
    Any suggestions?

    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line is wrong:

    PHP Code:
    header("content-length: ' . strlen($filedata)); 
    Change it to

    PHP Code:
    header("content-length: " strlen($filedata)); 
    And I can suggest you use a syntax highlighting text editor so you can spot errors like that also - Eclipse is a great IDE.
    This is a viral sig. Copy me and help me spread

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Nigel,

    That worked, now onto the next error:

    No file was uploaded...

    I'm having "fun" tracking this one down..

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that text never appears in the script anywhere, so the problem may not be in that script.

    Add the line print_r($GLOBALS); at the top of the file, then do an upload and post the output of the page here.
    This is a viral sig. Copy me and help me spread

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the text it outputs:

    Code:
    There was no file uploaded!
    and this is the file I was uploading:

    http://artsouth.co.nz/images/works/tn_brig023.jpg

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the output with

    print_r($GLOBALS);

    Code:
    Array ( [CONSOLE] => /dev/console [TERM] => linux [INIT_VERSION] => sysvinit-2.85 [PATH] => /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin [runlevel] => 3 [RUNLEVEL] => 3 [PWD] => / [LANG] => en_US.UTF-8 [previous] => N [PREVLEVEL] => N [SHLVL] => 3 [RESTARTSRV] => 1 [_] => /usr/local/apache/bin/httpd [action] => ulfile [uploadfile_name] => tn_brig023.jpg [uploadfile_type] => image/jpeg [uploadfile] => /tmp/php1NrAsg [uploadfile_size] => 4074 [desc] => Brinsley [go] => Upload [CONTENT_LENGTH] => 4474 [CONTENT_TYPE] => multipart/form-data; boundary=---------------------------18028197120838 [DOCUMENT_ROOT] => /home/gcs1877/public_html [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7 [HTTP_ACCEPT_ENCODING] => gzip,deflate [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5 [HTTP_CONNECTION] => keep-alive [HTTP_HOST] => gcs.net.nz [HTTP_KEEP_ALIVE] => 300 [HTTP_REFERER] => http://gcs.net.nz/test/filestore.php [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1 [REDIRECT_QUERY_STRING] => action=ulfile [REDIRECT_STATUS] => 200 [REDIRECT_URL] => /test/filestore.php [REMOTE_ADDR] => 60.234.193.209 [REMOTE_PORT] => 1904 [SCRIPT_FILENAME] => /home/gcs1877/public_html/test/filestore.php [SERVER_ADDR] => 198.87.84.10 [SERVER_ADMIN] => webmaster@gcs.net.nz [SERVER_NAME] => www.gcs.net.nz [SERVER_PORT] => 80 [SERVER_SIGNATURE] =>
    Apache/1.3.33 Server at www.gcs.net.nz Port 80
    [SERVER_SOFTWARE] => Apache/1.3.33 (Unix) mod_gzip/1.3.26.1a mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.10 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => POST [QUERY_STRING] => action=ulfile [REQUEST_URI] => /test/filestore.php?action=ulfile [SCRIPT_NAME] => /test/filestore.php [PATH_TRANSLATED] => /home/gcs1877/public_html/test/filestore.php [PHP_SELF] => /test/filestore.php [argv] => Array ( [0] => action=ulfile ) [argc] => 1 [HTTP_POST_VARS] => Array ( [desc] => Brinsley [go] => Upload ) [_POST] => Array ( [desc] => Brinsley [go] => Upload ) [HTTP_GET_VARS] => Array ( [action] => ulfile ) [_GET] => Array ( [action] => ulfile ) [HTTP_COOKIE_VARS] => Array ( ) [_COOKIE] => Array ( ) [HTTP_SERVER_VARS] => Array ( [CONTENT_LENGTH] => 4474 [CONTENT_TYPE] => multipart/form-data; boundary=---------------------------18028197120838 [DOCUMENT_ROOT] => /home/gcs1877/public_html [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7 [HTTP_ACCEPT_ENCODING] => gzip,deflate [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5 [HTTP_CONNECTION] => keep-alive [HTTP_HOST] => gcs.net.nz [HTTP_KEEP_ALIVE] => 300 [HTTP_REFERER] => http://gcs.net.nz/test/filestore.php [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1 [PATH] => /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin [REDIRECT_QUERY_STRING] => action=ulfile [REDIRECT_STATUS] => 200 [REDIRECT_URL] => /test/filestore.php [REMOTE_ADDR] => 60.234.193.209 [REMOTE_PORT] => 1904 [SCRIPT_FILENAME] => /home/gcs1877/public_html/test/filestore.php [SERVER_ADDR] => 198.87.84.10 [SERVER_ADMIN] => webmaster@gcs.net.nz [SERVER_NAME] => www.gcs.net.nz [SERVER_PORT] => 80 [SERVER_SIGNATURE] =>
    Apache/1.3.33 Server at www.gcs.net.nz Port 80
    [SERVER_SOFTWARE] => Apache/1.3.33 (Unix) mod_gzip/1.3.26.1a mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.10 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => POST [QUERY_STRING] => action=ulfile [REQUEST_URI] => /test/filestore.php?action=ulfile [SCRIPT_NAME] => /test/filestore.php [PATH_TRANSLATED] => /home/gcs1877/public_html/test/filestore.php [PHP_SELF] => /test/filestore.php [argv] => Array ( [0] => action=ulfile ) [argc] => 1 ) [_SERVER] => Array ( [CONTENT_LENGTH] => 4474 [CONTENT_TYPE] => multipart/form-data; boundary=---------------------------18028197120838 [DOCUMENT_ROOT] => /home/gcs1877/public_html [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7 [HTTP_ACCEPT_ENCODING] => gzip,deflate [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5 [HTTP_CONNECTION] => keep-alive [HTTP_HOST] => gcs.net.nz [HTTP_KEEP_ALIVE] => 300 [HTTP_REFERER] => http://gcs.net.nz/test/filestore.php [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1 [PATH] => /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin [REDIRECT_QUERY_STRING] => action=ulfile [REDIRECT_STATUS] => 200 [REDIRECT_URL] => /test/filestore.php [REMOTE_ADDR] => 60.234.193.209 [REMOTE_PORT] => 1904 [SCRIPT_FILENAME] => /home/gcs1877/public_html/test/filestore.php [SERVER_ADDR] => 198.87.84.10 [SERVER_ADMIN] => webmaster@gcs.net.nz [SERVER_NAME] => www.gcs.net.nz [SERVER_PORT] => 80 [SERVER_SIGNATURE] =>
    Apache/1.3.33 Server at www.gcs.net.nz Port 80
    [SERVER_SOFTWARE] => Apache/1.3.33 (Unix) mod_gzip/1.3.26.1a mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.10 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => POST [QUERY_STRING] => action=ulfile [REQUEST_URI] => /test/filestore.php?action=ulfile [SCRIPT_NAME] => /test/filestore.php [PATH_TRANSLATED] => /home/gcs1877/public_html/test/filestore.php [PHP_SELF] => /test/filestore.php [argv] => Array ( [0] => action=ulfile ) [argc] => 1 ) [HTTP_ENV_VARS] => Array ( [CONSOLE] => /dev/console [TERM] => linux [INIT_VERSION] => sysvinit-2.85 [PATH] => /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin [runlevel] => 3 [RUNLEVEL] => 3 [PWD] => / [LANG] => en_US.UTF-8 [previous] => N [PREVLEVEL] => N [SHLVL] => 3 [RESTARTSRV] => 1 [_] => /usr/local/apache/bin/httpd ) [_ENV] => Array ( [CONSOLE] => /dev/console [TERM] => linux [INIT_VERSION] => sysvinit-2.85 [PATH] => /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin [runlevel] => 3 [RUNLEVEL] => 3 [PWD] => / [LANG] => en_US.UTF-8 [previous] => N [PREVLEVEL] => N [SHLVL] => 3 [RESTARTSRV] => 1 [_] => /usr/local/apache/bin/httpd ) [HTTP_POST_FILES] => Array ( [uploadfile] => Array ( [name] => tn_brig023.jpg [type] => image/jpeg [tmp_name] => /tmp/php1NrAsg [error] => 0 [size] => 4074 ) ) [_FILES] => Array ( [uploadfile] => Array ( [name] => tn_brig023.jpg [type] => image/jpeg [tmp_name] => /tmp/php1NrAsg [error] => 0 [size] => 4074 ) ) [_REQUEST] => Array ( [action] => ulfile [desc] => Brinsley [go] => Upload ) [GLOBALS] => Array *RECURSION* [dbcnx] => Resource id #3 [image_dir] => test/images ) There was no file uploaded!

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well this part here:

    Code:
     [_FILES] => Array ( [uploadfile] => Array ( [name] => tn_brig023.jpg [type] => image/jpeg [tmp_name] => /tmp/php1NrAsg [error] => 0 [size] => 4074 ) )
    Would suggest that the file was in fact uploaded correctly, so perhaps there is an error in the script? I haven't got time to analyse it now though.
    This is a viral sig. Copy me and help me spread

  8. #8
    Non-Member
    Join Date
    May 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its bein written to the temp folder, so its havin trouble writing to ur actual folder, check uve specified the correct folder name and that its chmodded - failing that check the function thats actually writing the file to your directory

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the input, i'll check on these suggestions tomorrow and report back.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •