繁体   English   中英

我想通过php上传数据库中的图片,但图片未上传到数据库中

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

我尝试上传图像,但是当getimagesize图像为空时,它返回false ..并且警告即将来临,它没有保存在数据库中。数据库名称是project,表名称是images,字段是name和image。一个代码...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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