Hi,
I am putting an upload function in my project. I need to upload only images that is, .jpeg, .gif.

But I am not being able to do this, all types of files are being uploaded. Below is my code:

include("db_connect.php");
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !="none")
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "<table width=300 height=300 align=center>
    <tr><td align=center><B>
        Your symbol has been successfully uploaded.<?B>
    </td></tr>";
echo "<tr><td align=center>";

echo "<tr><td align=center>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
echo "</td></tr>";
echo"<tr><td align=center>";
echo "Image name:.$key";
echo "</td></tr>";
echo "<tr><td align=center>";
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";

As you can see in this code the images are saved in a folder but i want to send them in a database. I am not being able to use the POST method to send it to database. Can anyone please help????

Recommended Answers

All 4 Replies

here is an upload code i just typed up for you. change the variables to what you need and also change the sql query to your database specifications.

Remember: always store images in a folder on the server and have the path to the file in the database.

here is the code:

<?php

//Change this to the name of the page
$thispage = 'index.php';

//Set the allowed file types
$types = array("gif","jpeg","jpg","png");

//Set max size of image (in MB)
$maxSize = '5';

//Set upload directory
$uploadDir = 'upload';

//This function get the extension of the image
function getExtension($str) {
	$i = strrpos($str,".");
	if (!$i) {
		return "";
	}
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return $ext;
}

if (isset($_POST['submit'])) {
	if (empty($_FILES['image']['name'])) {
		$result = 'Please choose an image';
	}
	else {
		$imageName = $_FILES['image']['name'];
		$exten = getExtension($imageName);
		if (!in_array($exten,$types)) {
			$result = 'Unknown extension, please choose a different image';
		}
		else {
			$tmpFile = $_FILES['image']['tmp_name'];
			$size = filesize($tmpFile);
			if ($size > ($maxSize * 1024)) {
				$result = 'Image exceeds size limit, choose a smaller image';
			}
			else {
				$newName = $uploadDir . '/' . $time . '.' . $exten;
				if (!copy($tmpFile,$newName)) {
					$result = 'Unable to upload image, try again';
				}
				else {
					$sql   = "INSERT INTO `images` (`image_path`) VALUES ('" . $newName . "')";
					$query = mysql_query($sql) or die('Error: ' . mysql_error());
					$result = 'Image Uploaded Successfully';
				}
			}
		}
	}
}
$html =<<<HTML
<form action="$thispage" method="post" enctype="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="5">
	<tr>
		<td align="right">Image:</td>
		<td align="left"><input type="file" name="image" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" name="submit" value="Upload" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center">$result</td>
	</tr>
</table>
</form>
HTML;

echo $html;

?>

there might be some errors because i don't have time to test it. let me know and I will fix it.

commented: Good work! +7

Hi kkeith29,
The code works fine but I am having problems retrieving the images from my database. The code only sends the file name to the database and not the image itself.

But Then how do I retrieve those images and then display them.

I am using the normal

<?php echo $rows['symbol']; ?>

whereby symbol is my column name.

It rerieves other images that I have manually entered in the database but only file names of uploaded images. I guess I need to retrieve from the folder 'upload' but how to do it?

Could you please help???

Code works but it posts image without filename - in folder it is just " .gif" and in database it is "upload/.gif"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.