dongtong848825 2018-03-02 04:58
浏览 38
已采纳

Php上传文件如果无效则设置其他值而不是随机字

This script is working fine.. but i just wonder how do i change the value insert into database if no image selected to value "none" instead of random words like 15199660151398490708

<?php
    $name = ''; $type = ''; $size = ''; $error = '';
    $upload = "asssets/img/";
    $new = time().rand();
    $target =  $upload . $new . basename( $_FILES['photo']['name']);

    require_once('includes/config.php');

    // If form submitted, insert values into the database.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {

        $pic=$new .($_FILES['photo']['name']);

    $query = "INSERT INTO posting(photo) VALUES('$pic')";
        $result = mysql_query($query);
//Writes the photo to the server
if (($_FILES["photo"]["type"] == "image/gif") ||
            ($_FILES["photo"]["type"] == "image/jpeg") ||
            ($_FILES["photo"]["type"] == "image/png") ||
            ($_FILES["photo"]["type"] == "image/pjpeg")) {
            $filename = compress_image($_FILES["photo"]["tmp_name"], $target, 80);
{
        echo "";
        }
    }else
echo "";{
?><form method="post" style="padding:0px;margin:0px;display: block;" enctype="multipart/form-data" autocomplete="off">

<input name="photo" type="file" accept="image/*">
<input name="submit" class="button_post" id="click" type="submit" value=" Post" />

</form><?php } ?>

This is example image mysql phpmyadmin

  • 写回答

1条回答 默认 最新

  • doutang1856 2018-03-02 05:06
    关注

    Check if the file input if empty it's first or an error code is returned because it may be 0 if something went wrong.

    if ($_FILES['photo']['size'] == 0 && $_FILES['photo']['error'] == 0)
    {
    $pic = "none";
    }
    else
    {
    $pic = $new .($_FILES['photo']['name']);
    }
    

    The code will check if the file input is empty. If empty, it will set the $pic variable value to "none" else it will generate random numbers.

    The overall code would then be:

    <?php
        $name = ''; $type = ''; $size = ''; $error = '';
        $upload = "asssets/img/";
        $new = time().rand();
        $target =  $upload . $new . basename( $_FILES['photo']['name']);
    
        require_once('includes/config.php');
    
        // If form submitted, insert values into the database.
    if(isset($_POST['submit']) && !empty($_POST['submit'])) {
    
               if ($_FILES['photo']['size'] == 0 && $_FILES['photo']['error'] == 0)
    {
    $pic = "none";
    }
    else
    {
    $pic = $new .($_FILES['photo']['name']);
    }
    
        $query = "INSERT INTO posting(photo) VALUES('$pic')";
            $result = mysql_query($query);
    //Writes the photo to the server
    if (($_FILES["photo"]["type"] == "image/gif") ||
                ($_FILES["photo"]["type"] == "image/jpeg") ||
                ($_FILES["photo"]["type"] == "image/png") ||
                ($_FILES["photo"]["type"] == "image/pjpeg")) {
                $filename = compress_image($_FILES["photo"]["tmp_name"], $target, 80);
    {
            echo "";
            }
        }else
    echo "";{
    ?><form method="post" style="padding:0px;margin:0px;display: block;" enctype="multipart/form-data" autocomplete="off">
    
    <input name="photo" type="file" accept="image/*">
    <input name="submit" class="button_post" id="click" type="submit" value=" Post" />
    
    </form><?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计