How to rename my upload image?

Hello dudes I am new to this phphelp forum, i Have code which upload image to my server in folder and store it’s path to mysql database, actually it upload image with it’s real name, what I want to do is to rename the image and then save it to mysql database and then upload to my upload folder.

I have attached the file if anybody could help me in this regard.

please upload the script when you finished,
Thanks in Advance


image uplaod script.zip (1.23 KB)

[php]move_uploaded_file($_FILES[“image”][“tmp_name”],“photos/” . $_FILES[“image”][“name”]);[/php]
–>
[php]$newName = ‘newName’;
move_uploaded_file($_FILES[“image”][“tmp_name”],“photos/” . $newName);[/php]

Thanks Jiml,
Actually I forgot to mention, I want to randomize the name of my uploaded picture, so how can i do that, please help.

Post some examples of the output you wish to have

Hello Jim this is the real code.

Index.php

[php]

Select Image:

Caption


Photo Archieve

<?php include('config.php'); $result = mysql_query("SELECT * FROM photos"); while($row = mysql_fetch_array($result)) { echo '
'; echo '

'; echo '

'.$row['caption'].'

'; echo '
'; } ?> [/php]

and this is the addexec.php code
[php]<?php
include(‘config.php’);
if (!isset($_FILES[‘image’][‘tmp_name’])) {
echo “”;
}else{
$file=$_FILES[‘image’][‘tmp_name’];
$image= addslashes(file_get_contents($_FILES[‘image’][‘tmp_name’]));
$image_name= addslashes($_FILES[‘image’][‘name’]);

		move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);
		
		$location="photos/" . $_FILES["image"]["name"];
		$caption=$_POST['caption'];
		
		$save=mysql_query("INSERT INTO photos (location, caption) VALUES ('$location','$caption')");
		header("location: index.php");
		exit();					
}

?>
[/php]

now what i want to do is to rename the photo name and give random names to photos so that photos in photos folder on my server won’t overwrite each other. every photo must have unique name, using md5 rand method. please solve my problem, Thanks in Advance.

It’s just to change the file name, please post some code if you’ve tried something that doesn’t work.

Ps: what happens if you post the form with caption: This isn’t a horse

Hey Jiml I have tried and almost successful in my project but still am facing problems,
now i have script which uploads picture with uniqe id to my upload folder but it don’t update avatar field in database please help me in this regard the code is here,
settings.php
[php]<?php
/*************** AVTAR UPLOAD SCRIPT /
//Requirements: PHP Thumbnail class (http://phpthumb.gxdlabs.com/)
// License: Free
/
*****************/
if($_POST[‘Submit’] == ‘Upload’)
{
// This is the unique user_id
$id = $_SESSION[‘user_id’];

if (!empty ($_FILES[‘ifile’][‘tmp_name’]))
{
/* Thumbnail class is required */
include_once(‘phpthumb/ThumbLib.inc.php’);

/* GetImageSize() function pulls out valid info about image such as image type, height etc. If it fails
then it is not valid image. */

if (!getimagesize($_FILES[‘ifile’][‘tmp_name’]))
{
die(“Error - Invalid Image File.”);

}

$imgtype = array(‘1’ => ‘.gif’, ‘2’ => ‘.jpg’ , ‘3’ => ‘.png’);

// extract the width and height of image
list($width, $height, $type, $attr) = getimagesize($_FILES[‘ifile’][‘tmp_name’]);

// Extract the image extension
switch ($type)
{
case 1: $ext=’.gif’; break;
case 2: $ext = ‘.jpg’;break;
case 3: $ext=’.png’; break;
}
// Dont allow gif files to upload as it may contain harmful code
if ( $ext == ‘.gif’) {
die(“Sorry - GIF not allowed. Please use only PNG or JPEG formats”);
}

/* Specify maximum height and width of users uploading image */
if ($width > 1000 || $height > 1000)
{
die(“ERROR: Maximum width and height exceeded. (max 1000x1000 pixels)”);

}
/* Specify maximum file size here in bytes */
if ($_FILES[‘ifile’][‘size’] > 500000 )
{
die(“Error: Large File size. (max 500kb)”);

} 
/******** IMAGE RESIZING *********************/
// Before we start resizing, we first have to move the image file to server
// save it there under a unique name and then do the final resizing and save the resized image.

// Specify which directory you want to upload. It should be a subfolder where the script is present
// We also generate a unique name for picture FILE-USERID-XXX where xxx is random number
// The uploads folder must have writable permissions.
$uploaddir = 'uploads/';
$secondname = rand(100,99);
$uploadfile =  $uploaddir . "img-$id-$secondname". $ext;

if (!move_uploaded_file($_FILES['ifile']['tmp_name'], $uploadfile ))
 {
   die("Error moving the uploaded file");
 }

$thumb = PhpThumbFactory::create($uploadfile);
//specify the height and width of avatar image to resize
$thumb->resize(100,100);
$thumb->save($uploadfile);
//$thumb->show();
//MySQL query to update avatar filename in the database. You need to create a field avatar
mysql_query("update users set avatar='$uploadfile' where id='$id'");

//$thumb->destruct();
}

}
?>

Image Upload

Max 100 Kb, JPEG/PNG only (100x100 pixels maximum)

[/php]

phptumbnailer class is also downloaded to my server but still don’t know what is the problem.
please help me

Sponsor our Newsletter | Privacy Policy | Terms of Service