dqnz43863 2016-02-06 18:30
浏览 27

如果图像字段为空,则应接受数据

I am writing the query to accept image field only svg,png,jpeg format.But if the image field is empty it is displaying as error while uploading,actually it should insert the remaining fields like name and all if I am not adding image also.Here is my query.

example.php

$title=$_POST['blog_title'];
$result = str_replace(" ", "-", $title);
$description=$_POST['blog_description'];
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$name=$_FILES["image"]["name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$temp=$_FILES["image"]["tmp_name"];
$error=$_FILES["image"]["error"];
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if($error > 0){
    die("error while uploading");
}else{
    $permissible_extension = array("png", "jpg", "jpeg", "svg", "jpg","jpe");
    if(in_array($ext, $permissible_extension)){
        if(move_uploaded_file($temp,"upload/".$name)){
            $sql = mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')");
            if($sql){
                header("Location:blogimage.php");  
                exit();
            }else{
                echo "Insertion failed";
            }
        }else{
            echo "File couldn't be uploaded";
        }
    }else{
        echo "Invalid format";
    }
}
}else{
$sql = mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')");
if($sql){
    header("Location:blogimage.php");  
    exit();
}else{
    echo "Insertion failed";
}
}
  • 写回答

1条回答 默认 最新

  • douji5329 2016-02-06 19:41
    关注

    Firstly, mandatory advice against the use of mysql_* functions. This is deprecated, use mysqli_* functions or PDO instead.

    Now, your issue. If you want to store the other informations about the file even when the filetype is empty or wrong, you will need to do your INSERT before the test.

    Below are some code to you try:

    <?php
    //connect to the database using mysqli
    $name=$_FILES["image"]["name"];
    $type=$_FILES["image"]["type"];
    $size=$_FILES["image"]["size"];
    $temp=$_FILES["image"]["tmp_name"];
    $error=$_FILES["image"]["error"];
    if($error>0)
        die("error while uploading");
    else
    {
        //Insert the informations in your database here, so even without the upload, the info about the file will be stored
        $query = mysqli_query("INSERT INTO blogs(image)values('$name')");
        if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe" || $type==" ")
        {
            move_uploaded_file($temp,"upload/".$name);
            echo "upload complete"; 
        }
        else
        {   
            die("Format not allowed or file size too big!");
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?