2011-07-07 54 views

回答

1

絕對。但是你的陳述中有一些混淆。我想你的意思是限制上傳文件的類型和大小,否則你可以用名稱「sampleFile.ext」輕鬆創建一個文件,並用特定大小的空格填充內容,我看不到任何東西雖然有用。

開始一個新的對象:

$uploaded_file = new Zend_File_Transfer_Adapter_Http(); 

文件大小驗證:

// Set a file size with 20000 bytes 
    $upload->addValidator('Size', false, 20000); 

    // Set a file size with 20 bytes minimum and 20000 bytes maximum 
    $upload->addValidator('Size', false, array('min' => 20, 'max' => 20000)); 

    // Set a file size with 20 bytes minimum and 20000 bytes maximum and 
    // a file count in one step 
    $upload->setValidators(array(
      'Size' => array('min' => 20, 'max' => 20000), 
      'Count' => array('min' => 1, 'max' => 3), 
    )); 

擴展驗證:

// Limit the extensions to jpg and png files 
    $upload->addValidator('Extension', false, 'jpg,png'); 

    // Limit the extensions to jpg and png files but use array notation 
    $upload->addValidator('Extension', false, array('jpg', 'png')); 

    // Check case sensitive 
    $upload->addValidator('Extension', false, array('mo', 'png', 'case' => true)); 
    if (!$upload->isValid('C:\temp\myfile.MO')) { 
     print 'Not valid because MO and mo do not match with case sensitivity;'; 
    } 

排除的文件擴展名驗證:

// Do not allow files with extension php or exe 
    $upload->addValidator('ExcludeExtension', false, 'php,exe'); 

    // Do not allow files with extension php or exe, but use array notation 
    $upload->addValidator('ExcludeExtension', false, array('php', 'exe')); 

    // Check in a case-sensitive fashion 
    $upload->addValidator('ExcludeExtension', 
      false, 
      array('php', 'exe', 'case' => true)); 
    $upload->addValidator('ExcludeExtension', 
      false, 
      array('php', 'exe', 'case' => true)); 

要更改文件名,請使用過濾器:Zend_File_Transfer_Http How to set uploaded file name

官方參考文件驗證:http://framework.zend.com/manual/1.11/en/zend.file.transfer.validators.html#zend.file.transfer.validators.extension

相關問題