Jump to content

Recommended Posts

Ok i have created a picture upload script.  THe problem is...is want to the pics to be renamed then stored.  I dont want any pic to have to same filename as any other ones.  How can I do this?  I thought about using rand but then there is a small chance that it could actually rename a file to something that already has that file name.  What about that autoincrement thing in mySql?  would that work?  please give me an example of how to do this.  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/63582-renaming-files/
Share on other sites

I use something like this for multiple file uploads:

<?php
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image"]["tmp_name"][$key];

# Create a new file name

$string = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = '';
for($i=0;$i<45;$i++){
	$pos = rand(0,36);
	$str .= $string{$pos};
}
$ext = getExt($_FILES["image"]["name"][$key]);
        $name = time().$str.$ext;

# End new file name

if(mysqli_query($db,"INSERT INTO friends_images 
(
	`ownerID`,
	`addDate`,
	`fileName`
) VALUES (
	'{$_SESSION['id']}',
	'$date',
	'$name'
)")or die(mysqli_error($db))){
$fullDir = "../../images/users/full/";
move_uploaded_file($tmp_name, $fullDir.$name);
list($width) = getimagesize($fullDir.$name);
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/63582-renaming-files/#findComment-316883
Share on other sites

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.