lisa_85 Posted June 1, 2009 Share Posted June 1, 2009 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 More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 you can always just use time() to append a unixtimestamp to each file. or you can use time() and date() functions to format the date however you want Link to comment https://forums.phpfreaks.com/topic/160482-solved-add-datestamp-to-filename/#findComment-846882 Share on other sites More sharing options...
lisa_85 Posted June 1, 2009 Author Share Posted June 1, 2009 Hi lonewolf, adding a timestamp would be fine. I tried using time() but I ended up uploading an empty file with the timestamp as the filename and nothing else Do you know how I would get it to work with my code? Thanks Link to comment https://forums.phpfreaks.com/topic/160482-solved-add-datestamp-to-filename/#findComment-846888 Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 haven't done much with file uploads, but try this <?php $filen = $_FILES["item_file"]['name']["$j"].time(); //file name Link to comment https://forums.phpfreaks.com/topic/160482-solved-add-datestamp-to-filename/#findComment-846891 Share on other sites More sharing options...
lisa_85 Posted June 1, 2009 Author Share Posted June 1, 2009 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 Link to comment https://forums.phpfreaks.com/topic/160482-solved-add-datestamp-to-filename/#findComment-846931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.