Jump to content

[SOLVED] Add datestamp to filename


lisa_85

Recommended Posts

Hi everyone, I found a great script for uploading multiple files which I have integrated with my DB but I want to modify it to explode the filename and add a datestamp.  So for filename myfile.doc I want it like myfile_010609.doc.  I had a go but kept getting errors.  My code is:

function upload(){
foreach($_POST as $key=>$value) {
	$$key = $value;
}
mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')");
$lastinsert = mysql_insert_id();

if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
	$GLOBALS['msg'] = ""; //initiate the global message
	for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
		$filen = $_FILES["item_file"]['name']["$j"]; //file name
		$path = 'uploads/'.$filen; //generate the destination path
		mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')");
		if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
			$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
		}
	}
}
else {
	$GLOBALS['msg'] = "No files found to upload"; //Failed message	
}
uploadForm(); //display the main form
}

Please can you let me what I have to do as I am worried about files being uploaded with the same name.  Thanks :-*

Link to comment
https://forums.phpfreaks.com/topic/160482-solved-add-datestamp-to-filename/
Share on other sites

Hi, I was trying to do it much more complicated lol :)  Your way appends the date to the end of the filename like myfile.doc123456789.  I just moved the time to the front and now it works like 123456789_myfile.doc

$filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name

  Thanks :D

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.