1

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;
1
  • wtf triston that was my favorite part of this whole question.. Commented Jan 14, 2016 at 17:44

2 Answers 2

2

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

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

:)

Sign up to request clarification or add additional context in comments.

1 Comment

You have an extra semi-colon on the readfile line, but otherwise this works perfectly. Thank you!
0

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);

5 Comments

1) that's basically the same answer i gave, 2) you explained it wrong. PHP doesn't care if the included file is PHP or not.
@Pamblam Eh, it's probably good to show 2 different possible ways to do it, no harm there. But you are 100% correct in that the interpreter won't care if it's php, html, etc., in the included file.
if he would remove the incorrect information i will gladly remove the downvote. i can't remove it unless he edits the question anyway.
I forgot to mention that using include on image files is a security risk, and yes, you beat me to it while I was writing my initial post.
Images are no more of a security risk than including other files, but perhaps it's a more common risk. Never trust user input, regardless of it being a JPG or a PDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.