Jump to content

put uploaded files in folder per month


arbitter

Recommended Posts

What I want to do is that if you upload a photo in say January '10, the photo goes in the folder 'January '10'. I've managed to get get files uploaded and displayed, but only get the files in a global folder. I was told to use filemtime(), date(), file_exists() and mkdir(). I'm not so bright with the time part though. This is what I've come up with:

 

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

 

To determine filemtime() is "filemtime($_FILES["file"]["tmp_name"])"

This is where 'm stuck, andI don't even know if this works..

Anybody any help please?

Link to comment
https://forums.phpfreaks.com/topic/186849-put-uploaded-files-in-folder-per-month/
Share on other sites

Hi,

 

Filemtime will give you the last time the file was changed, not the time of uploading; if I understand your question well, you'd like to orden them on the time they were uploaded, correct?

 

$uploaddir = date("F")." - ".date("y");

if(is_uploaded_file($_FILES['file']['tmp_name']))
	{
		move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
	}

 

That would move them to a dir named 'Month - year in two digit representation'.

 

Example:

 

January - 10

 

Good luck.

I've done some work but it doesn't quite work yet.

$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']); }
		}

 

But something isn't quite right yet; when I ttry uploading I get warnings about the mkdir() and about move_uploaded_file()

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.