将上传的文件名插入数据库表

时间:2014-12-28 16:00:10

标签: php zend-framework2

我正在尝试将上传的文件名插入到包含上传日期的表中,但是我尝试使用$ _FILES

获取文件名的值时遇到了一些错误

这是我的代码:

  public function uploadAction()
  {
    if (!$user = $this->identity()) {
        return $this->redirect()->toUrl('/login/log');
    }


    $user = $this->identity();
    $layout = $this->layout();

    $layout->setVariable('user1', $user->username);


    $form = new FileUploadForm();

    $request = $this->getRequest();

    if ($request->isPost()) {
        $file = new File();
        $form->setInputFilter($file->getInputFilter());

        $captions = $request->getPost()->toArray();
        $get_file = $this->params()->fromFiles('file');


        $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());

        $form->setData($data);

        if ($form->isValid()) {
            $size = new Size(array('min' => '10kB', 'max' => FileHandler::FILESIZE . 'MB'));
            $extension = new Extension(array('jpg', 'jpeg', 'png'), true);

            $adapter = new Http();
            $adapter->setValidators(array($size, $extension), $get_file['name']);

            if (!$adapter->isValid()) {
                return $this->redirect()->toUrl('/admin/upload-failure');
            } else {
                $dir_check = !is_dir(FileHandler::UPLOAD_PATH . $user->username)
                ?
                   mkdir(FileHandler::UPLOAD_PATH . $user->username) ? FileHandler::UPLOAD_PATH . $user->username : null

                : FileHandler::UPLOAD_PATH . $user->username;

                $adapter->setDestination($dir_check);
                if ($adapter->receive($get_file['name'])) {
                    $this->getFileUploadFactory()->insertUploadDate($_FILES);
                    $file->exchangeArray($form->getData());
                    return $this->redirect()->toUrl('/admin/upload-success');
                } else {
                    return $this->redirect()->toUrl('/admin/upload-failure');
                }
            }
        }
    }


  public function insertUploadDate(array $file)
  {
    try {
        $insert = new Insert('uploads');

        foreach ($file as $key => $value) {
            $insert->columns(array('filename', 'upload_date'))
                ->values(array('filename' => $value, 'upload_date' => date('Y-m-d')));

            $adapter = $this->table_gateway->getAdapter();

            $adapter->query(
                $this->sql->getSqlStringForSqlObject($insert),
                $adapter::QUERY_MODE_EXECUTE
            );
        }

        return true;
    } catch (\PDOException $e) {
        // save the exception message to the error file
        $writer = new Stream(self::ERROR_PATH);
        $logger = new Logger();
        $logger->addWriter($writer);

        $logger->info($e->getMessage() . "\r\r");

        return false;
    }
}

然后在控制器中我这样称呼它:

 $this->getFileUploadFactory()->insertUploadDate($_FILES);

就像我说的,它没有插入我上传的文件的正确名称(使用html5多个上传选项)

谢谢!

0 个答案:

没有答案