Jump to content

File Rename


princeofpersia

Recommended Posts

Hi

 

As i am a newbie, i finally been able to upload and resize an image, but i need to rename the files to random numbers, i have found a code but i dont know where i should have to embed it in my php can u please tell me where and how?

 

 

this is my php

 


if (isset($_POST['register']) && $_POST['register'])
{
$update = mysql_query("UPDATE agents SET credit= credit-1 WHERE username='$username'");
	//image1
	$nameone=$_FILES['myfileone']['name'];
	if ($nameone)

	{
		$dst_filename = resize_upload_image($_FILES['myfileone'], "images/");
		if ($dst_filename !== false) {
			extract($dst_filename);
			$image1 = mysql_query ("UPDATE img SET image1='$img_filename', thumb1='$thumb_filename'");
		}
	}


function resize_image($srcfilename, $dstfilename, $new_width, $new_height)
{
$ext = strtoupper(pathinfo($srcfilename, PATHINFO_EXTENSION)); 
// JPEG image 

if(is_file($srcfilename) && ($ext == "JPG" OR $ext == "JPEG")) 
{ 
	// Get src dimensions
	list($width, $height) = getimagesize($srcfilename);

	// Resample
	$image_p = imagecreatetruecolor($new_width, $new_height);
	$image = imagecreatefromjpeg($srcfilename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

	// Output
	imagejpeg($image_p, $dstfilename, 100);
	return TRUE;
} 
// PNG image 
elseif(is_file($srcfilename) && $ext == "PNG") 
{ 
	// Get src dimensions
	list($width, $height) = getimagesize($srcfilename);

	// Resample
	$image_p = imagecreatetruecolor($new_width, $new_height);
	$image = imagecreatefrompng($srcfilename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

	// Output
	imagepng($image_p, $dstfilename, 0);
	return TRUE;
}
return false;
}

function resize_upload_image($file, $path)
{
global $standard_width, $standard_height;
global $thumb_width, $thumb_height;

$img_name= $file['name'];
$tmp_name= $file['tmp_name'];
if ($img_name)
{
	if (substr($path, strlen($path)-1) != "/") {
		$path .= "/";
	}

	$original_img_name = $path."tmp-".$img_name;
	move_uploaded_file($tmp_name, $original_img_name);
	$img_location = $path.$img_name;
	$thumb_location = $path."thumb-".$img_name;

	if (resize_image($original_img_name, $img_location, $standard_width, $standard_height) === FALSE) {
		unlink($original_img_name);
		return false;
	}
	if (resize_image($original_img_name, $thumb_location, $thumb_width, $thumb_height) === FALSE) {
		unlink($original_img_name);
		return false;
	}
	unlink($original_img_name);
	return array("img_filename"=>$img_location, "thumb_filename"=>$thumb_location);
}
return false;
}


 

sp this is a time id which could be added

 

$image_name=time().'.'.$extension;

$filename = "img/". $image_name;

$filename1 = "img/small_". $image_name;


Link to comment
Share on other sites

If I understand your question right, you want to know what to edit in which function, to rename the files to random numbers.

As I see it the resize_upload_image() function creates two files, a thumbnail and an image.

So to rename them to random numbers you would have to edit the variables thumb_location and img_location as following:

 

$random_number = rand(0,999); // The random number, you wanna set as suffix.
$img_location = $path.$img_name."-".$random_number;
$thumb_location = $path."thumb-".$img_name."-".$random_number;

 

Hope you can use it pal ;)

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.