Jump to content

Image upload and rename


master82

Recommended Posts

Not sure where to start with this, What I am hoping to do is create a simple form to securly upload an image file into a specific folder in the root of my site directory (folder: upics). I also need it to rename the image to user(userid here) [eg user1234.jpg]. But if that file already exists, then to simply overite it.

 

Is this possible? And if so, where should I start?

Link to comment
https://forums.phpfreaks.com/topic/48360-image-upload-and-rename/
Share on other sites

a good place to start would be google for "php image upload" and then, when you have managed to upload, you go and read about rename() on php.net... then you use your fantasy and inserts the rename function insde the upload code =)

 

this is what I'm using for upload and renameing... use it as a guideline... i'm still new to this uploade part.. so the code can most likely be better written than that... but it works perfect, so i'm happy =)

 

<?php
function uploadBilde($orgName,$newName)
{

	unset($imagename);

	if(!isset($_FILES) && isset($HTTP_POST_FILES)) { $_FILES = $HTTP_POST_FILES; }
	if(!isset($_FILES['bilde'])) { $error["image_file"] = "An image was not found."; }

	$imagename = basename($_FILES['bilde']['name']);
	//echo $imagename;

	if(empty($imagename))
	$error["imagename"] = "The name of the image was not found.";

	if(empty($error))
	{
		$newimage = "images/members/" . $imagename;
		//echo $newimage;
		$result = move_uploaded_file($_FILES['bilde']['tmp_name'], $newimage);
		if(empty($result)) { $error["result"] = "There was an error moving the uploaded file."; }
		$newName = str_replace("@", "a", $newName);
		$newName = strtolower($newName);
		rename($newimage, "images/members/".$newName);
		chmod("images/members/".$newName, 0777);
		$image = "images/members/".$newName;
		$chkImageSize = getimagesize($image);
		$error = array();
		if ($chkImageSize[0] > 300) { $error[] = "Bilde var for bredt. Max bredde = 300px"; }
		if ($chkImageSize[1] > 300) { $error[] = "Bilde var for høyt. Max høyde = 300px"; }
		if (!empty($error)) {
			foreach ($error as $err) {
				echo $err."<br>";
			}
		} else { echo "Bildet ble lastet opp<br>"; }
	}
}
?>

 

Good Luck

- Clown

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.