0

i tried to upload an image but when getimagesize the image is empty it is returning false.. and warning is coming ,it is not saving in database .the database name is project and table name is images and fields are name and image .Here is a code...

<?php
 ini_set('mysql.connect_timeout',300);
 ini_set('default_socket_timeout',300);

?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/formdata">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
  die("Database connection failed: " . mysqli_connect_error());
}
else
{
  echo "Connected successfully";
}
 //data upload
 if( isset($_POST['submit'] ))
 {
     if(getimagesize($_FILES['image']['tmp_name'])==FALSE) //image size is checked
     {
            echo "upload image";
     }
     else
     {
        $image= addslashes($_FILES['image']['tmp_name']);
        $name=addslashes($_FILES['image']['name']);
        $image=file_get_contents($image);
        $image= base64_encode($image);
        saveimage($name,$image);
      }
    }
    function saveimage($name,$image)
    {
      $conn = mysql_connect('localhost', 'root','');
      mysql_select_db("project",$conn);
      $result = mysql_query("insert into images(name,image) values('$name','$image')");    //query implemented

    }
    ?>      //function written to save image
    </body>
    </html>
  • Error message? What is the column type of `image`? Is it `BLOB`? Have you considered storing images in your system rather than in your database? Try using `===` instead of `==`. – Logan Wayne Apr 13 '16 at 04:00
  • You should store image in system and store that image folder path in database... – Piyush Gupta Apr 13 '16 at 04:06
  • @Logan Wayne ,this the error message Undefined index: image in C:\wamp\www\imageupload.php on line 37 getimagesize() [function.getimagesize]: Filename cannot be empty in C:\wamp\www\imageupload.php on line 37 and column type is LONGBLOB nd i trying to store image in local server nd i tried using === it is still not working – upasana arora Apr 13 '16 at 04:17
  • @Piyush i want to directly store image in database – upasana arora Apr 13 '16 at 04:19
  • Try this http://stackoverflow.com/questions/26757659/how-to-store-images-in-mysql-database-using-php – Piyush Gupta Apr 13 '16 at 05:10

2 Answers2

0
<?php
 ini_set('mysql.connect_timeout',300);
 ini_set('default_socket_timeout',300);

?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
  die("Database connection failed: " . mysqli_connect_error());
}
else
{
  echo "Connected successfully";
}
 //data upload
 if( isset($_POST['submit'] ))
 {
    mysql_select_db("project",$conn);
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $name=addslashes($_FILES['image']['name']);
    $result = mysql_query("insert into images(name,image) values('.$name.','.$image.')");  
    mysql_close($conn); 
    }?>
    </body>
    </html>
ghaziksibi
  • 471
  • 3
  • 12
0

Edit this line

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Maulik
  • 877
  • 1
  • 8
  • 17
  • a new warning came,Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\wamp\www\imageupload.php on line 58 and Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\wamp\www\imageupload.php on line 58 and still the image is not stored in database – upasana arora Apr 13 '16 at 04:24
  • ok replace this code $uploads_dir = '/uploads'; $image= addslashes($_FILES['image']['tmp_name']); $name=addslashes($_FILES['image']['name']); $image=file_get_contents($image); $image= base64_encode($image); move_uploaded_file($image, "$uploads_dir/$name"); – Maulik Apr 13 '16 at 04:26
  • still warning,Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\wamp\www\imageupload.php on line 66 and Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\wamp\www\imageupload.php on line 66 – upasana arora Apr 13 '16 at 04:31
  • create uploads folder in your project – Maulik Apr 13 '16 at 04:33
  • oh,i removed saveimage function now its working,but i want to save image in database.please help – upasana arora Apr 13 '16 at 04:35
  • ok chenge insert query $result = mysql_query("insert into images(name) values('$name')"); – Maulik Apr 13 '16 at 04:45
  • after creating folder also image is not saving in folder. – upasana arora Apr 13 '16 at 04:45
  • saveimage($name,$image); line is required – Maulik Apr 13 '16 at 04:54
  • still not saving in a folder – upasana arora Apr 13 '16 at 04:57
  • $result = mysql_query("insert into images(name) values('$name')")or die("cant insert"); and tell me what is output – Maulik Apr 13 '16 at 05:05
  • the output is -cant insert – upasana arora Apr 13 '16 at 05:09
  • check your database name – Maulik Apr 13 '16 at 05:14
  • database name is "project" – upasana arora Apr 13 '16 at 05:16
  • in your data base run this query CREATE TABLE images ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, image VARCHAR(40) NOT NULL, PRIMARY KEY ( id )); ); – Maulik Apr 13 '16 at 05:17
  • #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '". "id INT NOT NULL AUTO_INCREMENT, ". "name VARCHAR(100) NOT NULL, ". "image VA' at line 1 this error came – upasana arora Apr 13 '16 at 05:20
  • remove your old table who's name is images after run this query – Maulik Apr 13 '16 at 05:20
  • oh,thanx its working,but what was the reason it was not working and now how to store the image in database – upasana arora Apr 13 '16 at 05:29
  • mistake in your database – Maulik Apr 13 '16 at 05:30
  • the image is not uploading,the query with name is working earlier query is not working. – upasana arora Apr 13 '16 at 05:33
  • did you create folder the name is "uploads" in your project directory – Maulik Apr 13 '16 at 05:34
  • yes,i made that.this query is working $result = mysql_query("insert into images(name) values('$name')")or die("cant insert"); and this is not $result = mysql_query("insert into images(name,image) values('$name','$image')"); – upasana arora Apr 13 '16 at 05:35
  • can u help me answering another question,http://stackoverflow.com/questions/36590041/to-retrieve-the-image-from-the-database-which-is-stored-in-binary-format – upasana arora Apr 13 '16 at 06:33