duancheng8000 2015-11-25 05:41
浏览 18
已采纳

如果选择了文件,则上传文件,如果未选择,则在db中使用文件大小验证发送空值

I want to upload file if file is selected and if not selected then send null value in db and if file size is grater then 3 mb send alert msj to user and in that conduction do not update query in db. I have tried this code but having problem in condutional statement please help me out with this with suitable code my input type file tag name is AttachBills:

if(isset($_POST["AddUnit"])){
    echo "<script>alert(' in page subtion');</script>";

    function test_input($data)
        {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        } 
    $InvoiceNumber=$StockName=$UnitsAdded=$TotalCost=$AttachBills=$PaymentMode=
    $PurchaseDate=$Comments="";

    $InvoiceNumber    =    test_input($_POST["InvoiceNumber"]);
    $StockName        =    test_input($_POST["StockName"]);
    $UnitsAdded       =    test_input($_POST["UnitAdded"]);
    $TotalCost        =    test_input($_POST["TotalCost"]);            
    $PaymentMode      =    test_input($_POST["PaymentMode"]);   
    $PurchaseDate     =    test_input($_POST["PurchaseDate"]);
    $Comments         =    test_input($_POST["Comments"]);
    $uploadOk         =    1;

    if($_FILES['AttachBills']['size'] > 0 ) {
        echo "<script>alert('their is a file attachbills');</script>";
        $Bill_File_Name=$_FILES["AttachBills"]["name"];
        $Bill_Tmp=$_FILES["AttachBills"]["tmp_name"];
        $Bill_Size=$_FILES["AttachBills"]["size"]/1024;

        if($Bill_Size<=200) {
            //New file name
            echo "<script>alert(' filesize is less then 200 coundutoion');</script>";
            //File upload path
            $Bill_Path="uploads/" . $Bill_File_Name;

            //function for upload file
            if(move_uploaded_file($Bill_Tmp,$Bill_Path)){
                $random=rand(1111,9999);
                $Attach_Name=$random.$Bill_File_Name; 
                echo "<script>alert(' in if moved coundution');</script>";
            } else {
                echo "<script>alert('Problem With FileUpload. Please Try Again');</script>";
            }
        } else {
            echo "<script>alert('Maximum upload file size limit is 200 kb');</script>";
            $uploadOk=0;
        }
    } else {
        if($uploadOk==1) {
            echo "<script>alert(' in uploodad ok 1');</script>";
            $UnitQuery="INSERT INTO `inventory_add_unit`(`InvoiceNumber`, `StockName`, `UnitsAdded`, `TotalCost`, `AttachBills`, `PaymentMode`, `PurchaseDate`, `Comments`) VALUES ('$InvoiceNumber','$StockName','$UnitsAdded','$TotalCost','$AttachBills','$PaymentMode','$PurchaseDate','$Comments')";
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duanliexi1052 2015-11-25 06:19
    关注

    Possible error

    1. enctype="multipart/form-data" is missing
    2. php.ini might not have file_uploads = on set
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘