Kaylub Posted January 9, 2010 Share Posted January 9, 2010 Hello, I'm kind of stupid when it comes to php and I need a tiny bit of help. I've got a form/php setup that allows a user to upload an image to my server. The upload is working well, and the code is functioning as is, however, I would like to add a timestamp to the file name before it is saved to the server, as well as making sure the time stamped file name is turned into a variable I can recall on a later php page. This is all in an effort to create unique filenames for uploaded content. Here is my current form code: <FORM ENCTYPE="multipart/form-data" ACTION="uploadck.php" METHOD=POST> Upload this file: <INPUT NAME="file_up" TYPE="file"> <INPUT TYPE="submit" VALUE="Send File"></FORM> and here is the php for that form: <? $file_upload="true"; $file_up_size=$_FILES['file_up']; echo $_FILES[file_up][name]; if ($_FILES[file_up]>1000000){$msg=$msg."Your uploaded file size is more than 1MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>"; $file_upload="false";} if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>"; $file_upload="false";} $file_name=$_FILES[file_up][name]; $add="upload/$file_name"; // the path with the file name where the file will be stored, upload is the directory name. if($file_upload=="true"){ if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){ echo "Congratulations, file succesfully uploaded"; }else{echo "Failed to upload file Contact Site admin to fix the problem";} }else{echo $msg;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/ Share on other sites More sharing options...
mikesta707 Posted January 9, 2010 Share Posted January 9, 2010 you can get a timestamp using the time() function. $timestamp = time(); Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992014 Share on other sites More sharing options...
Kaylub Posted January 9, 2010 Author Share Posted January 9, 2010 Thanks for the quick reply. I've got that much figured out ( $timestamp= time(); ) ...but I can't seem to apply the outputed timestamp to my file name/subsequent variable. Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992018 Share on other sites More sharing options...
mikesta707 Posted January 9, 2010 Share Posted January 9, 2010 instead of $add="upload/$file_name"; you could just do $add="upload/$timestamp"; [code=php:0] Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992023 Share on other sites More sharing options...
Kaylub Posted January 9, 2010 Author Share Posted January 9, 2010 Right on, Thanks Mikesta That's what I had in mind. However, is there any way to combine both the timestamp and the file name into one string? (I know this is incorrect, but something along the lines of) $uniquename = $timestamp+$file_name; then later on $add="upload/$uniquename"; Is that possible? What's the proper syntax/format? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992027 Share on other sites More sharing options...
mikesta707 Posted January 9, 2010 Share Posted January 9, 2010 ahh ok. Thats called concatenating, and in PHP, the concatenation operator is the dot (.) so what you are looking for is $uniquename = $timestamp.$file_name; you were on the right track the, the plus sign is the concatenation operator for other languages (like javascript) Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992030 Share on other sites More sharing options...
Kaylub Posted January 9, 2010 Author Share Posted January 9, 2010 Nice Mikesta, you're a god send. I'd like to reitterate that I started off my post by saying I was pretty stupid when it comes to php . Heh, thanks for being patient with me. I'll mock it up and post a reply real soon with results. Thanks again Kaylub Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992032 Share on other sites More sharing options...
mikesta707 Posted January 9, 2010 Share Posted January 9, 2010 No prolem, thats what i'm here for. If the problem is fixed, you can also mark the topic as solved by clicking the "mark solved" button at the bottom of the thread Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992034 Share on other sites More sharing options...
Kaylub Posted January 9, 2010 Author Share Posted January 9, 2010 Nice, I checked it out and my files are saving with timestamps server side now. Works wonderfully. I'll be sure to remember your name. Now, on to design! Peace for now Quote Link to comment https://forums.phpfreaks.com/topic/187890-need-help-adding-a-timestamp-to-my-filenamevariable/#findComment-992036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.