如何在文件值上是否存在if语句

时间:2019-08-04 15:03:26

标签: php file-upload

大家好,我正在尝试做某事 我有2个文件,一个是custom.js 其中一个是manage_menu.php

我的custom.js中有html

jQuery("#main_menu_item_edit_div_"+menuid).html("" +

        "<form action='dashboard.php?p=manage_menu&edit=menuitem' method='post' class='form addmenuform' id='adnewmenuitmfrm' enctype='multipart/form-data'>" +
        "<center><img src='"+menuimg+"' alt='Avatar' class='avatar' height='200px' width='200px'></center>" +

        "<input type='hidden' name='rid' id='rid' value='"+menuid+"'>" +
        "<input type='hidden' name='menuid' id='menuid' value='"+mainmenuidd+"'>" +
        "<div class='col50 left twocll'> <p>" +
        "<center><input type='text' name='menu_name' id='menu_name' value='"+menuname+"' required='Name'></center>" +
        "<label alt='Name' placeholder='Name'>" +
        "</label></p> </div> <div class='col50 right twocll'> " +
        "<p><input type='text' name='menu_dsc' id='menu_dsc' value='"+menudesc+"' required=''>" +
        "<label alt='Description' placeholder='Description'></label></p>" +



        "<p><input class='uploadbtn' type='file' name='menu_img' id='menu_img' value='"+menuimg+"' required='Image'></p> " +
        "<p><input type='text' name='menu_imgs' id='menu_imgs' value='"+menuimg+"'>" +
        "<label alt='Item Image' placeholder='Item Image'></label></p>" +


        " </div> <div class='col50 left twocll'> <p>" +
        "<input type='text' name='menu_price' value='"+menuprce+"' id='menu_price' required='Price'>" +
        "<label alt='Price' placeholder='Price'>" +
        "</label><p style='margin-top: 0px !important'>" +
        "<input type='checkbox' name='outofstock' id='require_items' value='1' "+menuoutofstock+" /> " +
        "Out Of Stock</p></p> </div> <div class='col50 right twocll'> <p>" +
        "<input type='submit' value='Update Menu Item'></p>" +
        "</div> <div class='clear'></div></form>");
});

并且在php文件中是

$id = $_POST['rid'];
            $restaurant_menu_id = htmlspecialchars($_POST['menuid'], ENT_QUOTES);
            $name = htmlspecialchars($_POST['menu_name'], ENT_QUOTES);
            $description = htmlspecialchars($_POST['menu_dsc'], ENT_QUOTES);

            $item_image = htmlspecialchars($_POST['menu_img'], ENT_QUOTES);
            $item_final_image = htmlspecialchars($_POST['menu_imgs'], ENT_QUOTES);

            $price = htmlspecialchars($_POST['menu_price'], ENT_QUOTES);
            $outofstock = htmlspecialchars(@$_POST['outofstock'], ENT_QUOTES);

然后

 $temp = explode(".", $_FILES["menu_img"]["name"]);
                    $newfilename = round(microtime(true)) . '.' . 

end($temp);
                move_uploaded_file($_FILES['menu_img']['tmp_name'], './menuimg/' . $newfilename);

                $data = array(
                    "id" => $id,
                    "restaurant_menu_id" => $restaurant_menu_id,
                    "name" => $name,
                    "description" => $description,
                    "item_image" => "https://3marka.com/stores/menuimg/" . $newfilename,
                    "price" => $price,
                    "out_of_order" => $outofstock
                );

我想要的是,如果输入文件具有图像上传的值,并且如果已经上传的文件不需要上传任何文件,则可以保存,而无需使用生成随机名称。就像1651505616一样。当我不放置图像而只是生成随机名称时,会发生什么情况,我希望它只执行图像列上的编辑而无需编辑

1 个答案:

答案 0 :(得分:0)

您可以使用file_exists()方法来确定文件是否存在。

if (file_exists('./menuimg/'.$newfilename)
{
   echo "exists!";
}
else
{
   echo "not exists";
}