PHP文件上传表单验证

时间:2016-03-27 20:37:02

标签: php forms validation

所以我想读一下如果用户没有在表单提交上传文件,它之前有效,但由于某种原因它不再有效。它说我的错误vars $errorImg& $errorThumb未定义。但其他设置相同的人都很好。我必须在某个地方制造一个恐怖,我似乎无法找到它。

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

 <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"> 

1 个答案:

答案 0 :(得分:0)

虽然上述方法有效,但看起来更稳定

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'];
 }