开发者

Calling a PHP script to get image urls

My path is like this

/website/php

/website/javascript

/website/photographer/index.html

/website/photographer/photos

I have a javascript which displays a photo gallery for the photos in the folder. Using ajax with jquery, it calls a PHP file that returns the image urls in "photos" so that the javascript can then set up the gallery, etc.

The problem is that I can't seem to get PHP to access the "photos" folder. See, I have a bunch of different photographer pages which do the same thing, so I'm trying to have one PHP file and one javascript file, but different directories.

The method I devised was to send "window.location.pathname" in the ajax get call so that php can get the directory the current page is in, and then simply search the "photos" folder in that directory.

chdir($_GET["currentPath"] . "photos"); //change the directory so we just get the file names
echo json_encode(glob("*.jpg"));

I get this 开发者_开发技巧error: Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /hermes/bosweb/web054/b541/ipg.danielechevarriacom/testing/oneonethousand_test/php/images.php on line 4 []

And when I

echo $_GET["currentPath"] . "photos";

It prints: "/testing/oneonethousand_test/steele/photos"

That is the correct directory where the photos are, so I don't know why this isn't working. I am assuming this has to do with how chdir works.

I don't want to do chdir("../steele/photos") because I will have many directories and I want to have the PHP file stay general. I just want to be able for javascript to tell php where the current directory is and then PHP switches to that directory. Why doesn't this work?

Thanks.


First off, NEVER trust the variables in $_GET/$_POST/etc. Using your script, I could potentially list out file in any directory on the server (including non-web-accessible ones), not just the ones you want.

It doesn't work because the path in $_GET["currentPath"] is the URL, not the folder name: it's set to "http://danielechevarria.com/testing/oneonethousand_test/steele/" instead of "steele".

Change the variable sent by the JavaScript to just be the top-level path (such as "steele") and consider changing your script to be something like this:

//Validate the path somehow, in this case, reject anything that's not alphanumeric
if(!preg_match("/^\w+$/", $_GET["currentPath"])) die("{'error':'Invalid path'}");
chdir("../" . $_GET["currentPath"] . "/photos");
echo json_encode(glob("*.jpg"));


chdir requires a file system path, not an http path.

Based on the output of the echo command, it looks like it is passing the whole http url ("http://danielechevarria.com/testing/oneonethousand_test/steele/photos") to the chdir function.


You can use curl in php to call ("http://danielechevarria.com/testing/ oneonethousand_test/steele/photos")


I believe chdir only works on local paths, not web paths. So, in order to "chdir" to the photos directory, you need the FULL system path.

i.e. /home/user/public_html/photos or C:\www\photos

http://us2.php.net/chdir

Here's my input: No idea why you are using ajax and javascript to pull in images and build a gallery. Why not just use PHP to generate the gallery? Javascript over complicates this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜