Jump to content

Write data to text file loop (Solved!)


Iceman512

Recommended Posts

Hello all,

I am working in a directory on my server called [b]'gallery'[/b]. Within the [b]'gallery'[/b] directory, I have a folder called [b]'images'[/b], a text file called [b]'image_list.txt'[/b] and the php file containing my script.

I want the script to get a list of all files in the [b]'images'[/b] folder and save the list as [b]'image_list.txt'[/b], with a new entry on each line.

[b]For example:[/b][color=red]
image1.jpeg
image2.jpg
image3.png
image4.gif
[/color]
Here is my code so far:
[code]
<?php
$dir=opendir("./images");
while($file=readdir($dir)){
   if ($file!="." and $file!="..") {
      if (is_dir($file)) {
         print "$file (directory)<br>";
      } else {
         print $file."<br />";
      }
   }
}
closedir($dir);

// Start writing the data to the file, 'image_list.txt':
$dest = "image_list.txt";
$Handle = fopen($dest, 'w+');
$Data = $file;
fwrite($Handle, $Data);
fclose($Handle);
}
}
$d->close();
?>
[/code]

Experienced coders will probably cringe at my code!
I have tried several looping methods, but I can only manage a single entry in the text file at best.

Thanks for any help,
Iceman
Link to comment
Share on other sites

I rearranged your code a little

[code]<?php
$dir=opendir("./images");
$dest = "./images/image_list.txt";
$Handle = fopen($dest, 'w');

while($file=readdir($dir)){
  if ($file!="." and $file!="..") {
      if (is_dir($file)) {
        print "$file (directory)<br>";
      } else {
        print $file."<br />";
        fwrite($Handle, $file . "\r\n");
      }
  }
}
closedir($dir);
fclose($Handle);

?>[/code]
Link to comment
Share on other sites

Thank you Barand!

Just had to edit a tiny piece and it works like a charm.
Here's my tested, working code:

[code]
<?php
$dir=opendir("./images");
$dest = "image_list.txt";
$Handle = fopen($dest, 'w');

while($file=readdir($dir)){
  if ($file!="." and $file!="..") {
      if (is_dir($file)) {
        print "$file (directory)<br>";
      } else {
        print $file."<br />";
        fwrite($Handle, $file . "\n"); // Original syntax: fwrite($Handle, $file . "\r\n");
      }
  }
}
closedir($dir);
fclose($Handle);
?>
[/code]

Regards,
Iceman
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.