繁体   English   中英

在php中上传文件的验证

[英]validation for file upload in php

您好,我试图限制文件上传,如果文件大小超过特定大小,但我的问题是,如果文件大小超过最大大小,我在php服务器中没有得到有关文件的任何信息,我的HTML代码如下:以下请帮助我为什么我没有得到信息。

<?php echo $form->create('Add', array('id'=>'addFile', 'enctype'=>'multipart/form-data', 'url'=>'/content/upload?path='.$contentItem['url_path']),array(         
    'inputDefaults' => array('label' => false))); ?>    
<div class="_100 formText">
    <label>Section&nbsp;: </label>
     <input type="input" value="<?php echo (!empty($contentItem['display_text'])) ? $contentItem['display_text'] : $contentItem['parent_disp_txt'] ;?>" name="data[Add][section]" readonly="readonly"/>
</div>
<div class="clear" style="height: 5px;"></div>
<div class="_100 formText">
    <label>File Name&nbsp;: </label>
     <input type="input" value="" name="data[Add][file]" class="filePath"/>
</div>
<div class="clear" style="height: 5px;"></div>
<div class="_99 formText">
    <label>File Description&nbsp;: </label>
    <textarea rows="3" cols="" name="data[Add][description]" class="description" style="resize:none;"></textarea>
</div>
<div class="clear" style="height: 15px;"></div>
<div class="_99 formText">
    <label>Select File to Upload: </label>
    <input type="file" name="data[Add][upload]" class="filesToUpload" id="file"/>
</div>
<?php echo $html->tags['formend'];?>    
if(isset($_FILES['uploaded_file'])) {
    $errors     = array();
    $maxsize    = 2097152;
    $acceptable = array(
        'application/pdf',
        'image/jpeg',
        'image/jpg',
        'image/gif',
        'image/png'
    );

    if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 2 megabytes.';
    }

    if(!in_array($_FILES['uploaded_file']['type'], $acceptable) && (!empty($_FILES["uploaded_file"]["type"]))) {
    $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}

if(count($errors) === 0) {
    move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
} else {
    foreach($errors as $error) {
        echo '<script>alert("'.$error.'");</script>';
    }

    die(); //Ensure no more processing is done
}
}

试试这个代码。

暂无
暂无

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

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