Jason Cathcart
Jason Cathcart

Reputation: 23

Displaying Image from PHP Source results in Parse Error

I am working with a simple script that outputs an image via a PHP script in an HTML IMG tag. The code used to get the image file is as follows.

<img <?php print "$width"; ?>src="http://www.cdja.ca/validate/img.php?memnum=<?php print "$memnum"; ?>&type=<?php print "$type"; ?>" alt="">

This is perfect and works fine with the existing images. However the organization has recently updated their logo and wishes to have the images updated to reflect.

When I reference the new images I get a PHP warning and the image does not output. The warning I get is

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /hsphere/local/home/cdjai/cdja.ca/validate/logo_links/Vice-A.gif on line 1 Parse error: syntax error, unexpected T_STRING in /hsphere/local/home/cdjai/cdja.ca/validate/logo_links/Vice-A.gif on line 1

Google tells me this is a namespace issue, however I don't believe I am using namespaces in the code.

As a test I decided to see what was being output by the script for the original logo and the new logo.

The original logo gets me GIF output - http://cdja.ca/validate_new/img.php?memnum=150&type=2

But the new logo just gets me the above warning - http://cdja.ca/validate_new/img.php?memnum=548&type=2

If I change the output file type from GIF to PNG - which is my preference - the warning goes away and I just get a parse error.

Parse error: syntax error, unexpected T_STRING in /hsphere/local/home/cdjai/cdja.ca/validate_new/logo_links/Vice-A.png on line 4

I am really stuck about why I am getting a parse error with the new files instead of the output I get with the old files.

The img.php code is below

ini_set('display_errors',1); 
error_reporting(E_ALL);
include ("../phpcommon/phpHelper.php");
include ("../quoteadmin/db_writer.php");

$memnum = getParameterString("memnum");
$type = getParameterString("type");

$img = "logo_links/";
$ext = "gif";

/* ----------  Image Versions ----------  */

if ($type == 1){
    if ($memnum == "001" or $memnum == "002" or $memnum == "006" or $memnum == "015" or $memnum == "121" or $memnum == "153" or $memnum == "323" or $memnum == "150"){
        $filename1 = "Life-White";
    } 
else{
        $filename1 = "Certified1";
    }
}

include $img . $filename1 . "." . $ext;

Upvotes: 1

Views: 538

Answers (2)

Ruben Vincenten
Ruben Vincenten

Reputation: 907

Don't use include to display an image. If it's an uploaded file, you are exposing security risks that way.

Try the following:

header('Content-Type: image/gif');
echo file_get_contents($file);

Upvotes: 0

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23409

instead of including the image, use readfile to send it..

header("Content-Type: image/gif");
readfile($img . $filename1 . "." . $ext);
exit;

:)

Upvotes: 2

Related Questions