Jump to content

upload image into database


garydt

Recommended Posts

I'm trying to upload an image file into a mysql database and i got the following code off the net but the image isn't going into the database.

<?php require_once('Connections/elvisdb.php'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

mysql_select_db($database_elvisdb, $elvisdb);

 

if (!$_POST['uploaded']){

//If nothing has been uploaded display the form

?>

 

 

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" 

ENCTYPE="multipart/form-data">

Upload:<br><br>

<input type="file" name="image"><br><br>

<input type="hidden" name="uploaded" value="1">

<input type="submit" value="Upload">

</form>

 

<?

 

}else{

$ip=$REMOTE_ADDR;

 

//don't continue if an image hasn't been uploaded

if (!empty($image)){

 

//copy the image to directory

copy($image, "C:/Program Files/xampp/htdocs/epeople/".$ip."");

 

//open the copied image, ready to encode into text to go into the database

$filename1 = "C:/Program Files/xampp/htdocs/epeople/".$REMOTE_ADDR;

$fp1 = fopen($filename1, "r");

 

//record the image contents into a variable

$contents1 = fread($fp1, filesize($filename1));

 

//close the file

fclose($fp1);

 

//encode the image into text

$encoded = chunk_split(base64_encode($contents1)); 

 

//insert information into the database

mysql_query("INSERT INTO images (img,data)"."VALUES ('NULL', '$encoded')");

 

//delete the temporary file we made

 

}

 

//end

}

?>

</body>

</html>

 

 

Ideally i want to load images into the same record that contains other fields of data. Can you tell me if this is possible?

Thanks

Link to comment
Share on other sites

Personally I think that code is a bit off. For example, I don't see where $image variable's value is even set. Basically a file upload consists of using the $_FILES array that PHP creates when a file is uploaded. It consists of a couple of important things:

 

$_FILES['uploadfile']['tmp_name'];

which is the temporary name that is assigned to the uploaded file. As a default, the image is uploaded to a temporary folder and assigned the temporary name.

 

and

 

$_FILES['uploadedfile']['name'];

which is the actual name of the file.

 

There's also $_FILES['uploadedfile']['size']; and $_FILES['uploadedfile']['type']; as well so you can check against size restrictions and file types.

 

Here's an image upload snippet I use that also validates the extension and the size that you can use as a model:

 

<?php
// Upload File
$eg_success_File1 = false;
if(!empty($_FILES['photo']['name']))
{
	// Check file is not larger than specified maximum size
	$eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false;
	// Check file is of the specified type
	if($eg_allowUpload)
		$eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false;

	if($eg_allowUpload)
	{
		if(is_uploaded_file($_FILES['photo']['tmp_name']))
		{
			$eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/";

			$eg_uploadFile1 = $eg_uploaddir.rawurlencode($_FILES['photo']['name']);	
			// Create a unique filename for the uploaded file
			$eg_i = 1;
			while (file_exists($eg_uploadFile1))
			{
				$eg_separated_filename = explode(".",$eg_uploadFile1);
				if (substr($eg_separated_filename[0],-1) == $eg_i)
				{
					$eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1));
					$eg_i++;
				}
				$eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i";
				$eg_uploadFile1 = implode(".",$eg_separated_filename);
			}

			$eg_success_File1 = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadFile1);
		}

	}

}
?>

 

For the mysql insert i'd add the value like this:

 

'".substr(strrchr($eg_uploadFile1, "/"), 1)."'
Link to comment
Share on other sites

You could try just storing the name of the file in the database instead of storing the file itself, and use the upload information above to upload the file to a specific directory.  then when you need to recall the file again later for display or whatever, all you have to do is query your database for the name and do something like

 

echo '<img src="'.$row['img_name'].'"/>';

 

where row is one you have just queried previously.  Not sure if that method is appropriate for what you want to do , but that is how I do it.

Link to comment
Share on other sites

You could try just storing the name of the file in the database instead of storing the file itself, and use the upload information above to upload the file to a specific directory.  then when you need to recall the file again later for display or whatever, all you have to do is query your database for the name and do something like

 

echo '<img src="'.$row['img_name'].'"/>';

 

where row is one you have just queried previously.  Not sure if that method is appropriate for what you want to do , but that is how I do it.

 

calab is right. it is pretty much standard procedure to store the file's information into a database. for instance, upon upload, you'd store the filename into one field, the path to the file into another field, the file size in another field, the file type in another field, and so on... then, to call the file, you do what calab said and do something like:

 

<?php
        echo "<img src=\"". $row['file_path'] . $row['file_name'] ."\">\n";
?>

Link to comment
Share on other sites

You need an HTML form like the following:

 

<html>
<head>
<title>Upload form</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

 

 

Link to comment
Share on other sites

I've been trying this and I'm getting error     

Parse error: parse error, unexpected '<' in C:\Program Files\xampp\htdocs\epeople\upload.php on line 47

 

The code-

<?php require_once('Connections/elvisdb.php'); ?>

<?php

// Upload File

$eg_success_File1 = false;

if(!empty($_FILES['photo']['name']))

{

// Check file is not larger than specified maximum size

$eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false;

// Check file is of the specified type

if($eg_allowUpload)

$eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false;

 

if($eg_allowUpload)

{

if(is_uploaded_file($_FILES['photo']['tmp_name']))

{

$eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/";

 

$eg_uploadFile1 = $eg_uploaddir.rawurlencode($_FILES['photo']['name']);

// Create a unique filename for the uploaded file

$eg_i = 1;

while (file_exists($eg_uploadFile1))

{

$eg_separated_filename = explode(".",$eg_uploadFile1);

if (substr($eg_separated_filename[0],-1) == $eg_i)

{

$eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1));

$eg_i++;

}

$eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i";

$eg_uploadFile1 = implode(".",$eg_separated_filename);

}

 

$eg_success_File1 = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadFile1);

}

 

}

 

}

 

$insertSQL = sprintf("INSERT INTO images (imageName) VALUES ('".substr(strrchr($eg_uploadFile1, "/"), 1)."');

  mysql_select_db($database_elvisdb, $elvisdb);

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error())

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<form enctype="multipart/form-data" action="uploader.php" method="POST">

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

Choose a file to upload: <input name="photo" type="file" /><br />

<input type="submit" value="Upload File" />

</form>

</body>

</html>

 

 

Have i written the code right to insert the image name variable into the database?  I'm not sure which directory it's storing the actual image. Sorry to keep  asking questions.

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.