Jump to content

File upload - Random file name


-markie-

Recommended Posts

How would i do a random file name for my upload script

 

$type = $_FILES['uploadedfile']['type'];
// Where the file is going to be placed 
$target_path ="/***/***/public_html/****/lofslidernews/images/";

/* Add the original filename to our target path.  
Result is "/images/uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$uploadedfile2 = $_FILES['uploadedfile']['name'];

if (empty($quicktitle) || empty($maintitle ) || empty($description ) || empty($uploadedfile)) 
   {
       $_SESSION["promoerror"] = "Please Select an image and enter a Quick Title, Main Title and Description!" ;
       header("Location: promo.php"); //This sets the redirection information
       //echo "Going to login.php, username or password is empty";
       exit(); //Ends the script and redirects to above
   }


$query = "INSERT INTO promotion (title, maintitle, description, image) VALUES ('$quicktitle','$maintitle','$description', '$uploadedfile2')";



$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 


if ( ( $type != "image/jpeg") && ($type != "image/gif") ) 

{
die ("That format is not allowed");
} 
else
{
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
header("Location: promo.php");
$_SESSION["promoerror"] = "Added Successfully!";

}

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

Something like this

 

$uploadedfile2 = $_FILES['uploadedfile']['name'];
$path_part = pathinfo($uploadedfile2);
$uploadedfile2 = uniqid(md5_file($uploadedfile2)).".".$path_part['extension'];

should work,

it really depends on what's being uploaded, MD5 will give the same result if  the same file is used, the above will add some randomness to that, normally i would add the username/id or something else that's unique.

 

Thanks for your help so far. I added that and it gave the following error:

[04-Jan-2011 17:25:45] PHP Warning:  md5_file(test.jpg) [<a href='function.md5-file'>function.md5-file</a>]: failed to open stream: No such file or directory in /home/*****/public_html/*****/addpromoitem.php on line 35

Hmm still not changing the actual file name

 

// create query 
$query = "SELECT * FROM promotion";

$quicktitle = $_POST["quicktitle"];
//Check its ok - if not then add an error message to the error string
if (empty($quicktitle)) 
    $errorString = $errorString."<br>Please enter a Quick Title."; 

$maintitle = $_POST["maintitle"];
//Check its ok - if not then add an error message to the error string
if (empty($maintitle)) 
    $errorString = $errorString."<br>Please enter a Main Title.";  

$description = $_POST["description"];
//Check its ok - if not then add an error message to the error string
if (empty($description)) 
    $errorString = $errorString."<br>Please enter a Description.";

$type = $_FILES['uploadedfile']['type'];
// Where the file is going to be placed 
$target_path ="/home/g*****/public_html/****/lofslidernews/images/";

/* Add the original filename to our target path.  
Result is "/images/uploads/filename.extension" */


$uploadedfile2 = $_FILES['uploadedfile']['name'];

$target_path = $target_path . $uploadedfile2;

$path_part = pathinfo($uploadedfile2);
$uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension'];

if (empty($quicktitle) || empty($maintitle ) || empty($description ) || empty($uploadedfile2)) 
   {
       $_SESSION["promoerror"] = "Please Select an image and enter a Quick Title, Main Title and Description!" ;
       header("Location: promo.php"); //This sets the redirection information
       //echo "Going to login.php, username or password is empty";
       exit(); //Ends the script and redirects to above
   }


$query = "INSERT INTO promotion (title, maintitle, description, image) VALUES ('$quicktitle','$maintitle','$description', '$uploadedfile2')";



$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 


if ( ( $type != "image/jpeg") && ($type != "image/gif") ) 

{
die ("That format is not allowed");
} 
else
{
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
header("Location: promo.php");
$_SESSION["promoerror"] = "Added Successfully!";

}

//header("Location: loggedon.php?username=".mysql_insert_id() );
// (6) close connection 
    mysql_close($connection); 
?>

oh, i see.

 

$uploadedfile2 = $_FILES['uploadedfile']['name'];

//$target_path = $target_path . $uploadedfile2;

$path_part = pathinfo($uploadedfile2);
$uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension'];

$target_path = $target_path . $uploadedfile2;

oh, i see.

 

$uploadedfile2 = $_FILES['uploadedfile']['name'];

//$target_path = $target_path . $uploadedfile2;

$path_part = pathinfo($uploadedfile2);
$uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension'];

$target_path = $target_path . $uploadedfile2;

didn't i say that ?

 

move $target_path under uploadedfile2

and change to

$target_path = $target_path . $uploadedfile2;

 

oh i did!

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.