是否可以为Zend File Transfer Adapter设置名称,类型和大小?

时间:2011-07-07 08:27:44

标签: file zend-framework http adapter transfer

我想手动为name对象设置typesizeZend_File_Transfer_Adapter_Http,是否可以?

1 个答案:

答案 0 :(得分: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