【问题标题】:PHP file upload size & type not workingPHP文件上传大小和类型不起作用
【发布时间】:2016-06-25 19:46:37
【问题描述】:

实际上,我已经开发了一个 CMS,可以通过...发布到我的网站。 对于发布图像,我想设置 500KB 的限制,并且只允许使用 GIF、JPEG、JPG 和 PNG 格式。此代码执行成功并且警报也出现了。但是当上传包含​​允许的格式和大小的图像时,它会再次显示警报消息..我在这里使用exit();function 是正确的吗? HTML 是:

<div class="form-container">
   <form method="post" action="publish_post.php" enctype="multipart/form-data">
     <input class="inputs" type="text" name="p_title" placeholder="What's the post title"/>
     <input class="inputs" type="text" name="p_author" placeholder="Who's the post author"/>
     <select class="input" name="p_category" size="1" />
        <option value="null">Select post category</option>
        <option value="softwares">Softwares</option>
        <option value="tips & tricks">Tips & tricks</option>
        <option value="wallpapers">Wallpapers</option>
        <option value="walpapers_more">Walpapers_more</option>
     </select>
     <textarea name="p_content" placeholder="What's the post content"></textarea>
     <textarea name="p_misc" placeholder="Enter an html code"></textarea>
     <textarea name="p_video" placeholder="Enter an embed code"></textarea>
     </br>
     <div class="upload">
     <input type="file" name="p_image" title="Pick an image for post"/>
     </div></br>
      <span style="margin-left: 5px; color: gray; font-family: sans-serif; font-size: .8em; ">max. size 5MB
      </span></br></br>
     <input type="submit" name="pub" value="Publish"/>
     <input type="button" name="cancel" value="Cancel" onclick="window.open('dashboard','_self');"/>
   </form>
 </div>

PHP 是:

<?php
if(isset($_POST['pub'])){

    $category = mysqli_real_escape_string($con,$_POST['p_category']);
    $author = mysqli_real_escape_string($con,$_POST['p_author']);
    $title = mysqli_real_escape_string($con,$_POST['p_title']);
    $content = mysqli_real_escape_string($con,$_POST['p_content']);
    $image_name = $_FILES['p_image']['name'];
    $image_type = $_FILES['p_image']['type'];
    $image_size = $_FILES['p_image']['size'];
    $image_tmp = $_FILES['p_image']['tmp_name'];
    $date = date("M,d,Y");
    $misc = $_POST['p_misc'];
    $video = $_POST['p_video'];

    if($image_type != "jpg" && $image_type != "png" && $image_type != "gif" && $image_type != "jpeg" && $image_size>500000)
    {
        echo "<script>alert('Image size is larger or invlaid format');</script>";
        exit();
    }

$query = "INSERT INTO `me`(`post_category`, `post_author`, `post_title`, `post_content`, `post_image`, `post_date`, `post_misc`, `post_video`) VALUES ('$category','$author','$title','$content','$image_name','$date','$misc','$video')";

    if(mysqli_query($con,$query))
    {
    move_uploaded_file($image_tmp,"images/$image_name");
    header("location:dashboard?done2=done2.png");
    }
  } 
?>

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    //图片类型

    $expensions= array("jpeg","jpg","png","gif");
    if(in_array($image_type,$expensions)=== false){
        echo "<script>alert('Image size is larger or invlaid format');</script>";
        exit();
    }
    

    //图片大小

    if($image_size  > 500000){
        echo "<script>alert('Image size is larger or invlaid format');</script>";
        exit();
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      在 if 条件下试试这个:

      unset($_POST);  
      exit("<script>alert('Image size is larger or invlaid format');</script>");
      

      并确保您的插入查询看起来像这样

      $query = "INSERT INTO `me`(`post_category`, `post_author`, `post_title`, `post_content`, `post_image`, `post_date`, `post_misc`, `post_video`) VALUES ('".$category."','".$author."','".$title."','".$content."','".$image_name."','".$date."','".$misc."','".$video."')";
      

      【讨论】:

        【解决方案3】:
        if(($image_type != "image/jpeg" && $image_type != "image/png" && $image_type != "image/gif") || $image_size > 500000)
        {
            echo "<script>alert('Image size is larger or invalid format');</script>";
        
        }
        else
        {
            echo "<script>alert('valid format');</script>";
        }
        

        【讨论】:

          【解决方案4】:

          条件不成立。试试这段代码。如果你有更多的条件,试试这个方法。

          if(($image_type != "jpg") && ($image_type != "png") && ($image_type != "gif") && ($image_type != "jpeg") && ($image_size>500000))
          {
             echo "<script>alert('Image size is larger or invlaid format');</script>";
             exit();
          }
          

          【讨论】:

            猜你喜欢
            • 2016-10-25
            • 1970-01-01
            • 1970-01-01
            • 2015-06-05
            • 2012-06-02
            • 2011-12-15
            • 2017-12-19
            • 2015-11-09
            • 2011-02-11
            相关资源
            最近更新 更多