0

I am trying to upload a image from my html form to my mysql blob column. I feel like the php file isn't receiving the file based off of all the testing to try to figure out what is not working.

relevant HTML code:

<form action = "insert.php" method="post" enctype="multipart/form-data">
<input type="file" name="myimage">
<input type="submit" name = "Insert" value="Insert">
</form>

relevant PHP code:

$imagename=$_FILES["myimage"]["name"];

if(getimagesize($FILES['myimage']['tmp_name']) == FALSE){
    echo "no image";
}
//Get the content of the image and then add slashes to it 
$imagetmp = addslashes($_FILES['myimage']['tmp_name']);
$image = file_get_contents($imagetmp);
$sql = "INSERT INTO locations (image) VALUES ('$image')";
$res = mysql_query($sql) or die(mysql_error());

I believe the php isn't recieving the file because I always get my "no image" echoed.

3 Answers3

0

What kind of error you faced you did not notice.I find a problem in your code.I think it should be work.

<form action = "insert.php" method="post" enctype="multipart/form-data">

Warning

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
  • What would the query line look like for mysqli? – George Young Aug 07 '16 at 08:25
  • Just go through the link it may help you. http://stackoverflow.com/questions/22377686/insert-view-image-into-from-a-mysql-db . After posting answer of your specific question do not edit/add anything to your question code.Beacuse someone give the answer according to your first question.Thanks. – Chonchol Mahmud Aug 07 '16 at 08:35
0

Before inserting into the database I think you should base64_encode the data.

$imagename=$_FILES["myimage"]["name"];
if( getimagesize( $FILES['myimage']['tmp_name'] ) == FALSE ){
    exit( "no image" );
}

$image = base64_encode( file_get_contents( $_FILES['myimage']['tmp_name'] ) );

To use mysqli you could do something like:

$servername = "localhost";
$username = "foo";
$password = "xxx";
$dbname = "bar";

$db=new mysqli( $servername, $username, $password, $dbname );

$sql='insert into `locations` set `image`=?';
$stmt=$db->prepare( $sql );

$stmt->bind_param( 's', $image );
if( $stmt ) $stmt->execute();

$db->close();

$sql = "INSERT INTO `locations` (`image`) VALUES ('$image')";
$res = mysql_query( $sql ) or die( mysql_error() );
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

it's stupid idea that uploading image to mysql database. Upload image to folder on your server and write path (path to image) to database. That is optimal variant and better.

rakhmatov
  • 369
  • 3
  • 9