...

View Full Version : HELP! need someone brainy.



cragllo
04-27-2004, 07:08 PM
How can i change this code to make the files show up, A to Z.



$path = "digits";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (is_dir($path."/".$file)) {
if (!is_file($path."/".$file)) {
if (($file!=".") && ($file!="..")) {
echo "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
}
}
}
}
closedir($dh);



Any suggestions are greatful.

missing-score
04-27-2004, 07:33 PM
im assuming you mean so that they show up alphabetically?

well what I would do, is store the values in an array and then sort them.... like so:



$files = array();

$path = 'digits';
$dh = opendir($path);
while ($file = readdir($dh)) {
if (is_dir($path.'/'.$file)) {
if (!is_file($path.'/'.$file)) {
if (($file!='.') && ($file!='..')) {
$files[] = $file;
}
}
}
}
closedir($dh);

sort($files);

for($i=0;$i<count($files);$i++){

echo "<p><table border=0><tr><td class=\"boldtitle\">".$files[$i]."</td></tr><tr><td><img src=\"digits/".$files[$i].".gif\"></td></tr></table><br></p>\n";


}


You may want to take a look at http://www.php.net/sort to see how this works...

sad69
04-27-2004, 07:33 PM
$files = Array();
$path = "digits";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (is_dir($path."/".$file)) {
if (!is_file($path."/".$file)) {
if (($file!=".") && ($file!="..")) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
}


That should do the trick.. I dunno if there's a better way..

Whoops! Almost at the same time, eh missing-score? On the same track though

Sadiq.

missing-score
04-27-2004, 07:34 PM
Just to note in the future, that you should give a more descriptive topic title, a good title for this post would be "Ordering files in a directory" or "Alphabetical Directory Listing".

http://www.codingforums.com/postguide.htm

missing-score
04-27-2004, 07:36 PM
looks like mine and sad69's posts were crossed, although it seems we had pretty much the same idea... The only difference is I used sort and he used asort().

cragllo
04-27-2004, 07:41 PM
Thank you guys, I'll try them both to see which one is best, I have only started learning PHP a few days ago, so I dont know much yet, I'm glad i found this forum,
Thanx again.

missing-score
04-27-2004, 07:48 PM
They will do exactly the same thing for what you want... They way we loop through each file is slightly different so its really up to your prefrence :)

cragllo
04-27-2004, 08:13 PM
The second code worked, the first one did not. wierd, dunno y.

Im glad I found it tho. Its good how you can just add files and it will find them for you, amazing if you ask me. Thanks guys! Its for a hit counter service im doing.

have a look at what I was trying to do if you want:
http://www.hostultra.com/~sponkindustries/counter/index.php?p=styles

missing-score
04-27-2004, 08:41 PM
oops :o, simple typing error... glad you got it working anyway :)

cragllo
05-07-2004, 10:36 PM
Ok guys, thank you for helping. But... I need to change the code again, I need it so it only shows the image files without searching the folders. Also to still go A-Z,
Anyone? :thumbsup:

sad69
05-07-2004, 10:51 PM
Hmm.. I don't know too much about this stuff, so I dunno if you can just ask if a file is an image file...

You may need to clarify what you consider to be an image file (by extension or something..). Once you've got that figured out, insert those statements where I've got the red colour..



$files = Array();
$path = "digits";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (is_dir($path."/".$file)) {
if (!is_file($path."/".$file)) {
if (($file!=".") && ($file!="..") && (preg_match(/gif/i,$file) && (preg_match(/jpg/i,$file)) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
}


Like I say, I don't know too much about this kind of stuff, so there may be a better way...

Hope that helps,
Sadiq.

cragllo
05-07-2004, 10:57 PM
It didnt work, It's stull searching for the folder, and the image wont show up if the folder aint there. Thanx neway

missing-score
05-07-2004, 11:02 PM
Try this:



$files = Array();
$path = "digits";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (is_dir($path."/".$file)) {
if (!is_file($path."/".$file)) {
if (($file!=".") && ($file!="..") && preg_match("/\.(gif|jpg|jpeg|bmp)/is", $file)) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
}

cragllo
05-07-2004, 11:10 PM
that did not work either, would it be easier to do a new code? i think its because on the code, it's on a diffrent folder that the last time.

missing-score
05-07-2004, 11:13 PM
shouldnt make any difference, i think this may work:

Just replace this line in the code:


if (($file!=".") && ($file!="..") && preg_match("/(\.gif|\.jpg|\.jpeg|\.bmp+)/is", $file)) {

sad69
05-07-2004, 11:14 PM
Did you change the $path variable to hold the right folder name?

Sadiq.

cragllo
05-07-2004, 11:16 PM
yes i did,

it's still not working,

could someone tell me a diffrent code, to see if that works?

missing-score
05-07-2004, 11:20 PM
hmm... well the code is going to be pretty much the same... so not really, make sure you have images in the folder... I once spend 4 hours "debugging" a script that was meant to load information from a database, and then realised there was nothing in the database to load.

cragllo
05-07-2004, 11:24 PM
that must have realy p'd u off, yea there are images in the folder. could you tell me the code anyway? please?

missing-score
05-07-2004, 11:28 PM
there isnt a different code... theres variations on the code (ever so slight that they dont really do anything).

What are you currently getting when you run the script?

cragllo
05-07-2004, 11:36 PM
nothing is showing up
just a blank bit
http://www.hostultra.com/~sponkindustries/clipart2.php

firepages
05-08-2004, 03:51 AM
if you have PHP>=4.3 you can use glob..



<?
$path='/home/user/images';
chdir($path);
$bits = glob( "$path/*.*" );
foreach($bits as $bobs){
if(@getimagesize($bobs)){
echo $bobs.'<br />';
}
}
?>

cragllo
05-08-2004, 11:39 AM
nope, still nothing showing up.

missing-score
05-08-2004, 01:00 PM
hmm.. this makes me think that the directory isnt being read properly.... Can you post the entire code you are using? With your directorys inserted?

cragllo
05-08-2004, 01:32 PM
$files = Array();
$path = "images/clipart";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (is_dir($path."/".$file)) {
if (!is_file($path."/".$file)) {
if (($file!=".") && ($file!="..") && preg_match("/\.(gif|jpg|jpeg|bmp|png)/is", $file)) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
}



All I want to do is have the images in that folder show up without adding them to the pages every time. So if i put a new image in the folder it will show up without having to udate the page. Do you see what im trying to do?

missing-score
05-08-2004, 01:56 PM
I think you may have put your ! in the wrong place...




$files = Array();
$path = "images/clipart";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (!is_dir($path."/".$file)) { // changed this line \ Changed the line with the !
if (is_file($path."/".$file)) { // changed this line /
if (($file!=".") && ($file!="..") && preg_match("/\.(gif|jpg|jpeg|bmp|png)/is", $file)) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
}

Íkii
05-08-2004, 06:59 PM
In furtherance to firepages note on using the newish glob function, you can also match multiple files through using the GLOB_BRACE setting - eg


$files = glob("/home/site/pub/images/{*.gif,*.jpg,*.png}", GLOB_BRACE);
foreach($files as $file) {


Note - you may need to add uppercase versions of the extensions to the braced array. Hope you get the pathing issues sorted anyway.

cragllo
05-08-2004, 09:12 PM
Thank you to everyone who helped. This code finnaly worked.


<?
$files = Array();
$path = "images/clipart";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (!is_dir($path."/".$file)) {
if (is_file($path."/".$file)) {
if (($file!=".") && ($file!="..") && preg_match("/\.(gif|jpg|jpeg|bmp|png)/is", $file)) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<img src=\"images/clipart/".$file."\">\n";
}
?>

It was the ! that made all the diffrence, as you can tell i have no clue about what I was doing, anyway thank you all.

missing-score
05-08-2004, 11:08 PM
not a problem... i think the moral here is always to post the code you are using :)

cragllo
05-08-2004, 11:21 PM
Yes, I think ill have to next time. Thank you, I have changed the code a bit, because some images were not showing up i took out this bit.


&& preg_match("/\.(gif|jpg|jpeg|bmp|png)/is", $file)

off the 8th line, i left in the )) { bit in so it still works. Ive used it on lots of pages, tis code has been a great help.

missing-score
05-08-2004, 11:33 PM
yup, you wont need that anymore :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum