开发者

Is it possible to set name, type, size for Zend File Transfer Adapter?

I want to开发者_如何转开发 set name, type, size for the Zend_File_Transfer_Adapter_Http object manually, is it possible?


Absolutely. But there is some confusion in your statement. I suppose you mean to limit the type and size for an uploaded file, because otherwise you could just create a file easily with the name "sampleFile.ext", and fill content with spaces for specific size, I can't see that of anything useful though.

Start with a new object:

$uploaded_file = new Zend_File_Transfer_Adapter_Http();

File size validators:

// 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),
    ));

Extension validators:

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

Exclude file extension validators:

// 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));

To change file name, please use filter: Zend_File_Transfer_Http How to set uploaded file name

Official reference for file validators: http://framework.zend.com/manual/1.11/en/zend.file.transfer.validators.html#zend.file.transfer.validators.extension

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜