Hi All, I have an image upload that works for my requirements -

But what I am unable to figure out is how to rename the image to the datatime stamp and to keep the file extension
.jpg / .png / .gif / .jpeg etc

I have a variable $datetime
$datetime = date('l jS F Y h:i:s');

here is the code that I am trying to work with -

Any help with be much appreciated -

////////////////////////////////////////////////////////////////////////////////////////////////////////////
				$img1 = $_FILES['jan_image']['name'];
				//Add a security check, to make sure only allowed files are uploaded
				$ext=explode('.',$img1);//explode the image name into an array to get the extension
				$allowed_exts=array('jpg','jpeg','png','bmp','gif');// allowed extensions
				if(in_array(strtolower($ext[1]), $allowed_exts)){//file type is allowed!
					//destination
					$dest="members/$securecode/$sname/images/igal/".$img1;
					//temp image name
					$val= move_uploaded_file($_FILES['jan_image']['tmp_name'], $dest);
				
					chmod($dest, 0644);//make sure you have permissions for the file
					if($val)
					{ //update database and rename
					//rename image to datetime stamp
					rename($dest, 'members/'.$securecode.'/'.$sname.'/images/igal/jan.jpg');
					//set $dest to the new file name so the database entry is correct
					$dest="members/".$securecode."/".$sname."/images/igal/jan.jpg";
						
					mysql_query("update tbl set igimg1='$dest' where id=$lastid");
					}
					}
Member Avatar for diafol
$now = time(); //this gives a unix timestamp (integer)
//OR $now = date('YmdHis') //gives a more readable timestamp, e.g. 20120211121545 (2012-02-11 12:15:45)

$dest="members/$securecode/$sname/images/igal/{$image_name}_{$now}.{image_extension};

This way you don't need to rename.

Obviously you need to exctract the filename and the extension from the uploaded file. You can do this with pathinfo().

$fdata = pathinfo($_FILES['jan_image']['name']);
$imagename = $fdata['filename'];
$image_extension = $fdata['extension'];

Using explode is a little dangerous as a period (.) may be included in the filename (it shouldn't be, but it pays to obviate this eventuality).
Of course you could use other string functions to get the true extension, but why bother with all that nonsense?

So for example:

if uploaded file was: mynewpicture.jpg

the stored file will be mynewpicture_20120211121545.jpg (depending on the $now format used)

Hi thanks for a quick reply -

How can i integrate your solution into my image upload ?

Member Avatar for diafol

Want me to upload it for you too?!

$img1 = $_FILES['jan_image']['name'];

$fdata = pathinfo($_FILES['jan_image']['name']);
$image_name = $fdata['filename'];
$image_extension = $fdata['extension'];

$now = date('YmdHis');

$allowed_exts=array('jpg','jpeg','png','bmp','gif');
if(in_array(strtolower($image_extension), $allowed_exts)){
   $dest="members/$securecode/$sname/images/igal/{$image_name}_{$now}.{image_extension};
   $val= move_uploaded_file($_FILES['jan_image']['tmp_name'], $dest);
   chmod($dest, 0644);//make sure you have permissions for the file
   if($val){
      mysql_query("update tbl set igimg1='$dest' where id=$lastid");
   }
}					}

Why are you updating the last id??
Why not just make an insert with the code above along with the rest of your fields?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.