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>
5
  • 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 ==. Commented Apr 13, 2016 at 4:00
  • You should store image in system and store that image folder path in database... Commented Apr 13, 2016 at 4: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 Commented Apr 13, 2016 at 4:17
  • @Piyush i want to directly store image in database Commented Apr 13, 2016 at 4:19
  • Try this stackoverflow.com/questions/26757659/… Commented Apr 13, 2016 at 5:10

2 Answers 2

0

Edit this line

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

22 Comments

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
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");
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
create uploads folder in your project
oh,i removed saveimage function now its working,but i want to save image in database.please help
|
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>

4 Comments

warnings,Undefined index: image in C:\wamp\www\imageupload.php on line 27 and Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in C:\wamp\www\imageupload.php on line 27 and Notice: Undefined index: image in C:\wamp\www\imageupload.php on line 28
$name=basename($_FILES['image']['name']);
it did not made any change
change enctype from "multipart/formdata" to "multipart/form-data"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.