Welcome to WebmasterWorld Guest from 23.22.136.56

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP saves image, can't access it

in Wordpress

     
10:35 pm on May 25, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 30, 2008
posts:104
votes: 0


I'm building a custom WordPress theme that includes multiple admin pages, including one that deals with photos. My script takes an uploaded photo, resizes it multiple times and saves the results into their respective folders. Everything works properly on my local Ubuntu development server, but when I took it live on my BlueHost account, it went all screwy. I know the photos are being saved properly because I can download the new images and they work properly. It seems that whenever I reference the new photos in an image declaration, I get nothing. It says file not found, and I'm almost certain the path is right. I'm running the test setup in a subdomain on my main hosting account.

What could it be?
10:52 pm on May 25, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 10, 2007
posts:507
votes: 0


What about access rights?

Does the script have the correct permissions to access the newly created images?
11:43 pm on May 25, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 30, 2008
posts:104
votes: 0


The images and parent folders all have 644 permissions (I tried 777 temporarily to see if that made a difference) so I believe the server has access. The plugin script uses WP's template system, so it's not really a script per-se, more of a function. Should I make the actual file 644 too?
7:36 am on May 26, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


and I'm almost certain the path is right

Have you echoed the path to the screen just to make sure?

dc
5:38 pm on May 26, 2010 (gmt 0)

New User

10+ Year Member

joined:July 31, 2006
posts: 39
votes: 0


Sometimes people get confused with the URL path and the directory path. Also with CMS like wordpress you would likely use an internal function or constant to find the absolute path of your image file.
1:34 am on May 27, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


To expand on the previous post, as I do believe that is your problem, because of this:

It says file not found


Too often in php you'll see stuff like this

Take a script

scripts/widget-processor.php

and in that file,

$info = GetImageSize("../images/filename.jpg");

then output

echo '<img src="../images/filename.jpg" width=' . $info[0] . '" "' . $info[1] . '" alt="whatever">';

Which is fine, as long as they are both executed from the same directory, etc. But often there are other things at play:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Widgets\/*$ /scripts/widget-processor.php
</ifModule>

What just happened there?

When I call example.com/Widgets, it executes scripts/widget-processor.php That script outputs

echo '<img src="../images/filename.jpg" width=' . $info[0] . '" "' . $info[1] . '" alt="whatever">';

But since the request is /Widgets, not /scripts/widget-processor.php, the img src, ../, is going to look for a directory ABOVE /Widgets - which is not (should not) be possible.

There's an easy fix for both of these, so you always know "where you are at." If you have this structure:

/ (your domain root)
/images/filename.jpg
/scripts/widget-processor.php

For images or other resource references from within a web page, always start with /. This means "start at the domain root" and will work no matter how many directories deep you are. For PHP functions, always use the FULL PATH. So the two above examples can be

$info = GetImageSize($_SERVER['DOCUMENT_ROOT'] . "/images/filename.jpg");

echo '<img src="/images/filename.jpg" width=' . $info[0] . '" "' . $info[1] . '" alt="whatever">';

... and I'll bet it will work. :-)

A caveat: I just caught this

I'm running the test setup in a subdomain on my main hosting account.


This would be one of the exceptions to the above (the other being virtual hosts.) So you may need to do something like this.

$virtual_path = $_SERVER['DOCUMENT_ROOT'] . "/subdomain.example.com";
$full_url = "https://subdomain.example.com";

$info = GetImageSize("$virtual_path/images/filename.jpg");

echo '<img src="$full_url/images/filename.jpg" width=' . $info[0] . '" "' . $info[1] . '" alt="whatever">';

I had to use https so you could see the code, can't wait until they fix that stupid little quirk on this board . . .

I threw in the mod_rewrite bit as an example as it's common for a little rewriting to be going on in WordPress. Even if it's not, the above principles should help you.
3:16 am on May 27, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 30, 2008
posts:104
votes: 0


Thanks everyone, for your replies. I will get to them all shortly and hopefully have a solution.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members