【问题标题】:\Zend\File\Transfer\Adapter\Http() and filerenameupload\Zend\File\Transfer\Adapter\Http() 和文件重命名上传
【发布时间】:2014-03-19 09:03:15
【问题描述】:

我正在尝试将 'filerenameupload' 过滤器与 \Zend\File\Transfer\Adapter\Http() 一起使用:

    $adapter = new \Zend\File\Transfer\Adapter\Http();

    $adapter->addFilter('filerenameupload', array(
        'target' => BASE_DIR . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR .
        'img' . DIRECTORY_SEPARATOR . 'gallery' .
        DIRECTORY_SEPARATOR . 'image.jpg',
        'randomize' => true,
    ));

    var_dump($adapter->isValid()); // true

    if (!$adapter->receive()) {
        $messages = $adapter->getMessages();
        echo implode("\n", $messages);
    }

总是出错,

无法重命名文件“/tmp/somefile.png”。处理文件时出错。

/tmp/somefile.png - 存在,可读可写,扩展文件夹也可读可写

错误来自类 RenameUpload:

protected function moveUploadedFile($sourceFile, $targetFile)
{
    ErrorHandler::start();
    $result = move_uploaded_file($sourceFile, $targetFile);
    $warningException = ErrorHandler::stop();
    if (!$result || null !== $warningException) {
        throw new Exception\RuntimeException(
        sprintf("File '%s' could not be renamed. An error occurred while processing the file.", $sourceFile), 0, $warningException
        );
    }

    return $result;
}

所以也许有人有一个例子或知道如何解决这个错误?还是我做错了什么?

【问题讨论】:

    标签: php file-upload zend-framework2


    【解决方案1】:

    感谢来自#zftalk 的@hemangpatel 的建议,因此工作操作如下:

    public function uploadAction()
    {
        $adapter = new \Zend\File\Transfer\Adapter\Http();
    
        $adapter->setDestination(BASE_DIR . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR .
                'img' . DIRECTORY_SEPARATOR . 'gallery' . DIRECTORY_SEPARATOR);
        // Returns all known internal file information
    
        $adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() .
            DIRECTORY_SEPARATOR . rand(2, 10) . '.jpeg',
            'overwrite' => true));
    
        if (!$adapter->receive()) {
            $messages = $adapter->getMessages();
            return new ViewModel(['messages' => $messages]);
        } else {
            $this->flashMessenger()->addSuccessMessage('Upload success');
    
            $this->redirect()->toRoute('admin', ['controller' => 'gallery',
                'action' => 'index']);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2015-09-09
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多