在zend-framework中将图像上传到文件夹时出错

问题描述

嗨,我正在尝试将图像文件上传到zend框架中的文件夹中,但出现HTTP 400错误。 原始项目在这里https://github.com/nginadofficial/nginad/blob/master/upload/module/DashboardManager/src/DashboardManager/Controller/DemandController.php

这是我的控制器...

public function uploadcreativeAction() {
    
        $initialized = $this->initialize();
        if ($initialized !== true) return $initialized;
    
        $creatives_dir = 'public/creatives/' . $this->auth->getUserID() . '/';
        
        if (!file_exists($creatives_dir)):
            mkdir($creatives_dir,0755,true);
        endif;
        
        $site_url = $this->config_handle['delivery']['site_url'];
        
        if(substr($site_url,-1) == '/'):
            $site_url = substr($site_url,-1);
        endif;
        
        $files =  $this->request->getFiles()->toArray();
        $httpadapter = new \Zend\File\Transfer\Adapter\Http();
        $filesize  = new \Zend\Validator\File\Size(array('max' => 2000000 )); //2MB
        $extension = new \Zend\Validator\File\Extension(array('extension' => array('jpg','jpeg','png','gif','swf')));
        $httpadapter->setValidators(array($filesize,$extension),$files['file']['name']);
        $ext = pathinfo($files['file']['name'],PATHINFO_EXTENSION);
        $newName = md5(rand() . $files['file']['name']) . '.' . $ext;
        $httpadapter->addFilter('File\Rename',array(
                'target' => $creatives_dir . $newName,'overwrite' => true
        ));
        if($httpadapter->isValid()):
            if($httpadapter->receive($files['file']['name'])):
                $httpadapter->getFilter('File\Rename')->getFile();
                $newfile = $httpadapter->getFileName();
                header("Content-type: text/plain");
                echo $site_url . substr($newfile,strlen('public'));
                exit;
            endif;
        endif;
        $error = array();
        $dataError = $httpadapter->getMessages();
        foreach($dataError as $key=>$row):
            $error[] = $row;
        endforeach; 
        http_response_code(400);
        header("Content-type: text/plain");
        echo implode(',',$error);
        exit;

    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)