Jump to content

Random Name


Julian

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/42017-random-name/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203766
Share on other sites

<?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

Link to comment
https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203906
Share on other sites

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/42017-random-name/#findComment-203928
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.