Zend没有上传文件

时间:2015-04-12 01:36:10

标签: php zend-framework file-upload

我正在尝试使用zend上传文件,但似乎无法理解为什么这段代码不起作用。它不会显示在我指定的目录中。你能帮忙的话,我会很高兴。谢谢!

public function uploadFormAction()
{
  $form     = new UploadForm('upload-form');
    $tempFile = null;

    $prg = $this->fileprg($form);
    if ($prg instanceof Response) {
        return $prg; // Return PRG redirect response
    } elseif (is_array($prg)) {
        if ($form->isValid()) {
            $data = $form->getData();

           // echo $data['image-file']['name'];

            $tmp = $data['image-file']['tmp_name'];
            $name = $data['image-file']['name'];
            $uploads_dir = '//Applications/MAMP/htdocs/';


             move_uploaded_file($tmp, "$uploads_dir");

             echo($name);
             echo($uploads_dir);



            // Form is valid, save the form!
           // return $this->redirect()->toRoute('uploadForm/success');
        } else {
            // Form not valid, but file uploads might be valid...
            // Get the temporary file information to show the user in the view
            $fileErrors = $form->get('image-file')->getMessages();
            if (empty($fileErrors)) {
                $tempFile = $form->get('image-file')->getValue();
            }
        }
    }

    return array(
        'form'     => $form,
        'tempFile' => $tempFile,
    );
}

HTML FORM - upload-form.html

 $title = 'Upload';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>
 <?php
 $form->setAttribute('action', $this->url('album', array('action' => 'uploadForm')));
 $form->prepare();?>

 <?php echo $this->form()->openTag($form); ?>

    <div class="form-element">
        <?php $fileElement = $form->get('image-file'); ?>
        <?php echo $this->formLabel($fileElement); ?>
        <?php echo $this->formFile($fileElement); ?>
        <?php echo $this->formElementErrors($fileElement); ?>
    </div>

    <button>Submit</button>

<?php echo $this->form()->closeTag(); ?>

2 个答案:

答案 0 :(得分:0)

它接缝您忘记将enctype添加到<form>

  

$ form-&gt; setAttribute(“enctype”,“multipart / form-data”);

答案 1 :(得分:0)

似乎您的目标目录路径不正确。

$uploads_dir = '//Applications/MAMP/htdocs/';

这应该是:

$uploads_dir=realpath(APPLICATION_PATH) . "/../public/Your directory path

您可以使用 is_dir 功能查看路径。