Jump to content

Unique Name To Images


madness69

Recommended Posts

Hello there, i have a image upload that its not perfect bu it already enought for what i need, but there is a problem, the name of the image that i uploaded is always the same, and for some reason i upload a image whit same name it will overwright the image, i need to put my image upload code to give each image a unique name, here is the code to take a look.

What is missing to accomplishe this?

Best reagards

 

$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$err=$_FILES['image']['error'];
if($err==0)
{

move_uploaded_file($tmp, "../upload/$name");


}
$name=$_FILES['image2']['name'];
$tmp=$_FILES['image2']['tmp_name'];
$err=$_FILES['image2']['error'];
if($err==0)
{
move_uploaded_file($tmp, "../upload/$name");


}
$img=$_FILES["image"]["name"];
$img2=$_FILES["image2"]["name"];

Link to comment
https://forums.phpfreaks.com/topic/269576-unique-name-to-images/
Share on other sites

Instead of taking $name from the file info, give it a new name. uniqid produces a unique string.

 

In practice how would i insert uniqueid to my code?

 

$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$err=$_FILES['image']['error'];
if($err==0)
{

move_uploaded_file($tmp, "../upload/$name");


}
$name=$_FILES['image2']['name'];
$tmp=$_FILES['image2']['tmp_name'];
$err=$_FILES['image2']['error'];
if($err==0)
{
move_uploaded_file($tmp, "../upload/$name");


}
$img=$_FILES["image"]["name"];
$img2=$_FILES["image2"]["name"];

Hello there, i have a image upload that its not perfect bu it already enought for what i need, but there is a problem, the name of the image that i uploaded is always the same, and for some reason i upload a image whit same name it will overwright the image, i need to put my image upload code to give each image a unique name, here is the code to take a look.

What is missing to accomplishe this?

Best reagards

 

$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$err=$_FILES['image']['error'];
if($err==0)
{

move_uploaded_file($tmp, "../upload/$name");


}
$name=$_FILES['image2']['name'];
$tmp=$_FILES['image2']['tmp_name'];
$err=$_FILES['image2']['error'];
if($err==0)
{
move_uploaded_file($tmp, "../upload/$name");


}
$img=$_FILES["image"]["name"];
$img2=$_FILES["image2"]["name"];

 

try to replace the actual name to a certain algorithm such as userid the time and date so the filename will be unique

 

code should be like this

 

 

 

$UploaderID=1 // assume the value of the uploader in the database is 1

$GeneratedFilename = $Uploader + date("Ymd"); // then this code will generate the filename.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.