Jump to content

Random name generate for images


joshgarrod

Recommended Posts

Hi, I have a script to add images to my tabel and and upload them to my server, I have managed to get the upload script to rename the image with a 5 digit random number on the end to prevent overwriting, the only problem I can't seem to be able to add the same name to my tabel. Any ideas? Thanks in advance.

 

<?php

$idir = "uploads/";   // Path To Images Directory


if (isset ($_FILES['fupload'])){

//upload the image to tmp directory
$url = $_FILES['fupload']['name'];   // Set $url To Equal The Filename For Later Use 
	if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { 
		$file_ext = strrchr($_FILES['fupload']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
		$copy = copy($_FILES['fupload']['tmp_name'], $idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext);   // Move Image From Temporary Location To Perm 
			}
			}
if (isset ($_FILES['fupload2'])){

//upload the image to tmp directory
$url = $_FILES['fupload2']['name'];   // Set $url To Equal The Filename For Later Use 
	if ($_FILES['fupload2']['type'] == "image/jpg" || $_FILES['fupload2']['type'] == "image/jpeg" || $_FILES['fupload2']['type'] == "image/pjpeg") { 
		$file_ext = strrchr($_FILES['fupload2']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
		$copy = copy($_FILES['fupload2']['tmp_name'], "$idir" . $_FILES['fupload2']['name'] $file_ext).rand(10000 , 99999).$file_ext);    // Move Image From Temporary Location To Permanent Location 
			}
			}
if (isset ($_FILES['fupload3'])){

//upload the image to tmp directory
$url = $_FILES['fupload3']['name'];   // Set $url To Equal The Filename For Later Use 
	if ($_FILES['fupload3']['type'] == "image/jpg" || $_FILES['fupload3']['type'] == "image/jpeg" || $_FILES['fupload3']['type'] == "image/pjpeg") { 
		$file_ext = strrchr($_FILES['fupload3']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
		$copy = copy($_FILES['fupload3']['tmp_name'], "$idir" . $_FILES['fupload3']['name'] $file_ext).rand(10000 , 99999).$file_ext);      // Move Image From Temporary Location To Permanent Location 
			}
			}
if (isset ($_FILES['fupload4'])){

//upload the image to tmp directory
$url = $_FILES['fupload4']['name'];   // Set $url To Equal The Filename For Later Use 
	if ($_FILES['fupload4']['type'] == "image/jpg" || $_FILES['fupload4']['type'] == "image/jpeg" || $_FILES['fupload4']['type'] == "image/pjpeg") { 
		$file_ext = strrchr($_FILES['fupload4']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
		$copy = copy($_FILES['fupload4']['tmp_name'], "$idir" . $_FILES['fupload4']['name'] $file_ext).rand(10000 , 99999).$file_ext);      // Move Image From Temporary Location To Permanent Location 
			}
			}

$usr = "user";
    $pwd = "pass";
    $db = "db";
    $host = "host";

    # connect to database
    $cid = mysql_connect($host,$usr,$pwd);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }

    if ($_POST['submit']) {
$logo = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$image1 = mysql_real_escape_string("$idir" . $_FILES['fupload2']['name']);
$image2 = mysql_real_escape_string("$idir" . $_FILES['fupload3']['name']);
$image3 = mysql_real_escape_string("$idir" . $_FILES['fupload4']['name']);

$SQL = " INSERT INTO mhhire ";
        $SQL .= "  image1, image2, image3, logo) VALUES ";
        $SQL .= " ('$image1', '$image2', '$image3', '$logo') ";

$result = mysql_db_query($db,$SQL,$cid);
      $last=mysql_insert_id();

        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }

	header("location:thanks.php");
	exit();

    }
?>

Link to comment
Share on other sites

A few things..

 

When it comes to naming resources like images, it's definitely going to be safer to name them based off of a timestamp rather than a 5 digit random integer.  Even tho 99999 seems like a big number, your chance of a naming collision is pretty high.

 

You could go with something like microtime() and probably be a lot safer.  The even MORE safe route would be to store the data into a table immediately, and use the auto_inc value (or a convention based off of it.  At least that way you KNOW you're talking about a unique resource)

 

What error are you getting with your current script?

 

Link to comment
Share on other sites

Okay, I take on board your points.

 

I am getting no errors just that my uploaded image on the server is called "image12345.jpg" and the image source in my table is being recorded as "image.jpg".

 

I could use a variable $account which is defined early on in the script, this is a unique account number that each user will have and this will ensure no collisions. how would I add $account to the end of the image name?

 

Thanks for your help

Link to comment
Share on other sites

Ahhh well I can answer your current problem pretty easily :)

 

You aren't saving the "temp" file name to any variable.  When you copy the image to the directory it's getting named, but it's not going to rename the file.

 

Your best bet would be to take the filename and save THAT to a var.

 

Then on your insert use that :)

Link to comment
Share on other sites

I guess I am reading the problem differently than the last two posters. Looking at the code it seems he is trying to insert the ORIGINAL file name in the database - not the modified name with the random number.

 

First, I agree with nafetski  that a timestamp would be prefereable to a random number. But, more importantly - if you are storing the original names but not the modified names, how are you linking the database records to the images? You need to store both in the same record. That way you can display the original name the user uploaded but still link it back to the unique file name you create.

 

However, none of that is your problem. If you are getting a database error when trying to insert an image with the same original name as another image, the problem is that you have set that field in the database to be a unique field.

Link to comment
Share on other sites

hi mjdamato, I do want both the record in the database and the name of the image to be the same - how do I achieve this at the moment the image is saved to the server with the 5 digit number on the end but not in the database. Sorry I am a bit out of my depth with all this.

 

Then your answer is simple. Look at this bit of code you posted previously

if (isset ($_FILES['fupload']))
{
    //upload the image to tmp directory
    $url = $_FILES['fupload']['name'];   // Set $url To Equal The Filename For Later Use 
    if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg")
    {
        $file_ext = strrchr($_FILES['fupload']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
        $copy = copy($_FILES['fupload']['tmp_name'], $idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext);   // Move Image From Temporary Location To Perm
    }
}

 

You "save" the original name to the variable $url which you later used in your INSERT statement, but you actually saved the file using the name

$idir. basename($_FILES['fupload']['name'], $file_ext).rand(10000 , 99999).$file_ext);

 

You just need to create the new name as a varaible first then use that to both save the file AND to save to the database. Here is a modification of that block of code that you can use to rewrite the similar blocks:

if (isset ($_FILES['fupload']))
{
    //upload the image to tmp directory
    $file_ext = strrchr($_FILES['fupload']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
    $url = basename($_FILES['fupload']['name'], $file_ext).time().$file_ext); // Set $url To Equal The Filename For Later Use
    
    if (in_array($_FILES['fupload']['type'], array('image/jpg', 'image/jpeg','image/pjpeg'))
    {
        $copy = copy($_FILES['fupload']['tmp_name'], $idir.$url);   // Move Image From Temporary Location To Perm
    }
}

 

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.