How can I convert the web path to a file to the server path, or visa versa?
Ex:
D:\sites\blah\www\somedir\index.php to
/somedir/index.php
The only thing I was able to find that might do it is apache_lookup_uri, but I want to be able to do this independent of what server I'm using.
[SOLVED] How to convert web path to server path
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
the problem comes.. a server can use aliases outside of your control or knowledge for all paths.. so translating a path will be very tricky. However, most paths inside a hosted area aren't aliased most often. That being said, $_SERVER['DOCUMENT_ROOT'] will contain the literal path to your document root.
I don't want it for the script, though, I want it for any file.
I'm writing a photo-albums script that looks at a directory on the server and creates links to the photos within the directory. The problem I'm having is that scandir() wants the server path to the directory, but to actually output the path in webpage I need to use the web path. In the PHP manual's example of scandir they use the web path, but I can't get that to work for some reason.
Line 34 is where I'm having the problem. I need to create a link to the photo (using the web path, of course), but I have to give scandir the server path. The statement in line 14 produces an error from scandir() saying it failed to open the dir.
I'm writing a photo-albums script that looks at a directory on the server and creates links to the photos within the directory. The problem I'm having is that scandir() wants the server path to the directory, but to actually output the path in webpage I need to use the web path. In the PHP manual's example of scandir they use the web path, but I can't get that to work for some reason.
Line 34 is where I'm having the problem. I need to create a link to the photo (using the web path, of course), but I have to give scandir the server path. The statement in line 14 produces an error from scandir() saying it failed to open the dir.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Photo Albums</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$basePhotoDir = "C:/Documents and Settings/dunniana/My Documents/Web Design/Playground/photos";
//$basePhotoDir = '/Playground/photos';
if(array_key_exists('album', $_GET))
{
$album = $_GET['album'];
if(array_key_exists('photo', $_GET)) // If given album and photo then display photo from album
{
$photo = $_GET['photo'];
echo "album and photo given - displaying photo";
}
else // If only given album then display thumnails from album
{
$files = scandir($basePhotoDir . "/" . $album);
for($counter = 2 ; $counter < count($files) ; $counter++)
{
if(stripos($files[$counter], "-thumb") != false)
{
printf("<a href='%s?album=%s&photo=%s'><img src='%s' alt='%s' title='%s' /></a> ", $_SERVER['PHP_SELF'], $album, $files[$counter], $basePhotoDir . "/" . $album . "/" . str_replace("-thumb", "", $files[$counter]), $files[$counter], $files[$counter]);
}
}
}
}
else // If not given anything then display list of albums
{
$files = scandir($basePhotoDir);
for($counter = 2 ; $counter < count($files) ; $counter++)
{
printf("<a href='%s?album=%s'>%s</a><br />", $_SERVER['PHP_SELF'], $files[$counter], str_replace("-", " ",$files[$counter]));
}
}
?>
</body>
</html>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
/Playground/photos would likely translate to C:/Playground/photos
you can use realpath() to get the server's full path to a known location, however as the documentation states, the function will return false on failure.
you can use realpath() to get the server's full path to a known location, however as the documentation states, the function will return false on failure.
Here's the final (for now) version, if anyone's interested:
Code: Select all
<?php
/*
Assumptions:
* Each "album" is a subfolder of $basePhotoDir
* Only directories are in $basePhotoDir
* Only image files are in each "album" folder
* Thumbnails are named exactly the same as the original image, except have "-thumb" directly before the filename extension (e.g. dog.jpg and dog-thumb.jpg)
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Photo Albums</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$basePhotoDir = '/playground/photos';
if(array_key_exists('album', $_GET))
{
$album = $_GET['album'];
if(array_key_exists('photo', $_GET)) // If given album and photo then display photo from album
{
$files = scandir($_SERVER['DOCUMENT_ROOT'] . $basePhotoDir . "/" . $album);
$photoNum = $_GET['photo'];
$photo = str_replace("-thumb", "", $files[$photoNum]);
echo '<p style="text-align: center;">';
if(array_key_exists($photoNum - 2, $files) && $photoNum != 2)
printf('<a href="%s?album=%s&photo=%s">«</a> | ', $_SERVER['PHP_SELF'], $album, $photoNum - 2);
else
printf('« | ');
printf('<a href="%s?album=%s">Thumbnails</a> | ', $_SERVER['PHP_SELF'], $album);
if(array_key_exists($photoNum + 2, $files))
printf('<a href="%s?album=%s&photo=%s">»</a>', $_SERVER['PHP_SELF'], $album, $photoNum + 2);
else
printf('»');
echo '</p>' . chr(13) . chr(10);
printf('<p style="text-align: center"><img src="%s" style="border: 1px solid black;" alt="%s" title="%s" /></p>', $basePhotoDir . "/" . $album . "/" . $photo, $photo, $photo);
}
else // If only given album then display thumnails from album
{
$files = scandir($_SERVER['DOCUMENT_ROOT'] . $basePhotoDir . "/" . $album);
echo '<p style="text-align: center;">';
for($counter = 2 ; $counter < count($files) ; $counter++)
{
if(stripos($files[$counter], "-thumb") != false)
{
printf('<a href="%s?album=%s&photo=%s"><img src="%s" style="border: 1px solid black;" alt="%s" title="%s" /></a> ', $_SERVER['PHP_SELF'], $album, $counter, $basePhotoDir . "/" . $album . "/" . $files[$counter], $files[$counter], $files[$counter]);
}
}
echo "</p>";
}
}
else // If not given anything then display list of albums
{
$files = scandir($_SERVER['DOCUMENT_ROOT'] . $basePhotoDir);
for($counter = 2 ; $counter < count($files) ; $counter++)
{
printf("<a href='%s?album=%s'>%s</a><br />", $_SERVER['PHP_SELF'], $files[$counter], str_replace("-", " ",$files[$counter]));
}
}
?>
</body>
</html>