Jump to content

Need help assigning date to image


melloorr

Recommended Posts

Hey everyone. I have been trying to assign dates to image so I could sort them, and also display the date they were uploaded. But I need some help. I have a script that uploads an image to a file directory, and also stores the file directory and the upload date in a database. And I also have a script that displays the images in the file directory but I cannot find a way to assign the right date to the right image.

Here is the script that displays the images:

 

if (isset($_POST['submit'])) {
// fill an array with all items from a directory
$dir = "thumbs/";

$handle = opendir($dir);
while ($file = readdir($handle)) {
 $files[] = $file;

 // This removes the full stops from the array
unset($files[0]);
unset($files[1]);
                
}    
            foreach($files as $fileimg)
{

	echo "<img src='". $dir . $fileimg ."'/>\n";
                echo "$date";
}
        
}

 

and here is the script for calling the date from the database:

 

$query = mysql_query("SELECT imgaddress, uploaddate FROM thumbs WHERE imgaddress='" . $dir . $file . "'");

                if (mysql_num_rows($query) == 1) {
                     $row = mysql_fetch_assoc($query);

                     $date = $row['uploaddate'];

 

If I use the database statement before the foreach, then it does not get the right file directory, but if I call it inside the foreach, then I cannot use krsort() to sort the array.

 

Any help would be greatly appreciated, thanks

Link to comment
Share on other sites

Why are you trying to use both file handler functions AND the database? If you want to use file handler functions, then use those to read the date of the file. Or, better yet, just use the database values to display the output. If you have records for all the images, there is no need to read the files from the directory!

Link to comment
Share on other sites

if (isset($_POST['submit']))
{
    $query = "SELECT imgaddress, uploaddate FROM thumbs ORDER BY uploaddate";
    $result = mysql_query($query);

    while($row = mysql_fetch_assoc($result))
    {
        echo "<img src='{$row['imgaddress']}'/>\n{$row['uploaddate']}<br>\n";
    }
}

Link to comment
Share on other sites

Why are you trying to use both file handler functions AND the database? If you want to use file handler functions, then use those to read the date of the file. Or, better yet, just use the database values to display the output. If you have records for all the images, there is no need to read the files from the directory!

 

Now I feel pretty stupid because it seems so obvious  :-\

 

Thanks you for all the help  :)

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.