Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.