Closed Thread Icon

Topic awaiting preservation: PHP --> looping through directory (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12938" title="Pages that link to Topic awaiting preservation: PHP --&amp;gt; looping through directory (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP --&gt; looping through directory <span class="small">(Page 1 of 1)</span>\

 
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 10-16-2003 19:27

I have a directory called "post" that I need to loop through and open each file.
I can do it if I choose just one file but to grab all the files in the directory and open them up is giving me a headache.

here is what I am doing on the single file:

code:
<?

//open the posts folder
$dir_name = "posts/";
$dir = opendir($dir_name);

$filename = "posts/1066250730posts.dat";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);

$unserContent = unserialize($contents);

?>



The $contents are then echoed in the HTML part of the page.

What I need to do is to open the directory and read each file like this. For some reason it's not working.


Here is how I am saving each file:

code:
<?

//set the identifier of each post
$identifier = time();

//get the time and format it to be included in the post
$myTime = date("F j, Y, g:i a");

//if the datestamp works then do this . . .
if ($datestamp){
//after the post is made, go back to index page
header ("Location: ../index.php");

//save the post as a *.dat file
$filePointer = fopen("../posts/{$identifier}posts.dat", "a+");

//strip off the slashes so that It's doesn't come out like It\'s
$header=stripslashes($header);
$comment=stripslashes($comment);

//put all the info into an array
$myData = array('myTime' => $myTime, 'header'=>$header, 'comment'=>$comment);

//save the data in the array and serialize it
fwrite($filePointer, serialize($myData));

//close it up
fclose($filePointer);
}

?>




Thanks in advance...

Later,

C:\


~Binary is best~

[This message has been edited by CPrompt (edited 10-16-2003).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-16-2003 19:41

Well, this:
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
print "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}


Is straight from the php opendir page. (Obivously $dir = "/your/path/to/the/directory")

A simple mod would be

if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$handle = fopen ($file, "r");
// I'm lazy. Notice the "." so the content just gets added to the existing content
$unserContent .= unserialize(fread ($handle, filesize ($file)));
fclose ($handle);
}
closedir($dh);
}
}

print $unserContent;


does that help?



.:[ Never resist a perfect moment ]:.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 10-17-2003 01:37

Bit:

Yeah that is something that I tried but for some reason it doesn't work. No errors, just doesn't do anything.
Totally confused.

Was thinking about something else with it too. If I loop through each one of these and want each to show up seperate then the
html stuff is going to have to be in the loop too right?

Thanks for the help I am going to keep messing with it and see if I can figure out what is going on.

Later,

C:\


~Binary is best~

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 10-21-2003 14:26

Here are the errors that I get with that code. Got any suggestoins as what is freaking going wrong?




quote:
Warning: fopen(".","r") - Permission denied in c:\phpdev\www\guestbook\pages\journal.php on line 11

Warning: Supplied argument is not a valid File-Handle resource in c:\phpdev\www\guestbook\pages\journal.php on line 12

Warning: Supplied argument is not a valid File-Handle resource in c:\phpdev\www\guestbook\pages\journal.php on line 13

Warning: fopen("..","r") - Permission denied in c:\phpdev\www\guestbook\pages\journal.php on line 11

Warning: Supplied argument is not a valid File-Handle resource in c:\phpdev\www\guestbook\pages\journal.php on line 12

Warning: Supplied argument is not a valid File-Handle resource in c:\phpdev\www\guestbook\pages\journal.php on line 13

Warning: fopen("1066250730posts.dat","r") - No such file or directory in c:\phpdev\www\guestbook\pages\journal.php on line 11

Warning: Supplied argument is not a valid File-Handle resource in c:\phpdev\www\guestbook\pages\journal.php on line 12

Warning: Supplied argument is not a valid File-Handle resource in c:\phpdev\www\guestbook\pages\journal.php on line 13



Later,

C:\


~Binary is best~

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-21-2003 14:30

The only problem you really have there is that the directories or files you're trying ot open, you don't have permission to open, then the two file handler functions after that (what I'm assuming are fread() and fclose()) won't validate since the file variable you're handing them essentially doesn't exist. This repeats three times in the file since you try to do the same thing each time...

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 10-21-2003 15:43

Well maybe I just don't understand the whole "permission" thing. This is just running on my home box with phpDev for practice and testing.

It works if I just call the file directly, but when I try to loop through the file then everything goes haywire.

The file and the directory do exist and they are pointing to it correctly, I know this because like I said, it works as long as I call the file directly.
uggg........

Thanks for the help though.

Later,

C:\


~Binary is best~

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 10-21-2003 20:37

It looks like your code is trying to open things it shouldn't. You need to include something in your code to stop fopen() from trying to open the . and .. which are directories, not files. Something like this:

code:
if($readdir !== '..' && $readdir !== '.') {


I'm not quite sure why it's taking issue with the .dat file. Try building a full path from the root to open the file and see if that helps. Maybe add something like this:

code:
$filename = "posts/1066250730posts.dat";
$file_to_open = $dir . '/' . $filename;
$handle = fopen ($file_to_open, "r");



-Butcher-


[This message has been edited by butcher (edited 10-21-2003).]

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 10-21-2003 21:13
quote:
I'm not quite sure why it's taking issue with the .dat file. Try building a full path from the root to open the file and see if that helps. Maybe add something like this:

code:
--------------------------------------------------------------------------------

$filename = "posts/1066250730posts.dat";$file_to_open = $dir . '/' . $filename;$handle = fopen ($file_to_open, "r");

--------------------------------------------------------------------------------



Butcher, that is exactly what I did to make sure that it could even open the file. It works fine like that. It is just when I try to open the directory and read each file in that directory that everything goes wacky.

I'll try your suggestion and see if that helps any.


Later,

C:\


~Binary is best~

« BackwardsOnwards »

Show Forum Drop Down Menu