Julian Posted March 9, 2007 Share Posted March 9, 2007 Hi Guys! I have this script to generate a random name when I upload a file to my server: $file_temp_dir = "../fotos"; //Random name $file_name = $_FILES['userfile']['name']; //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); if (($userfile!="none")&&($userfile!="")) { move_uploaded_file($userfile, "$file_temp_dir/$rand_name.$file_ext"); } else { $filename = $HTTP_POST_VARS['imagen']; } What I want to do is reuse this script when I upload 3 or 4 files without adding the script 3 or 4 times. I think if I use as a function but I'm newbie on this. Any help will be appreciated. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/ Share on other sites More sharing options...
Orio Posted March 9, 2007 Share Posted March 9, 2007 So what's the problem with it? Only one thing I noticed: This- $rand_name = rand(0,999999999); Should be- $rand_name .= rand(0,999999999); Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203736 Share on other sites More sharing options...
Julian Posted March 9, 2007 Author Share Posted March 9, 2007 Here's the problem: $file_name = $_FILES['userfile']['name']; How do I do if I want to post different files as userfile2, userfile3? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203750 Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 if you need to post more then one file you need to use an array on the form name='go[]' ok Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203758 Share on other sites More sharing options...
Julian Posted March 9, 2007 Author Share Posted March 9, 2007 I think I'm not explaining right. The form I have upload 5 files to the server, and saves the info (of the file names) into a database. Without the random name I upload the "real" name of the file, but if somebody name the file as one on the server it will replace the one on the server. That's why I'm trying to insert a random name, so it will be hard to create an exact name. <input type="file" name="userfile"> <input type="file" name="userfile2"> <input type="file" name="userfile3">... and so on. How can I use only this script one time to process all the files? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203766 Share on other sites More sharing options...
Julian Posted March 9, 2007 Author Share Posted March 9, 2007 Any hint, guys? I'm hitting my head trying to resolve this.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203901 Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 <?php function randName($file) { $file_temp_dir = "../fotos"; //Random name $file_name = $file['name']; //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); if (($file!="none")&&($file!="")) { move_uploaded_file($file, "$file_temp_dir/$rand_name.$file_ext"); }else { $filename = $HTTP_POST_VARS['imagen']; } } foreach ($_FILE as $key => $val) { randName($val); } ?> Is that what you are looking for? --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203906 Share on other sites More sharing options...
Julian Posted March 9, 2007 Author Share Posted March 9, 2007 Great Frost, Thanks. I think you got it. The only thing missing here is: }else { $filename = $HTTP_POST_VARS['imagen']; In case nothing is uploaded, I just keep the old files on the database. I'm doing this with hidden tag and then $HTTP_POST_VARS the filename. Thanks for the great help! Quote Link to comment https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203928 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.