Jump to content

Random image name on upload and insert


joshgarrod

Recommended Posts

Hi all, I have an issue with an image upload script. So far the script will upload the image to the server and rename it with a random 5 digit number on the end. I then INSERT this data into a table for sourcing the image later on. the only problem is that the data in the table is the image's original name not the new one. how do I get it to change that name too? Thanks.

 

<?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 
			}
			}
error_reporting (E_ALL ^ E_NOTICE);
    $usr = "user";
    $pwd = "pword";
    $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']);

        $SQL = " INSERT INTO mhhire ";
        $SQL .= " (logo) VALUES ";
        $SQL .= " ('$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
https://forums.phpfreaks.com/topic/224161-random-image-name-on-upload-and-insert/
Share on other sites

Take a look here, it should make it clearer...

 

http://www.phpeasystep.com/phptu/18.html

 

<?php

 

// Your file name you are uploading

$file_name = $HTTP_POST_FILES['ufile']['name'];

 

// random 4 digit to add to our file name

// some people use date and time in stead of random digit

$random_digit=rand(0000,9999);

 

//combine random digit to you file name to create new file name

//use dot (.) to combile these two variables

 

$new_file_name=$random_digit.$file_name;

 

//set where you want to store files

//in this example we keep file in folder upload

//$new_file_name = new upload file name

//for example upload file name cartoon.gif . $path will be upload/cartoon.gif

$path= "upload/".$new_file_name;

if($ufile !=none)

{

if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))

{

echo "Successful<BR/>";

 

//$new_file_name = new file name

//$HTTP_POST_FILES['ufile']['size'] = file size

//$HTTP_POST_FILES['ufile']['type'] = type of file

echo "File Name :".$new_file_name."<BR/>";

echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";

echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";

}

else

{

echo "Error";

}

}

?>

 

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.