Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.