a1amattyj Posted January 6, 2008 Share Posted January 6, 2008 Hi, Im currently in the process of coding a image upload script. However, i would like it to rename the file to somethign random. Here is my code: <?php $imagename = $_FILES["file"]["name"]; $image2 = "<a href=\"http://www.optclan.info/upload/$imagename\">$imagename</a>"; ?> <?php if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < "$maxsize") && ($_FILES["file"]["size"] > "$minsize")) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo "$txt30"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "$txt32"; echo "<br /><br /><center>"; echo "<img src=\"http://www.$txt0/upload/$imagename\" alt=\"Your Image\" max-width=400 max-height=400 />"; echo "</center><br /><br />"; echo "Your Image is named: "; echo $image2; echo "<br />"; echo "File Size: " . ($_FILES["file"]["size"] / 2024) . " Kb<br /><br /><br />"; echo "<center>$txt33<br />"; echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">[url=http://$txt0][img]http://$txt0/upload/"; echo "$imagename"; echo "[/img]"; echo "[/url]"; echo "</textarea><br /><br /><br />"; echo "<center>$txt34<br />"; echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">http://$txt0/upload/"; echo "$imagename"; echo "</textarea><br /><br /><br />"; } } } else { echo "$txt31"; } ?> So where too would i put the rename function? rename(oldname,newname) Also, i dont know how to code a random name with numbers and images which is 10 charcs long. Thank you for all! Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/ Share on other sites More sharing options...
JJohnsenDK Posted January 6, 2008 Share Posted January 6, 2008 use the rand() function to create your random code: http://dk2.php.net/manual/da/function.rand.php Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/#findComment-431935 Share on other sites More sharing options...
papaface Posted January 6, 2008 Share Posted January 6, 2008 Change: move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); To: move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . md5(rand()) . $_FILES["file"]["name"]); That will give you a random name. Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/#findComment-431936 Share on other sites More sharing options...
a1amattyj Posted January 6, 2008 Author Share Posted January 6, 2008 Change: move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); To: move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . md5(rand()) . $_FILES["file"]["name"]); That will give you a random name. Hi papa, thanks it worked. Now, it uploads the file with a random name, how do i get the specific random name so the user knows what it is called? Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/#findComment-431946 Share on other sites More sharing options...
papaface Posted January 6, 2008 Share Posted January 6, 2008 <?php $imagename = md5(rand()) . $_FILES["file"]["name"]; $image2 = "<a href=\"http://www.optclan.info/upload/$imagename\">$imagename</a>"; ?> <?php if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < "$maxsize") && ($_FILES["file"]["size"] > "$minsize")) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("upload/" . $imagename)) { echo "$txt30"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $imagename); echo "$txt32"; echo "<br /><br /><center>"; echo "<img src=\"http://www.$txt0/upload/$imagename\" alt=\"Your Image\" max-width=400 max-height=400 />"; echo "</center><br /><br />"; echo "Your Image is named: "; echo $image2; echo "<br />"; echo "File Size: " . ($_FILES["file"]["size"] / 2024) . " Kb<br /><br /><br />"; echo "<center>$txt33<br />"; echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">[url=http://$txt0][img]http://$txt0/upload/"; echo "$imagename"; echo "[/img]"; echo "[/url]"; echo "</textarea><br /><br /><br />"; echo "<center>$txt34<br />"; echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">http://$txt0/upload/"; echo "$imagename"; echo "</textarea><br /><br /><br />"; } } } else { echo "$txt31"; } ?> Like that. Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/#findComment-431972 Share on other sites More sharing options...
kratsg Posted January 6, 2008 Share Posted January 6, 2008 $randname = md5(rand()) . $_FILES["file"]["name"]); move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $randname; Stored in a variable, you could save it in a session, stick it in a cookie, stored in a database logged with the user's IP address, w/e you want xD Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/#findComment-431975 Share on other sites More sharing options...
a1amattyj Posted January 6, 2008 Author Share Posted January 6, 2008 Thnx, worked Link to comment https://forums.phpfreaks.com/topic/84759-solved-rename-function/#findComment-431991 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.