Jump to content

upload filename using rand() & insert filname to sql


jacko_162

Recommended Posts

I want to use the following code to upload images to my server (below code works great)

 

<?php
$rand = rand(1,99999999999999);
$member_id = $_SESSION['SESS_MEMBER_ID'];

if(isset($_FILES['uploaded']['name']))
{
    $target = "gallery/";
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB) 
    $fileName = basename($_FILES['uploaded']['name']);
    $target = $target . $fileName;
$errors = array();

   // Get the extension from the filename.
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

   // Check if the filetype is allowed.
   if(in_array($ext,$allowed_filetypes))
    {
         $errors[] = "The file you attempted to upload is not allowed.";
    }
    
// Now check the filesize.
   if(filesize($_FILES['uploaded']['tmp_name']) > $max_filesize)
    {
         $errors[] = "The file you attempted to upload is too large.";
    }
  
    // Check if we can upload to the specified path.
   if(is_writable($target))
    {
         $errors[] = "You cannot upload to the specified directory, please CHMOD it to 777.";
    }

    //Here we check that no validation errors have occured.
    if(count($errors)==0)
    {

	//Try to upload it.
        if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
        {
            $errors[] = "Sorry, there was a problem uploading your file.";
        }
    }
    
    //If no errors show confirmation message
    if(count($errors)==0)
    {
         echo "<div class='notification success png_bg'>
			<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
			<div>
				The file {$fileName} has been uploaded<br>\n
			</div>
		</div>";

		//echo "The file {$fileName} has been uploaded";
	echo "<br>\n";
	echo "<a href='gallery.php'>Go Back</a>\n";
    }
    else
    {
        //show error message
        echo "<div class='notification attention png_bg'>
			<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
			<div>
				Sorry your file was not uploaded due to the following errors:<br>\n
			</div>
		</div>";
	//echo "Sorry your file was not uploaded due to the following errors:<br>\n";
        echo "<ul>\n";
        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }
        echo "</ul>\n";
	echo "<br>\n";
	echo "<a href='gallery.php'>Go Back</a>\n";
    }
    
}
else
{
    //Show the form
echo "Use the following form below to add a new image to your gallery;<br />\n";
    echo "<form enctype='multipart/form-data' action='' method='POST'>\n";
    echo "Please choose a file: <input name='uploaded' type='file' /><br />\n";
    echo "<input type='submit' value='Upload' />\n";
    echo "</form>\n";

//Echo Tests!
    echo "<br /><br />Random FileName: "; 
echo $rand;
echo "<br />";
echo "member ID: #";
echo $member_id;
}
?>

 

i then want it to rename the files for example:

 

"test.png" is uploaded and name changed to "92997863_321.png" (where first number is using my $rand then the "_" then second number is my $member_id)

 

the script already got these pieces of information and also gets the file extension using the $ext code.

 

can this be done, and can i also upload the new filename to my sql table?

Link to comment
Share on other sites

ok i started to code it to use a md5 tag instead, how can i edit the below to use $rand instead of the md5 and how do i add the last part of rename "_$memberid".

 

Then all i need to code is the "INSERT" query to get it to send new filename to the database.

 

<?php
$rand = rand(00000000000001,99999999999999);
$member_id = $_SESSION['SESS_MEMBER_ID'];

if(isset($_FILES['uploaded']['name']))
{
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB) 
    $fileName = basename($_FILES['uploaded']['name']);
//$newFileName = $rand.$fileName;
    $target = $target . $fileName;
$errors = array();
$target = "gallery/";
$fileBaseName = substr($fileName, 0, strripos($fileName, '.'));
//$newfilename = md5($fileName) . $ext;

   // Get the extension from the filename.
   $ext = substr($fileName, strpos($fileName,'.'), strlen($fileName)-1); 

   $newFileName = md5($fileBaseName) . $ext;

   // Check if the filetype is allowed.
   if(!in_array($ext,$allowed_filetypes))
    {
         $errors[] = "The file you attempted to upload is not allowed.";
    }
    
// Now check the filesize.
   if(!filesize($_FILES['uploaded']['tmp_name']) > $max_filesize)
    {
         $errors[] = "The file you attempted to upload is too large.";
    }
  
    // Check if we can upload to the specified path.
   if(!is_writable($target))
    {
         $errors[] = "You cannot upload to the specified directory, please CHMOD it to 777.";
    }

    //Here we check that no validation errors have occured.
    if(count($errors)==0)
    {

	//Try to upload it.
        if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target . $newfilename))
        {
            $errors[] = "Sorry, there was a problem uploading your file.";
        }
    }
    
    //If no errors show confirmation message
    if(count($errors)==0)
    {
         echo "<div class='notification success png_bg'>
			<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
			<div>
				The file {$fileName} has been uploaded<br>\n
			</div>
		</div>";

		//echo "The file {$fileName} has been uploaded";
	echo "<br>\n";
	echo "<a href='gallery.php'>Go Back</a>\n";
    }
    else
    {
        //show error message
        echo "<div class='notification attention png_bg'>
			<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
			<div>
				Sorry your file was not uploaded due to the following errors:<br>\n
			</div>
		</div>";
	//echo "Sorry your file was not uploaded due to the following errors:<br>\n";
        echo "<ul>\n";
        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }
        echo "</ul>\n";
	echo "<br>\n";
	echo "<a href='gallery.php'>Go Back</a>\n";
    }
    
}
else
{
    //Show the form
echo "Use the following form below to add a new image to your gallery;<br />\n";
    echo "<form enctype='multipart/form-data' action='' method='POST'>\n";
    echo "Please choose a file: <input name='uploaded' type='file' /><br />\n";
    echo "<input type='submit' value='Upload' />\n";
    echo "</form>\n";

//Echo Tests!
    echo "<br /><br />Random FileName: "; 
echo $rand;
echo "<br />";
echo "member ID: #";
echo $member_id;
}
?>

Link to comment
Share on other sites

^^ scrap above post.

 

My new code works perfectly for uploading, and renaming the file. but for some reason i cant seem to get the INSERT command to work correctly, and i doing this wrong?

 

My code:

<?php
$rand = mt_rand(1,9999999);
$member_id = $_SESSION['SESS_MEMBER_ID'];

if(isset($_FILES['uploaded']['name']))
{
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB) 
    $fileName = basename($_FILES['uploaded']['name']);
$errors = array();
$target = "gallery/";
$fileBaseName = substr($fileName, 0, strripos($fileName, '.'));

   // Get the extension from the filename.
   $ext = substr($fileName, strpos($fileName,'.'), strlen($fileName)-1); 

   //$newFileName = md5($fileBaseName) . $ext;
   $newFileName = $target . $rand . "_" . $member_id.$ext;
   
   // Check if filename already exists
   if(file_exists("gallery/" . $newFileName)) 
    {
     $errors[] = "The file you attempted to upload already exists, please try again.";
    }
   // Check if the filetype is allowed.
   if(!in_array($ext,$allowed_filetypes))
    {
         $errors[] = "The file you attempted to upload is not allowed.";
    }
    
// Now check the filesize.
   if(!filesize($_FILES['uploaded']['tmp_name']) > $max_filesize)
    {
         $errors[] = "The file you attempted to upload is too large.";
    }
  
    // Check if we can upload to the specified path.
   if(!is_writable($target))
    {
         $errors[] = "You cannot upload to the specified directory, please CHMOD it to 777.";
    }

    //Here we check that no validation errors have occured.
    if(count($errors)==0)
    {

	//Try to upload it.
        if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $newFileName))
        {
            $errors[] = "Sorry, there was a problem uploading your file.";
        }
    }
    
    //If no errors show confirmation message
    if(count($errors)==0)
    {
         echo "<div class='notification success png_bg'>
			<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
			<div>
				The file {$newFileName} has been uploaded<br>\n
			</div>
		</div>";


		//echo "The file {$fileName} has been uploaded";
	echo "<br>\n";
	echo "<a href='gallery.php'>Go Back</a>\n";


	// INSERT data into database.
	$insertData = "INSERT INTO 'gallery' (image, memberid, caption) VALUES ('$newFileName', '$member_id', 'caption')";
        mysql_query($insertData);

    }
    else
    {
        //show error message
        echo "<div class='notification attention png_bg'>
			<a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a>
			<div>
				Sorry your file was not uploaded due to the following errors:<br>\n
			</div>
		</div>";
	//echo "Sorry your file was not uploaded due to the following errors:<br>\n";
        echo "<ul>\n";
        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }
        echo "</ul>\n";
	echo "<br>\n";
	echo "<a href='gallery.php'>Go Back</a>\n";
    }
    
}
else
{
    //Show the form
echo "Use the following form below to add a new image to your gallery;<br />\n";
    echo "<form enctype='multipart/form-data' action='' method='POST'>\n";
    echo "Please choose a file: <input name='uploaded' type='file' /><br />\n";
    echo "<input type='submit' value='Upload' />\n";
    echo "</form>\n";

//Echo Tests!
    echo "<br /><br />Random FileName: "; 
echo $rand;
echo "<br />";
echo "member ID: #";
echo $member_id;
}
?>

Link to comment
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.