Jump to content

renaming files


BOS45430

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

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.