我花了几个小时尝试在Zend应用程序中添加一个简单的上传文件选项。我已经仔细检查了所有必要的权限,一切正常。很简单,我把它很好地上传到一个临时文件夹但是一旦我把它放在那个临时文件夹中,我就无法将它移动到它的永久存储位置。以下是不断失败的代码......
准确地说,代码在$ uploaded die语句中失败。我认为这可能是一个问题,因为我将它发送到模型而不是在Action中正确处理它,但这也没有解决问题。谁能指出我正确的方向?我只是无法直接将文件从temprorary中取出并进入我想要的永久存储区域。
谢谢!
//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 - Model
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;
}
答案 0 :(得分:4)
receive()
方法使用move_uploaded_file()
移动文件。因此,您使用的文件不再上传,这是正常的文件。您应该使用标准copy()
函数。