Jump to content

How to randomly name an uploaded image and save the new name into database?


lukelee

Recommended Posts

Hi, guys, I dont know how to change the file name, anyone has any idea? here is my code:

<?php
require_once('db.php');

$address = $_POST[house_address];
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];

copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];

if(($filesize1 && $filesize2 != 0) && ($filesize3==0) && ($filesize4 ==0))
{

$file1 = basename($path1);
$file2 = basename($path2);
    $query = mysql_query("insert into image (title,address,thumb,imagedata) values ('new','$address','1','$file1')");
    $query = mysql_query("insert into image (title,address,thumb,imagedata) values ('new','$address','2','$file2')");

echo "We have received your files, you will be redirecting to previous page in 3 seconds";
}
else {
echo "ERROR..... you cant leave the first and second image empty";
}
?>

Please use $_FILES instead of $HTTP_POST_FILES.

Also, use move_uploaded_file rather than copy.

 

Here is how you can do it, you just need to implement it into your code.

 

$file = $_FILES['filename']['name'];

$new_name = "./path/to/folder/" . md5(uniqid(rand(), true)) . substr($file, strrpos($file, "."));

if(move_uploaded_file($_FILES['filename']['tmp_name'], $new_name)){
//done, you can now use $new_name as the file name for storing in the database
}

Im not sure how to do it randomly but you can always check to see if an image of that name already exists in the database. If it does, send a message to the user to rename file and then unlink the file. If it doesn't, insert the name into the db and save the file. Just make sure the folder your sending the file to has all the names of the existing files in your image db field. That way your files will never be over written. If you only use that folder for uploads, you shouldn't have a problem.

 

 

Here's what i use for image upload.

$imgName = $_FILES['upload_file']['name'];
$query = "SELECT imgName FROM images WHERE imgName = '$imgName'";
$result = mysql_query($query);
$rows = mysql_num_rows($result);

if ($rows > 0) {
     echo "<table border='0' cellpadding='5' cellspacing='1' width='640'>";
     echo "<tr><td align='center'>System Information:</td></tr>";
     echo "<tr><td align='center'><p>An image by that name already exists. Please change the name of your image file.</p></td></tr></table>"; 
     unlink($_FILES['upload_file']['tmp_name']);
     

}  else {
     move_uploaded_file($_FILES['upload_file']['tmp_name'], $target_path) 
}

 

Don't know if that will help you but thought I'd post it anyway.

   

 

Yeah I should point out that there probably isn't anything totally "random", but what I did provide should be pretty darn hard to get a repeat of. If your worried about a repeat, you could make a recursive function to make the new file name. That way the function can check if the file exists, if it does call itself to make another file name until it makes a totally unique name, then it can return the new name.

Please use $_FILES instead of $HTTP_POST_FILES.

Also, use move_uploaded_file rather than copy.

 

Here is how you can do it, you just need to implement it into your code.

 

$file = $_FILES['filename']['name'];

$new_name = "./path/to/folder/" . md5(uniqid(rand(), true)) . substr($file, strrpos($file, "."));

if(move_uploaded_file($_FILES['filename']['tmp_name'], $new_name)){
//done, you can now use $new_name as the file name for storing in the database
}

Thanks for the help, I think I understand your codes, but I still have 2 problems here, here is my codes:

$file = $_FILES['ufile']['name'];
$new_name = "upload/" . md5(uniqid(rand(), true)) . substr($file, strrpos($file, "."));
move_uploaded_file($_FILES['ufile']['tmp_name'][0], $new_name);
if(move_uploaded_file($_FILES['filename']['tmp_name'][0], $new_name)){
$file1 = basename($new_name);	
    $query = mysql_query("insert into image (title,address,thumb,imagedata) values ('new','$address','1','$file1')");
}

 

after i submit an image, the file appear in upload folder is a file something like 'e4792d57727f164a1f28e12e105567bbArray' not an image anymore, another problem is it cant be saved into my database. have i done anything wrong? thanks again.

When a user uploads a file it's stored in a temporary location.

 

Most standard functions return some kind of data.

 

move_uploaded_file() moves a file from the temporary upload location (usually inaccessible by normal users) and moves it to where you want it to be, in most cases a public location.

 

the function usually returns true on success... but if not it returns False

 

 

the if() just checks if it was successful, so you don't run code that you would only want to run if it was successfully moved.

 

that's all

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.