Jump to content

[SOLVED] Newb: Image upload


kidintraffic

Recommended Posts

I am relatively new to php with using mysql databases, so bare with me.  I have a form that a user imputs their data into and when then click submit it uploads that information to the database.  I have another page that displays the data.

 

I want to allow the user to be able to upload an image and then I need to display the image on another page.  How do I store the image name into the database and also allow the user to upload to the site?

 

Any help is greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/48770-solved-newb-image-upload/
Share on other sites

The first thing. You create a form.

 

form1.php

 

<form action="form2.php" method="post" name="frmAddImage" >

 

<table>

<tr>

   <td width="150">Image</td>

   <td> <input name="fleImage" type="file">

    </td>

  </tr>

</table>

 

form2.php is the next page for processing.

 

 

Now form2.php

 

$image     = $_FILES[$fileImage]; 

 

OR OR OR

 

 

you can create a function that enable a user to upload the file.

 

example:

 

<?php
function uploadImage($inputName, $uploadDir)
{
$image     = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';

// if a file is given
if (trim($image['tmp_name']) != '') {
	$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

	// generate a random new file name to avoid name conflict
	$imagePath = md5(rand() * time()) . ".$ext";

	list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

	// make sure the image width does not exceed the
	// maximum allowed width
	if (LIMIT_PRODUCT_WIDTH && $width > MAX_IMAGE_WIDTH) {
		$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_IMAGE_WIDTH);
		$imagePath = $result;
	} else {
		$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
	}	

	if ($result) {
		// create thumbnail
		$thumbnailPath =  md5(rand() * time()) . ".$ext";
		$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);

		// create thumbnail failed, delete the image
		if (!$result) {
			unlink($uploadDir . $imagePath);
			$imagePath = $thumbnailPath = '';
		} else {
			$thumbnailPath = $result;
		}	
	} else {
		// the product cannot be upload / resized
		$imagePath = $thumbnailPath = '';
	}

}


return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

?>

 

the ALL capital letter is defined as constant. such as MAX_IMAGE_WIDTH.

 

try first and think. thanks. I hope this can guide you to the correct direction. There are many other ways to do the same thing.

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.