Jump to content

Unable to upload image to a database.


cool_techie

Recommended Posts

I am unable to upload image to db,

I get a message "No image selected/uploaded"  after every trial,

 

My HTML form is:

 

<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="insert.php" method="post">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
</body>
</html>

and my php script "insert.php" is-

<?php
// Create MySQL login values and 
// set them to your login information.
$username = "root";
$password = "";
$host = "localhost";
$database = "test";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database); 

// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      

      // Create the query and insert
      // into our database.
      $query = "INSERT INTO tbl_images ";
      $query .= "(image) VALUES ('$data')";
      $results = mysql_query($query, $link);
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
}else{
   print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>  


my table consists if an "id" and an "image" of type "blob".

 

 

Link to comment
https://forums.phpfreaks.com/topic/226129-unable-to-upload-image-to-a-database/
Share on other sites

My advice is "don't put images into a database. Save and use them from a file. Put the image name into the database." But if you really want to do this:

 

I would first find out if I'm uploading the image. Put it into a folder and see if that is working.

 

Then go here and read the last entry "MySQL Image Table" ->  http://hockinson.com/

 

 

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.