簡體   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