Jump to content

Need to create text file for photo descriptions


bschultz

Recommended Posts

I found a photo slideshow script at http://www.zinkwazi.com/wp/scripts/.  If I want photo descriptions, I have to create a text file with the following format:

 

    greg.jpg;Me

    dog.png;My dog John

    cat;

    tux.jpg;My friend Tux

 

I would like to create that text file on the fly.  I can read all image files in the directory with this...

 

<?php
$dir = opendir ("/my/directory/here");
        while (false !== ($file = readdir($dir))) {
                if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) {
                    echo "$file <br />";
                }
        }
?>

 

I also know how to write to a text file...but how can I take the file name garnered from the above code...and create a form with an editable description that can be added?

 

I tried

 

echo "$file[0] <br />";
echo "$file[1] <br />";

 

But it echoed this...

s

c

s

c

b

l

 

Thanks!

Link to comment
Share on other sites

I should add that eventually, I'll need to add file uploads (which isn't hard)...and adding to / editing the text file once new pics are added.  So, when you load the "edit" file for photo descriptions, it will need to keep the value that's already added for that file name.

Link to comment
Share on other sites

Hm...I'm working on a similar thing. I decided to not provide any of the features you are building. I would be interested in seeing the code once you are done.

 

To make a form, you can't possibly echo $file[0]. $file is a string, $file[0] means first character of $file.

 

You have to echo <form....><input type="text"... /></form>

 

Am I missing something in your question, or are you really asking about basics of HTML forms?

Link to comment
Share on other sites

I didn't word that very well.  I need to load the file names into an array...and then echo the result in the various form fields using the array value.  I used

 

<?php
$dir = opendir ("/my/directory/here");
        while (false !== ($file = readdir($dir))) {
                if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) {
                    echo "$file[0] <br />";   ////// this is where I used this code...not in the html form
                    echo "$file[1] <br />";  ////// this is where I used this code...not in the html form
                }
        }
?>

 

to test this...and it didn't work.

 

Link to comment
Share on other sites

Oh OK.

 

<?php
$dir = opendir ("/my/directory/here");
$files = array();
while (false !== ($file = readdir($dir))) {
  if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) {
    $files[] = $file;
  }
}
foreach ($files as $file_name) { echo $file; }
?>

Link to comment
Share on other sites

While that printed the directory names...it didn't put them in an array that could be used in the form field's

 

<?php
$dir = opendir ("."); //code changed to use current directory
$files = array();
while (false !== ($file = readdir($dir))) {
  if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) {
    $files[] = $file;
  }
}
foreach ($files as $file_name) { echo $file[0]; echo "<br />"; echo $file[1]; } //should this be $file_name instead of $file?  It didn't work either, but you've never used the foreach value.
?>

Link to comment
Share on other sites

<?php

$dir = opendir ("."); //code changed to use current directory

$file_names = array();

while (false !== ($file = readdir($dir))) {

  if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) {

    $file_names[] = $file;

  }

}

?>

 

Link to comment
Share on other sites

I'm starting to get mad...!!!

 

This

 

<?php
$dir = opendir ("."); //code changed to use current directory
$file_names = array();
while (false !== ($file = readdir($dir))) {
  if (strpos($file, '.gif',1)||strpos($file, '.jpg',1)||strpos($file, '.JPG',1) ) {
    $file_names[] = $file;
  }
echo $file_names[0]; echo "<br />"; echo $file_names[1];
}
?>

 

displayed this

screened product for sale.JPG

screened product for sale.JPG

screening black dirt.JPGscreened product for sale.JPG

screening black dirt.JPGscreened product for sale.JPG

screening black dirt.JPGscreened product for sale.JPG

screening black dirt.JPG

Link to comment
Share on other sites

Why are people still using the opendir & readdir loops to get an array of files in a directory when the glob function does it for you.

<?php
$file_names = glob('./*.{jpg,JPG,gif}',GLOB_BRACE);
echo '<pre>' . print_r($file_names,true) . '</pre>';
?>

 

Ken

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.