www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] error when including an image | Unexpected character

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Posts
    370

    resolved [RESOLVED] error when including an image | Unexpected character

    I am in unfamiliar territory.
    I am using an API to access data from a service. In an effort to take some of the load off the service and onto our server, we are caching the images.
    Here's a snippet that either pulls from cache or writes the new one to cache.
    PHP Code:
    if(file_exists($fullpath)){
    //$contents=file_get_contents($fullpath);
    //echo $contents;
    include($fullpath);
    }
    else{
    $urlREDACTED;
    save_image($url,$fullpath);
    include(
    $fullpath);

    The else portion used to be
    PHP Code:
    $urlREDACTED;
    echo(
    save_image($url,$fullpath)); 
    The save_image function uses cURL to access an image from the service at $url. I set it to return the raw data when I tried the echo version.

    At first it didn't occur to me that including the jpg would work, so I used file_get_contents. I noticed it was slow, so I wanted to speed it up. I tried incude and I got various error messages on 3 of the 49 images in my test query. the errors were like this:
    Code:
    Parse error: syntax error, unexpected '~' in /home/user/...###thumbs.jpg on line 7
    
    Warning: Unexpected character in input: '' (ASCII=14) state=1 in /home/user/public_html/...###thumbs.jpg on line 9
    Parse error: syntax error, unexpected T_STRING in /home/user/public_html/...###thumbs.jpg on line 9
    
    and 
    
    Warning: Unexpected character in input: '' (ASCII=22) state=1 in /home/user/...###thumbs.jpg on line 8
    Parse error: syntax error, unexpected T_STRING in /home/user/...###thumbs.jpg on line 8
    My guess is that I need to find a way to tell it not to parse, even though it is being included. Include seems to be much faster than file_get_contents, so I'd like to stick with it if I can.
    Last edited by TecBrat; 10-29-2010 at 01:06 AM. Reason: fix typos

  2. #2
    Join Date
    Oct 2010
    Location
    Melbourne, Australia
    Posts
    10
    Your errors are from include trying to parse the jpg image as PHP. You can include HTML files because they can be parsed as PHP but not images.

    Have you tried:
    Code:
    echo file_get_contents($fullpath);
    This avoids the intermediate variable which in theory should increase its response.
    __
    Leslie Liew - Web Developer

  3. #3
    Join Date
    Oct 2007
    Posts
    370
    As I was typing my question, I thought about skipping the variable and putting file_get_contents directly in the echo, but I haven't tried it. (It was way past my bed-time when I posted that. )I'll let you know how it goes.

    Thanks.

  4. #4
    Join Date
    Oct 2007
    Posts
    370
    Talman,

    Thanks for the suggestion. I tried it and it seems to be faster than my original method, and obviously fixes the error I was getting.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles