|
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
Hi all,
I'm trying to do something simple and grabbed a snippet from the php manual for reading files in a directory. The snippet echos out a nice list of files. <?php //Open images directory $dir = opendir("images"); //List files in images directory while (($file = readdir($dir)) !== false) { echo "filename: " . $file . "<br />"; } closedir($dir); ?> So with my very limited PHP skillset I am trying to concatente the $file variable in an image tag. I can't seem to escape this the right way. I've done some cutting and pasting and commenting trying to figure this out. (keep the laughing down. ![]() <?php //NOT WORKING - shows the little blue question mark box echo "<img src = \"/DSC01351.jpg\">"; //Works echo "<br>"; //Open images directory $dir = opendir("images"); //List files in images directory while (($file = readdir($dir)) !== false) { // Below Works---Shows the filename like DSC01351.jpg echo "filename: " . $file; //NOT - shows the little blue question mark box echo "<img src=" . $file . ">"; //NOT - shows the little blue question mark box echo "<img src=\".$file.\">"; } closedir($dir); ?> Just amazingly frustrated with something that is so simple. I'v tried a lot of ways to escape the quotes in the img tag but can't mahe it work. left a couple of example... Any would be greatly appreciated. TIA! |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
Confused but working on it <PostInGroups@wherever.com> wrote in
news:2007091114413816807-PostInGroups@wherevercom: > Hi all, > > I'm trying to do something simple and grabbed a snippet from the php > manual for reading files in a directory. The snippet echos out a nice > list of files. > <?php > //Open images directory > $dir = opendir("images"); > //List files in images directory > while (($file = readdir($dir)) !== false) > { > echo "filename: " . $file . "<br />"; > } > closedir($dir); > ?> > > So with my very limited PHP skillset I am trying to concatente the > $file variable in an image tag. I can't seem to escape this the right > way. I've done some cutting and pasting and commenting trying to > figure this out. (keep the laughing down. ![]() > > <?php > //NOT WORKING - shows the little blue question mark box > echo "<img src = \"/DSC01351.jpg\">"; This means: show an image, the source of which is at the top of the server. Perhaps you should put the entire path to the image, relative to where your PHP script is being called from. echo "<img src=\"path/to/my/image/DSC01351.jpg\">"; > //List files in images directory > while (($file = readdir($dir)) !== false) > { > // Below Works---Shows the filename like DSC01351.jpg > echo "filename: " . $file; > //NOT - shows the little blue question mark box > echo "<img src=" . $file . ">"; > //NOT - shows the little blue question mark box > echo "<img src=\".$file.\">"; > > } > closedir($dir); > ?> according to your script, the images are located in the directory "images". therefore, you need the directory name in addition to the image name. should be: echo "<img src=\"images/".$file."\" alt=\"my image\">"; etc > > Just amazingly frustrated with something that is so simple. I'v tried a > lot of ways to escape the quotes in the img tag but can't mahe it work. > left a couple of example... > > Any would be greatly appreciated. > > TIA! > > |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
..oO(Good Man)
>Confused but working on it <PostInGroups@wherever.com> wrote in >news:2007091114413816807-PostInGroups@wherevercom: > >> <?php >> //NOT WORKING - shows the little blue question mark box >> echo "<img src = \"/DSC01351.jpg\">"; > >This means: show an image, the source of which is at the top of the >server. Perhaps you should put the entire path to the image, relative >to where your PHP script is being called from. > >echo "<img src=\"path/to/my/image/DSC01351.jpg\">"; Additionally HTML also allows single quotes, which avoids the ugly escaping: echo "<img src='path/to/my/image/DSC01351.jpg'>"; >according to your script, the images are located in the directory >"images". therefore, you need the directory name in addition to the >image name. should be: > >echo "<img src=\"images/".$file."\" alt=\"my image\">"; echo "<img src='images/$file' alt='my image'>"; I find that much more readable. Micha |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
"Confused but working on it" <PostInGroups@wherever.com> wrote in message
news:2007091114413816807-PostInGroups@wherevercom... > Hi all, > > I'm trying to do something simple and grabbed a snippet from the php > manual for reading files in a directory. The snippet echos out a nice list > of files. > <?php > //Open images directory > $dir = opendir("images"); > //List files in images directory > while (($file = readdir($dir)) !== false) > { > echo "filename: " . $file . "<br />"; > } > closedir($dir); > ?> > > So with my very limited PHP skillset I am trying to concatente the $file > variable in an image tag. I can't seem to escape this the right way. I've > done some cutting and pasting and commenting trying to figure this out. > (keep the laughing down. ![]() > > <?php > //NOT WORKING - shows the little blue question mark box > echo "<img src = \"/DSC01351.jpg\">"; > //Works > echo "<br>"; > //Open images directory > $dir = opendir("images"); > //List files in images directory > while (($file = readdir($dir)) !== false) > { > // Below Works---Shows the filename like DSC01351.jpg > echo "filename: " . $file; > //NOT - shows the little blue question mark box > echo "<img src=" . $file . ">"; > //NOT - shows the little blue question mark box > echo "<img src=\".$file.\">"; > > } > closedir($dir); > ?> > > Just amazingly frustrated with something that is so simple. I'v tried a > lot of ways to escape the quotes in the img tag but can't mahe it work. > left a couple of example... > > Any would be greatly appreciated. > > TIA! Hi Confused, well you've certainly circled all around it ! First, a basic error, You've listed the files in a particular directory but neglected to tell the HTML image tag what the directory name is ! Second, not really an error, but you can nest single quotes inside double quotes to avoid all the confusing escaping. Try This <?php //Open images directory $dir = opendir("images"); //List files in images directory while (($file = readdir($dir)) !== false) { echo "<img SRC='images/".$file."'><br />"; echo "filename: " . $file . "<br />"; } closedir($dir); ?> note the SRC attribute is in single quotes with the double quotes encapsulating the srings. Cheers Ron |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
On 2007-09-11 14:50:53 -0700, Michael Fesser <netizen@gmx.de> said:
> .oO(Good Man) > >> Confused but working on it <PostInGroups@wherever.com> wrote in >> news:2007091114413816807-PostInGroups@wherevercom: >> >>> <?php >>> //NOT WORKING - shows the little blue question mark box >>> echo "<img src = \"/DSC01351.jpg\">"; >> >> This means: show an image, the source of which is at the top of the >> server. Perhaps you should put the entire path to the image, relative >> to where your PHP script is being called from. >> >> echo "<img src=\"path/to/my/image/DSC01351.jpg\">"; > > Additionally HTML also allows single quotes, which avoids the ugly > escaping: > > echo "<img src='path/to/my/image/DSC01351.jpg'>"; > >> according to your script, the images are located in the directory >> "images". therefore, you need the directory name in addition to the >> image name. should be: >> >> echo "<img src=\"images/".$file."\" alt=\"my image\">"; > > echo "<img src='images/$file' alt='my image'>"; > > I find that much more readable. > > Micha Wow, Thanks! I didn't even think to try adding the images/ part to the line. Figured it was outputting the filename so... Mixing single and double quotes in the case eliminates the need to escape I take it. Now it appears I have files named "." and "..", well, back to the manual... Thanks again |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
..oO(Confused but working on it)
>Now it appears I have files named "." and "..", well, back to the manual... readdir() returns _all_ directory entries, normal files and other directories. You can use is_file() or is_dir() to filter out the unwanted. See the manual for details and examples. Micha |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
"Confused but working on it" <PostInGroups@wherever.com> wrote in message
news:2007091115023216807-PostInGroups@wherevercom... > On 2007-09-11 14:50:53 -0700, Michael Fesser <netizen@gmx.de> said: > >> .oO(Good Man) >> >>> Confused but working on it <PostInGroups@wherever.com> wrote in >>> news:2007091114413816807-PostInGroups@wherevercom: >>> >>>> <?php >>>> //NOT WORKING - shows the little blue question mark box >>>> echo "<img src = \"/DSC01351.jpg\">"; >>> >>> This means: show an image, the source of which is at the top of the >>> server. Perhaps you should put the entire path to the image, relative >>> to where your PHP script is being called from. >>> >>> echo "<img src=\"path/to/my/image/DSC01351.jpg\">"; >> >> Additionally HTML also allows single quotes, which avoids the ugly >> escaping: >> >> echo "<img src='path/to/my/image/DSC01351.jpg'>"; >> >>> according to your script, the images are located in the directory >>> "images". therefore, you need the directory name in addition to the >>> image name. should be: >>> >>> echo "<img src=\"images/".$file."\" alt=\"my image\">"; >> >> echo "<img src='images/$file' alt='my image'>"; >> >> I find that much more readable. >> >> Micha > > Wow, Thanks! > I didn't even think to try adding the images/ part to the line. Figured it > was outputting the filename so... > > Mixing single and double quotes in the case eliminates the need to escape > I take it. > > Now it appears I have files named "." and "..", well, back to the > manual... > > Thanks again .. refers to the current directory, and .. refers to its parent - its an Operating System thing. Cheers Ron |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
On 2007-09-11 15:12:16 -0700, Michael Fesser <netizen@gmx.de> said:
> .oO(Confused but working on it) > >> Now it appears I have files named "." and "..", well, back to the manual... > > readdir() returns _all_ directory entries, normal files and other > directories. You can use is_file() or is_dir() to filter out the > unwanted. See the manual for details and examples. > > Micha Great leads, thanks |
|
![]() |
| Outils de la discussion | |
|
|