PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Can't Escape
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Can't Escape

Réponse
 
LinkBack Outils de la discussion
Vieux 11/09/2007, 22h41   #1
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Can't Escape

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!

  Réponse avec citation
Vieux 11/09/2007, 22h48   #2
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

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!
>
>


  Réponse avec citation
Vieux 11/09/2007, 22h50   #3
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

..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
  Réponse avec citation
Vieux 11/09/2007, 22h57   #4
Ron Barnett
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

"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


  Réponse avec citation
Vieux 11/09/2007, 23h02   #5
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

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

  Réponse avec citation
Vieux 11/09/2007, 23h12   #6
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

..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
  Réponse avec citation
Vieux 12/09/2007, 00h19   #7
Ron Barnett
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

"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



  Réponse avec citation
Vieux 12/09/2007, 00h29   #8
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

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

  Réponse avec citation
Vieux 12/09/2007, 00h30   #9
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

On 2007-09-11 16:19:12 -0700, "Ron Barnett" <No@Spam.Thanks> said:

> "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


Teah I scarcely remember taking that Unix class at Deanza College.
Should have saved my notes but most likely not relevant anyway.

  Réponse avec citation
Vieux 12/09/2007, 05h51   #10
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

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


I got this from the manual:

<?php
var_dump(is_dir('a_file.txt')) . "\n";
var_dump(is_dir('bogus_dir/abc')) . "\n";

var_dump(is_dir('..')); //one dir up
?>
The above example will output:
bool(false)
bool(false)
bool(true)

Don't think I need the var dump part. Below I inserted where I think
the test should go. Basically read from the directory, test to see if
it's a dir, if a dir get the next $file, if not echo. I don't think I
can replace the readdir with the is_dir because the first two items ARE
directories so I can't tell it to stop if it sees a dir.
<?php
//Open images directory
$dir = opendir("images");
//List files in images directory
while (($file = readdir($dir)) !== false)
{
// Test for file or directory with is_dir like if $file is a directory
get the next $file.
echo "<img src='images/$file'>";
}
closedir($dir);
?>

I didn't see any very simple examples of get me everything that is a
".jpg" type of thing which would work too.

Time for some sleep.

  Réponse avec citation
Vieux 12/09/2007, 13h03   #11
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can't Escape

Confused but working on it wrote:
> 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

>
> I got this from the manual:
>
> <?php
> var_dump(is_dir('a_file.txt')) . "\n";
> var_dump(is_dir('bogus_dir/abc')) . "\n";
>
> var_dump(is_dir('..')); //one dir up
> ?>
> The above example will output:
> bool(false)
> bool(false)
> bool(true)
>
> Don't think I need the var dump part.


No, that's showing how is_dir works, not necessarily "live code".

> Below I inserted where I think the
> test should go. Basically read from the directory, test to see if it's a
> dir, if a dir get the next $file, if not echo. I don't think I can
> replace the readdir with the is_dir because the first two items ARE
> directories so I can't tell it to stop if it sees a dir.
> <?php
> //Open images directory
> $dir = opendir("images");
> //List files in images directory
> while (($file = readdir($dir)) !== false)
> {
> // Test for file or directory with is_dir like if $file is a
> directory get the next $file.
> echo "<img src='images/$file'>";
> }
> closedir($dir);
> ?>
>
> I didn't see any very simple examples of get me everything that is a
> ".jpg" type of thing which would work too.
>
> Time for some sleep.
>


And you won't find examples for everything you want to do. There are
too many possibilities. You need to get familiar with the language and
function calls so you can look up what you need. I find having a local
copy of the PHP doc (as a windows file) is quite useful.

To get a file extension, check out
http://us2.php.net/manual/en/function.pathinfo.php

Then you can compare on the extension. Just a warning - if the files
are uploaded by users, don't depend on the extension being correct.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h05.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23479 seconds with 19 queries