图像未在MySQL中上传

时间:2017-03-07 06:43:42

标签: php html mysql html5

这一切都始于我得到一个未识别的索引错误:图像。然后我把支票放进去,所以我不必再看错了。但我知道图像没有上传。我使用合法的图像来测试,没有任何工作。我这几个小时一直在努力。请帮忙。

<html>
    <body>
      <form action="images-add.php" method="post" enctyp="multipart/form-data">
        <input type="file" name="image" />

        <input type="submit" name="sum" value="upload" />
      </form>
    </body>
</html>
<?php 
include('connection.php');

if(isset($_POST['image'])) {

if(empty($_FILES) || !isset($_FILES['image'])){
echo "Please Add an Image";
}else{
if(getimagesize($_FILES['image']['tmp_name']) == FALSE){
            $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){
      $con= mysql_connect("localhost", "root", ""); 
       mysql_select_db("hw2", $con);
        $qry= "insert into images (name, image) values ('$name', '$image')"; 
        $result= mysql_query($qry,$con); 
    if($result) {
     echo "<br/> Image Uploaded.";   
    }else{
        echo "Not Uploaded";     
    }
  }
?>

1 个答案:

答案 0 :(得分:0)

请在HTML表单中将enctyp更改为enctype。 还要更新代码 从

$image= addslashes($_FILES(['image']['tmp_name']));   

$image= addslashes($_FILES['image']['tmp_name']);

和 这行代码,来自

$name= addslashes($_FILES(['image']['name']));

$name= addslashes($_FILES['image']['name']);

完整代码如下......

<html>
    <body>
      <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="image" />

        <input type="submit" name="sum" value="upload" />
      </form>
    </body>
</html>
<?php 
include('connection.php');

if($_POST){
    if(empty($_FILES) || !isset($_FILES['image'])){
        echo "Please Add an Image";
    }else{
        if(getimagesize($_FILES['image']['tmp_name'])){

                $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){
    $con= mysql_connect("localhost", "root", ""); 
    mysql_select_db("hw2", $con);
    $qry= "insert into images (name, image) values ('$name', '$image')"; 
    $result= mysql_query($qry,$con); 

    if($result) {
        echo "<br/> Image Uploaded.";   
    }else{
        echo "Not Uploaded";     
    }
}
?>

我希望此代码适合您。