• I’m trying to serve some hidden images (not in public_html folder) using a php file. For some reason I’m getting a broken link image and I really don’t know what I’m doing wrong. (it might be something really stupid).

    For simplicity purpose, I’m storing the image file in the same level as the other files (as it still gives me a broken link).

    I’m storing the php file in the root and calling it on my php page using

    <img src="LoadImg.php"/>

    Then, the LoadImg.php is very simple:

    <?php
    
    header("Content-type: image/jpeg");
    $img='image.jpg';
    readfile($img);
    
    ?>

    I’m getting a 404 when the browser looks for http://siodental.com/LoadImg.php
    So I’m guessing it might have to do with the .htaccess or some default configuration that WordPress does.

    Thank you!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Your file works on one of my WP sites. I have the htaccess that pretty permalinks generated. WordPress is installed in the root and that’s where I put LaodImg.php. I see the image at mysite.com/LoadImg.php.

    So it’s not a problem for all WordPress sites. There is something different in yours.

    Thread Starter bertini

    (@bertini)

    Okay thanks. I guess I’ll have to look in another direction then.

    Did you look at your htaccess to see if it was the usual file? I can’t see your image file using siodental.com/image.jpg Are you sure you have the two files in the root?

    Thread Starter bertini

    (@bertini)

    I just changed the htaccess to the default one and still broken.
    I deleted image.jpg, but i’m using another one, that isn’t the problem but thanks for asking.

    Thread Starter bertini

    (@bertini)

    I added the <img src…> to the start page so maybe you can see the problem!

    Start page is link

    The complete php file I’m calling is

    <?php
    error_reporting(E_ALL); ini_set('display_errors', 1);
    header("Content-type: image/jpeg");
    require_once("wp-load.php");
    
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    
    $img='siodental.jpg';
    header('Content-Length: ' . filesize($img));
    readfile($img);
    
    ?>

    This php file is called img.php and it’s stored in the root (public_html). The image siodental.jpg is also in stored in the root.

    Thank you.

    Try require_once(‘/home/whatever/public_html/wp-load.php’); And I would put it in the first line of the file with the proper path for your installation.

    Dion

    (@diondesigns)

    Since nothing in your PHP file requires WordPress, I’d suggest removing this block of code:

    require_once("wp-load.php");
    
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;

    WordPress sets its own error reporting level as well as several headers, and it may be the cause of your problem.

    @diondesigns. You are right. The code in @bertini’s first post works in my site. But it doesn’t work in his. The extra code in his last post also works in my site.

    The issue is, why doesn’t the image show up on his site?

    I’d guess that either the file is not in the root folder, or there’s something somewhere that’s blocking it from being displayed. That’s because the URL for the img.php file is coming up as a 404 error.

    The image file that’s being referenced is there and I can see that, but the PHP script seems to be missing. That’s what the problem is. If you can find out what’s happening to cause that 404 error then you’ll be able to fix it. I’d suggest looking at the servers error logs and even access logs to see what’s in there.

    wow @catacaustic you have a great grasp of the obvious.

    Hmm… @wslade, I’m not ure if that’s meant to be nasty or nice, but I’m guessing that it’s nasty?

    I’m happy to be critisized, but I’d appreciate knowing what I thought/got wrong so I can correct it next time instead of just a line of vitriol like that.

    From what is posted here, the code works, so there’s nothing wrong. The image isn’t showing in the example on the given page because the URL gives a “not found” error from WordPress. So armed with that, what is your suggestion as to why there’s a 404 error instead of seeing the image output by the script. πŸ™‚

    Thread Starter bertini

    (@bertini)

    Hi, thanks for the answers. I posted the php file I’m working on, I need to access the ID to allow the image to be served or not. Still WIP. I posted the whole thing just to see if it affected the problem.

    As it works for both of you and the file locations are okay (tried different files, different locations), I guess I’ll have to talk with the hosting company and look at the logs.

    Thank you!

    Dion

    (@diondesigns)

    I guess you found the problem since the img.php script is now serving an image. Was your problem HTACCESS-related, file permission related, or something else?

    Thread Starter bertini

    (@bertini)

    Hello, sorry forgot to answer with the solution!

    I found out that not only it is bad practice to load wordpress function calling require-once wp-load but that it generated that 404 error.

    I now use this code to load wp functionality:

    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');

    And everything works!

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘serve image through PHP’ is closed to new replies.