简体   繁体   中英

I Want to Upload an image in database through php,the image is not uploading in database

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>
<?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>

编辑此行

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM