zend-framework - Zend move_uploaded_file 失败

标签 zend-framework

我花了几个小时尝试向我的 Zend 应用程序添加一个简单的上传文件选项。我已经仔细检查了所有必要的权限,一切正常。很简单,我将其很好地上传到临时文件夹,但是一旦将其放入该临时文件夹,我就无法将其移动到永久存储位置。下面是不断失败的代码...

准确地说,代码因 $uploaded die 语句而失败。我认为这可能是一个问题,因为我将其发送到模型而不是在操作中正确处理它,但这也没有解决问题。有人能指出我正确的方向吗?我只是无法直接将文件从临时存储位置取出并放入我想要的永久存储位置。

谢谢!

//This is the action that is called when form is submitted.

function addImageAction()
{
    $imgForm = new Admin_Form_ImageUploadForm();
    $imgForm->setAction('/admin/media/add-image');
    $imgForm->setMethod('post');
    $this->view->form = $imgForm;

    if($this->getRequest()->isPost())
    {

        if(!$imgForm->image->receive())
        {
            $this->view->message = '<div class="popup-warning">Errors Receiving File.</div>';
            $this->_redirect('/admin/media/add-image');
        }

        if($imgForm->image->isUploaded())
        {
            $imageModel = new Admin_Model_Image();
            $imageId = $imageModel->addImage($imgForm->image->getFileName());
            $this->_redirect('/admin/media/view-image/'.$imageId);              
        }

    }

}

block #2 - 模型

    public function addImage($image)
{
    // Process the New File
    // Check to see if Filename is already in Database
    $select = $this->select();
    $select->where('filename=?', $image);
    $row = $this->fetchRow($select);
    if ($row)
    {
        die("Filename already exists in Database.  Please try another file.");
    }


    // Move file to Storage Directory
        // Check/Create Storage Directoy (YYYYMMDD)

        // Temporarily set MEDIA_DIR
        $mediaDir = APPLICATION_PATH . '/../public/media/uploads/';
        $destinationDir = $mediaDir . date('Ymd');

        if (!is_dir($destinationDir)){
            $storageDir = mkdir($destinationDir);
        }

        // Save Image
        $uploaded = is_uploaded_file($image);
        if (!$uploaded) {
            die("Image has not been uploaded");
        }
        $image_saved = move_uploaded_file($image, $destinationDir);

        if(!$image_saved)
        {
            die("Image could not be moved");
        }

    // Create Alternative Sizes

    // Save Data to Database Tables
    $dateObject = new Zend_Date();

    $row = $this->createRow();  
    $row->filename      = $image;
    $row->date_added    = $dateObject->get(Zend_Date::TIMESTAMP);
    $row->date_modified = $dateObject->get(Zend_Date::TIMESTAMP);
    $row->save();

    // Fetch the ID of the newly created row
    $id = $this->_db->lastInsertId();

    // Retrieve IPTC Data

    // Retrieve EXIF Data

    // Return Image ID  
    return $id;
}

最佳答案

receive() 方法使用 move_uploaded_file() 移动文件。因此,您使用的文件不再上传,它是普通文件。您应该使用标准的 copy() 函数。

关于zend-framework - Zend move_uploaded_file 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5316684/

相关文章:

zend-framework - Zend-form setValue , View 具有空值,例如 &lt;input value ="">

php - 将 HTML 占位符添加到 Zend_Form 中

php - Zend Controller Action,在其他类中获取 Helper?

php - 使用 Sprockets 作为 PHP 应用程序的独立服务

php - 用于 SEO 的 Zend Framework URL 重写

zend-framework - 具有模块化结构的 Zend_ACL?

php - zend http 客户端当前 url

php - Zend + 表关系上的 Doctrine 错误

zend-framework - PHPUnit 和 Zend Framework assertRedirectTo() 问题

php - 如何正确使用 Zend Youtube API?