Page 1 of 1

when users uploaded images in my host automatic change the..

Posted: Sun Aug 31, 2008 5:28 pm
by nariman-gh
Hi
when users uploaded images in my host automatic change the name of file if exist .
i want the file to keep the same name, and new uploaded image have a new name .
i want when users upload images do not overwrite in same name.
for ex. in my host > pic.jpg and users uploaded > pic.jpg , change the name of new uploaded.
Please Help Me.....
My php script :

Code: Select all

<?php
//wwww.*****.com)
if(!is_dir("./img")) mkdir("./img", 0755); 
//move the uploaded file
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./img/".$_FILES['Filedata']['name']);
chmod("./img/".$_FILES['Filedata']['name'], 0777);
?>

Re: when users uploaded images in my host automatic change the..

Posted: Sun Aug 31, 2008 5:42 pm
by Cut

Code: Select all

 
<?php
//wwww.*****.com)
if(!is_dir("./img")) mkdir("./img", 0755);

Code: Select all

//set filename, and change if already exists.
$name = $_FILES['Filedata']['name'];
while(file_exists("./img/".$name)) {
 $name = $_FILES['Filedata']['name'].$mt_rand;
}

Code: Select all

 //move the uploaded file
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./img/".$name);
chmod("./img/".$name, 0777);
?> 
 
I added the middle bit, and changed the third to accommodate it. It checks if the file exists, and if it does, appends a random number on the name until a file with the current name is not found.

Note that I haven't tested this.

Re: when users uploaded images in my host automatic change the..

Posted: Sun Aug 31, 2008 5:55 pm
by nariman-gh
Not Working
:cry: