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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nariman-gh
Forum Newbie
Posts: 2
Joined: Sun Aug 31, 2008 5:24 pm

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

Post 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);
?>
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

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

Post 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.
nariman-gh
Forum Newbie
Posts: 2
Joined: Sun Aug 31, 2008 5:24 pm

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

Post by nariman-gh »

Not Working
:cry:
Post Reply