我有一个包含多个类型文件字段的表单。 foto1,foto2,foto3,foto4,foto5,foto6 只需要第一个字段。使用您在下面看到的代码上传文件。
在检查表单有效性
之后,在正常的控制器操作中调用此方法private function uploadFile($file,$methodName,$idCategoriaEvento,&$categoriaEventoEntity){
$obj = new \stdClass();
try{
//unset($_FILES['fotoGrande'],$_FILES['fotoBanner'],$_FILES['foto1'],$_FILES['foto2'],$_FILES['foto3'],$_FILES['foto4'],$_FILES['foto5'],$_FILES['foto6']);
if(!empty($file['tmp_name'])){
$getBasePath = new Getbasepath($this);
$this->getCurConfig();
$path_origine = $file['name'];
$ext = pathinfo($path_origine, PATHINFO_EXTENSION);
$file_name = $file['tmp_name'];
$new_file = $getBasePath->getPath().$this->config['ristotram']['categoria_foto_path']. $idCategoriaEvento . "/" . $path_origine;
$upload_obj = new \Zend\File\Transfer\Adapter\Http();
$upload_obj->addFilter('File\Rename',array(
'source' => $path_origine,
'target' => $new_file,
'overwrite' => true
));
if($upload_obj->receive()) {
//$categoriaEventoEntity->$methodName($path_origine);
$obj->s = true;
$obj->i = "";
}else{
$obj->s = false;
$obj->i = implode("<br />",$upload_obj->getErrors());
}
}else{
$obj->s = true;
$obj->i = "";
}
}catch(\Exception $e){
$obj->s = false;
$obj->i = $upload_obj->getErrors();
}
return $obj;
}
方法签名包含以下值
$file, -----> $_FILES['foto1']
$methodName,--------> un metodo della mia entità che invoco per valorizzare la sessione
$idCategoriaEvento->un indice che mi serve per specificare il path,
&$categoriaEventoEntity --------> la mia entità
文件未移至目的地但未显示错误
答案 0 :(得分:0)
好的代码错了
private function uploadFile($file,$methodName,$idCategoriaEvento,&$categoriaEventoEntity){
$obj = new \stdClass();
try{
if(!empty($file['tmp_name'])){
$this->getCurConfig();
$file_name = $file['name'];
$file_temp_name = $file['tmp_name'];
$new_file = $this->plgBasePath->getPath().$this->config['ristotram']['categoria_foto_path']. $idCategoriaEvento . "/" . $file_name;
$upload_obj = new Http();
$upload_obj->addFilter('File\Rename',array(
'source' => $file_temp_name,
'target' => $new_file,
'overwrite' => true
));
if($upload_obj->receive()) {
$categoriaEventoEntity->$methodName($file_name);
$obj->s = true;
$obj->i = "";
}else{
$obj->s = false;
$obj->i = implode("<br />",$upload_obj->getErrors());
}
}else{
$obj->s = true;
$obj->i = "";
}
}catch(\Exception $e){
$obj->s = false;
$obj->i = $upload_obj->getErrors();
}
return $obj;
}