简体   繁体   English

PHP文件上传表单验证

[英]PHP File upload form verification

So i want to read if the user did not upload a file on form submission, it work before but for some reason it no longer works. 因此,我想阅读一下,如果用户未在表单提交时上传文件,它之前就可以工作,但是由于某种原因它不再起作用。 It says my error vars $errorImg & $errorThumb Are undefined. 它说我的错误变量vars $errorImg$errorThumb是未定义的。 but the others with the same set up are fine. 但其他设置相同的人都可以。 I must be making an eror somewhere, i just can't seem to find it. 我一定在某个地方犯了错误,但我似乎找不到它。

PHP 的PHP

    $newChair_name='';
    $newPrice='';
    $newManu='';
    $newRegion='';
    $newStock='';
    $newThumb='';
    $newimg='';


if($_POST){
    $all_valid = true;

    if(empty($_POST['uChair_name'])){
        $all_valid = false;
    $errorNames= 'Please enter a name';
    }else{

    $newChair_name = $_POST['uChair_name'];
    }

    if(empty($_POST['uprice'])){
            $all_valid = false;

        $errorPricess = 'You must enter a price';

    }else if(!is_numeric($_POST['uprice'])){
                $all_valid = false;

        $newPrice = 'Price must be a number';
    }else{

        $newprice=$_POST['uprice'];
        }

    if(empty($_POST['uManu'])){
            $all_valid = false;

        $errorManu = 'We need to know the manufacturer';

    }else{

        $newManu=$_POST['uManu'];
    }

    if(empty($_POST['uRegion'])){
            $all_valid = false;

        $errorRegion = 'We need to know the region made';

    }else{

        $newRegion=$_POST['uRegion'];
    }


    if(empty($_FILES['uthumb'])){ //not being read
            $all_valid = false; 

        $errorThumb = 'We need a thumbnail'; 
    }else{ 
       $newThumb=$_FILES['uthumb'];


    } 

    if(empty($_FILES['ulrgthumb'])){ 
            $all_valid = false;
         $errorImg = 'We need a large image'; 
    }else{ 

         $newimg=$_FILES['ulrgthumb'];
     }  


}

HTML 的HTML

 <form class="chair_form" action="<?php //echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">

    <ul>
    <li>
    <label for="uChair_name">Chair Name:</label>
    <input type="text" name="uChair_name" placeholder="Azual blue" value="<?php echo $newChair_name; ?>" />
    <div class="errorAdd"><?php echo $errorNames; ?></div>

    </li>

    <li>
    <label for="uprice">Price:</label>
    <input type="text" name="uprice" placeholder="999.99" value="<?php echo $newPrice; ?>" />
    <div class="errorAdd"><?php echo $errorPricess; ?></div>
    </li>

    <li>
    <label for="uManu">Manufacturer:</label>
    <input type="text" name="uManu" placeholder="America" value="<?php echo $newManu; ?>" />
    <div class="errorAdd"><?php echo $errorManu; ?></div>

    </li>

    <li>
    <label for="uRegion">Region:</label>
    <input type="text" name="uRegion" placeholder="China" value="<?php echo $newRegion; ?>" />
               <div class="errorAdd"><?php echo $errorRegion; ?></div>

    </li>

    <li>
    <label for="uStock">Stock:*</label>
    <input type="number" name="uStock" placeholder="200"  value="<?php echo $newStock; ?>" />

    </li>

    <li>

    <label for="uthumb">Image of chair 120 x 200:</label>
    <input type="file" name="uthumb"/>
    <div class="errorAdd"><?php echo $errorThumb; ?></div>
    </li>

     <li>
    <label for="uthumb">Large Image of chair 400 x 500:</label>
    <input type="file" name="ulrgthumb"/>
    <div class="errorAdd"><?php echo $errorImg; ?></div>
    </li>


     <li>
    <label for="uDescrip">Description:*</label>
    <textarea  rows="6" cols="27" name="uDescrip"></textarea>  
    </li>


    </ul>
    <input type="submit"> 

while the above method had worked, this appears more stable 尽管上述方法有效,但看起来更稳定

if(empty($_FILES['uthumb']['name'])){ 
     $all_valid = false;
     $errorThumb = 'We need a large image'; 
}else{ 
    $newThumb=$_FILES['uthumb'];
 }

 if(empty($_FILES['ulrgthumb']['name'])){ 
     $all_valid = false;
     $errorImg = 'We need a large image'; 
}else{ 
    $newimg=$_FILES['ulrgthumb'];
 }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM