BOS45430 Posted August 6, 2007 Share Posted August 6, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/63582-renaming-files/ Share on other sites More sharing options...
The Little Guy Posted August 6, 2007 Share Posted August 6, 2007 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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63582-renaming-files/#findComment-316883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.