You are here: Re: Displaying Images form a Directory « PHP « IT news, forums, messages
Re: Displaying Images form a Directory

Posted by Jim Carlock on 10/16/05 11:36

"Jameson" <jameson_ray@comcast.net> wrote:
> If we take a look at it...

<?php
$dir = '.';
$sAltText = "Picture";
foreach (glob("*.jpg") as $file) {
echo "file: $file<br />\n";
echo "<i>filename:</i> <b>$file</b>, <i>filetype:</i> <b>" .
filetype($file) . "</b><br />\n";
echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' .
"\n";
}
?>

> ...Let's say we set the directory to $dir = '/Users/jray/Pictures';
> (a path that I can browse to just fine from the command line).
> Where is the $dir variable used in the rest of the script?

I used the $dir in one of the scripts and forgot to take it out this
one, or perhaps left it on purpose (unknown reason). :-)

The single dot indicates the current directory, two dots indicate
the parent directory of the current directory.

$dir = "..";

You could also use it as the path to the folder you want to parse
and applie the file exstension inside of it...

$dir = "../*.jpg";

> If a directory is specified in the glob() function, I believe it
> just defaults to the directory that the script is in.

That's exactly the way I see it. No difference. I find it much
better to use relative paths right at the moment, using

"../images/*.jpg"

to get to the folder in question, rather than absolute paths,

"/images/*.jpg"

The script would read...

<?php
$dir = '/images/*.jpg';
$sAltText = "Picture";
foreach (glob($dir) as $file) {
echo "file: $file<br />\n";
echo "<i>filename:</i> <b>$file</b>, <i>filetype:</i> <b>" .
filetype($file) . "</b><br />\n";
echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' .
"\n";
}
?>

As long as the web-root is set up properly, the "absolute
path" works fine on a website... let your root path be
'/Users/jray/' and the php file resides in, '/php', the images
reside in, '/images/'. So a php file in the php folder could
reference image files by using '../images/img.jpg' or
'/images/img.jpg'.

If you employed Explorer on a Windows system, you could
change from the currently viewed folder to another folder
by typing in ..\WINDOWS, or "..\Program Files\", or
...\WINDOWS\system32\ into the address bar. Someone
had a patent or some such on using the slash to denote paths.
Thus Microsoft patented the backslash concept (don't quote
me though as I can't identify the source read years ago and
maybe my mind plays tricks on me).

It works the same way on Unix systems but you change the
backslashes to forward slashes.

I imagine it works similar on a Mac, but I'll let someone
with Mac experience confirm it. Let me know.

> Thanks again for sticking with this. Sometimes is is good
> to have an understanding of how a function works across
> different operating systems.

Yes, especially when it all jives together and works in the
same or similar manners.

Hope that helps.

Jim Carlock
Post replies to the newsgroup.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация