I have been trying to set up this script for a charity website (animal sanctuary) in which members can upload a photo of their pets to the members log in area.
I have so far managed to get the basic script working but would like to give each image uploaded a unique ID.
Does anyone know how to do this?
This is my script that processes the upload:
<?php
$rank_check = 1;
$page_title = "User Profile";
include "header.inc.php";
print"$openHTML";
//This is the directory where images will be saved
$target = "photos/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$user_id=$_POST['user_id'];
$profile_id=$_POST['profile_id'];
$selected=$_POST['selected'];
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$user_id', '$profile_id', '$selected','$email', '$phone', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "<center>The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory.<p><center>Please click <a href=$base_url/user_photogallery.php?game=$game&user=$username>here</a> to go back to your photo edit page!";
}
else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
First, you could simply use the time() function to get the current time stamp and add that to the image name. You could even prefix it with a random number to reduce the chance of a collision.
Or, you could simply use the mysql_insert_id function to get the ID of the database row you store the image in. That should always be unique for each image.
Then you would get a file named something like 1234567-filename.jpg
The number, generated by time() is a Unix time-stamp, which is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Meaning that it will change ever second.
You could also use microtime() which also returns a Unix time-stamp, but includes microseconds, making it more unique.
You would of course want to add a little validation/error-handling to that, but you get the idea.
This would give you files that started with a unique ID (the row's PK from the database) followed by the actual name of the image. E.g: 123-image_no_123.png .
<?php
// Connect to Database
mysql_connect(localhost, user, pass) or die(mysql_error()) ;
mysql_select_db("thenNnow") or die(mysql_error()) ;
// Writes information on the database
mysql_query("INSERT INTO `thenNnow`.`User_Pic` ( `UP_ID`, `UPic`, `P_ID`, `O_ID`, `D_ID`, `K_ID`, `C_ID`) VALUES ( NULL , '$pic', NULL , NULL , NULL , NULL , NULL) ") ;
$target = "images/". mysql_insert_id() . ".jpg";
$pic=mysql_insert_id();
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "Your file ". $_FILES['photo']['name']. " has been uploaded!";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
The code is actually uploading a picture to the server with the name I desire. For example: A picture that will be place in the row ID number 5 is uploaded to the server with the name 5.jpg
My issue is, There's no record in the database, no name at all. I wrote
$pic=mysql_insert_id();
so the name in the Database is the same as the ID..
Anyway, another thing that I'm no able to do is allowing other formats besides .jpg
Can you please help with this? I've been around it for so long and I'm such a newbie in PHP... Thank you in advance
replace this line in your code :
$target = $target . basename( $_FILES['photo']['name']);
with this :
$target = $target . basename( rand(1, 999).rand(1000,9999 ).rand(1, 999)."_".$_FILES['photo']['name']);
and you can change this random values as you like in the same way you can use id or time() function.
Good Luck