Page 1 of 1

[SOLVED][PHP] Showing uploaded files by date

Posted: Wed Dec 30, 2009 7:22 am
by arbitter
Hello

I have finaly managed to make my photo-uploader work. The next step is to be able to show all uploaded photo's by month. So If I would upload a photo today; it would go in the folder 'December 2009', under the 'photo' tab.
Unfortunately I don't even know how to automaticaly get uploaded photos on my webpage. Momentarily they just go in the folder 'uploads'. Do I have to make subfolders right away, so that pictures added in december '09 go in the folder uploads/december '09? And how do I do that?
And how do I show those pictures?

Probably a big question, but I'll do with a good link too if that's easier... I've googled it and looked on some forums but havnt found what i've been looking for.

Re: [PHP] Showing uploaded files by date

Posted: Wed Dec 30, 2009 12:25 pm
by AbraCadaver
When an image is uploaded check to see if the current month year: date() directory exists: file_exists(). If not, create it: mkdir().

Use move_uploaded_file() to move the uploaded image to that directory.

To show the images, get a list of all of them from the directory using glob(), then loop through them: foreach() and display with the standard HTML img tag.

Re: [PHP] Showing uploaded files by date

Posted: Wed Dec 30, 2009 12:32 pm
by jayshields

Code: Select all

filemtime()

Re: [PHP] Showing uploaded files by date

Posted: Wed Dec 30, 2009 4:23 pm
by arbitter
As for displaying images automatically after uploading; that works fine after some more searching the web after glob() and foreach(). fitted a maximum width too.
Going to try and fix the date-problem now.

Thank you for the fast answers :)

Re: [PHP] Showing uploaded files by date

Posted: Thu Dec 31, 2009 9:10 am
by arbitter
Okay first a small question;
how can you give an image a maximum width? Because if I just give width='70%' also smaller images get pulled out so they are '70%'. And Id like only images that are bigger than 70% of the screen to be cropped to that 70%

So the filemtime() works, but I really don't know how to get the month and year out of it...

Something like this?

Code: Select all

If (file_exists([color=#FF0000]something with the date[/color])
 { move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . [color=#FF0000]"something to determine month and year"[/color] . $_FILES["file"]["name"]);}
else { mkdir([color=#FF0000]something with the date[/color]) move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . [color=#FF0000]"something to determine month and year"[/color] . $_FILES["file"]["name"]);}

Re: [PHP] Showing uploaded files by date

Posted: Fri Jan 01, 2010 4:18 pm
by arbitter
I found another way to do it;

Code: Select all

<?php 
$uploaddir = date("F")." '".date("y");
            if(is_uploaded_file($_FILES["file"]["tmp_name"]))
            {
                if (file_exists("uploads/" . $uploaddir))
                {   move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $uploaddir."/".$_FILES["file"]["name"]); 
                    }
                else { mkdir("uploads/" . $uploaddir);
                    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $uploaddir."/".$_FILES["file"]["name"]);
                    }
            }
?>