Hi all,

I was wondering if anyone could help with this one. I've devloped an upload form where my customers can upload their own pictures to use in my personalised gifts business, you could call it 'Create Your Own' Range of products.

I want to be able to store the file on the server which I have working but I would like each file name to be unique so I was wondering if I could add something like the date or time to the beginning of the filename e.g. 2008027_filename.jpg (filename being the name of the file being uploaded).

Here is the code I have so far which works (it uploads the file to the server and keeps a record of the upload in a database table):

<?php
//connect to database. Username and password need to be changed 
mysql_connect("**************", "**********", "**************"); 

//Select database, database_name needs to be changed 
mysql_select_db("*********"); 

//This is the directory where images will be saved 
$target = "e:/domains/p/personally-yours.co.uk/user/htdocs/development/images/upload/"; 
$target = $target . basename( $_FILES['homeImg']['name']); 

//This gets all the other information from the form 

$title=$_POST['title']; 
$paragraph=$_POST['paragraph']; 
$homeImg=($_FILES['homeImg']['name']);

//Writes the information to the database 

mysql_query("insert into home (title, paragraph, homeImg) values('$title', '$paragraph', '$homeImg')");

//Writes the photo to the server 
if(move_uploaded_file($_FILES['homeImg']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file has been uploaded"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
// Close database connection
mysql_close(); 
?>

hope you can help.

Regards

Recommended Answers

All 2 Replies

You can simply use the function time().

<?php
//connect to database. Username and password need to be changed 
mysql_connect("**************", "**********", "**************"); 

//Select database, database_name needs to be changed 
mysql_select_db("*********"); 

//This is the directory where images will be saved 
$target = "e:/domains/p/personally-yours.co.uk/user/htdocs/development/images/upload/"; 
$target = $target . time() . '_' . basename( $_FILES['homeImg']['name']); 

//This gets all the other information from the form 

$title=$_POST['title']; 
$paragraph=$_POST['paragraph']; 
$homeImg=($_FILES['homeImg']['name']);

//Writes the information to the database 

mysql_query("insert into home (title, paragraph, homeImg) values('$title', '$paragraph', '$homeImg')");

//Writes the photo to the server 
if(move_uploaded_file($_FILES['homeImg']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file has been uploaded"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
// Close database connection
mysql_close(); 
?>

Thanks for that, it nwo works perfectly. Thank You.

You can simply use the function time().

<?php
//connect to database. Username and password need to be changed 
mysql_connect("**************", "**********", "**************"); 

//Select database, database_name needs to be changed 
mysql_select_db("*********"); 

//This is the directory where images will be saved 
$target = "e:/domains/p/personally-yours.co.uk/user/htdocs/development/images/upload/"; 
$target = $target . time() . '_' . basename( $_FILES['homeImg']['name']); 

//This gets all the other information from the form 

$title=$_POST['title']; 
$paragraph=$_POST['paragraph']; 
$homeImg=($_FILES['homeImg']['name']);

//Writes the information to the database 

mysql_query("insert into home (title, paragraph, homeImg) values('$title', '$paragraph', '$homeImg')");

//Writes the photo to the server 
if(move_uploaded_file($_FILES['homeImg']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file has been uploaded"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
// Close database connection
mysql_close(); 
?>
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.