2011-03-15 73 views
2

我花了幾個小時試圖向我的Zend應用程序添加一個簡單的上傳文件選項。我有雙重檢查所有必要的權限,一切正常。很簡單,我把它上傳到一個臨時文件夾,但是一旦我把它放在臨時文件夾中,我就無法將它移動到永久存儲位置。下面是不斷失敗的代碼...Zend move_uploaded_file失敗

準確地說,代碼失敗並顯示$ uploaded die語句。我認爲這可能是一個問題,因爲我將它發送給Model,而不是在Action中正確處理它,但也沒有解決問題。任何人都可以將我指向正確的方向嗎?我無法直接將文件從臨時文件中移出,並進入我想要的永久存儲位置。

謝謝!

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

    } 

} 

塊#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; 
} 

回答

4

方法移到使用move_uploaded_file()該文件。所以你使用的文件不是已上傳了,這是正常的文件。您應該使用標準copy()函數。

+0

就是這樣。非常感謝你! – 2011-03-15 21:19:44

+0

np,很樂意幫忙;) – 2011-03-16 10:05:29