Php Yii2文件上载返回上载错误部分
创建新项目时图像无法上载,但在更新新创建的项目时,文件已成功上载。有什么原因吗? 趋势新闻模型 用法 应用程序日志显示了这一点Php Yii2文件上载返回上载错误部分,php,apache,yii,yii2,xampp,Php,Apache,Yii,Yii2,Xampp,创建新项目时图像无法上载,但在更新新创建的项目时,文件已成功上载。有什么原因吗? 趋势新闻模型 用法 应用程序日志显示了这一点 $_FILES = [ 'TrendingNews' => [ 'name' => [ 'featuredFile' => 'another_evento.jpg' ] 'type' => [ 'featuredFile' => ''
$_FILES = [
'TrendingNews' => [
'name' => [
'featuredFile' => 'another_evento.jpg'
]
'type' => [
'featuredFile' => ''
]
'tmp_name' => [
'featuredFile' => ''
]
'error' => [
'featuredFile' => 3
]
'size' => [
'featuredFile' => 0
]
]
]
featuredFile不是数据库字段
有什么地方做错了吗?
我使用的是xampp,在实时服务器上试用过,问题是相同的如果您必须在插入时而不是在更新时要求输入文件,那么您应该为其添加一个方案,以便仅在添加新记录时要求您提供文件 其次,您使用的是相同的ActiveRecord模型,并在其中添加了一个自定义属性以用作文件输入,在上载函数中,如果未验证,您将调用validate并返回false,但同时您在控制器操作中没有检查true或false,并且在下一行调用$model->savefalse使用false,从技术上讲,如果存在任何验证错误,您的脚本将永远不会通知用户 文件应在保存记录后上传,而不是在保存记录前上传,以便在出现错误时不会上传额外的文件,尽管您希望插入时需要该文件,因此从技术上讲,如果文件未上传,则不应保存记录,由于这个原因,我们有数据库插入的事务,您应该使用事务块来保存rcord和文件 如果有单独的FormModel用于文件上载,则只应调用validate方法用于文件上载,否则应将实例从UploadedFile加载到model字段,并调用$model->save,它将在保存之前自动验证模型。上传之前,只需检查空文件名,以便当您更新任何记录而不提交文件时,上一个文件应保持原样 您需要首先将验证规则更新为以下内容
/**
* @inheritdoc
*/
public function rules()
{
return [
[['headline_text','news_info', 'news_url', 'author', 'published', 'date_added', 'date_modified'], 'required'],
[['headline_text', 'image_url', 'news_info', 'news_url'], 'string'],
[['date_added', 'date_modified'], 'safe'],
[['author'], 'string', 'max' => 20],
[['published'], 'string', 'max' => 2],
[ [ 'featuredFile' ] , 'required' , 'on' => 'insert' ] ,
[ [ 'featuredFile' ] , 'file' , 'extensions' => 'png, jpg' , 'maxSize' => 200000 , 'tooBig' => 'Limit is 500KB' ] ,
];
}
将上载功能更改为以下内容
public function upload( $ymd , $fileName ) {
if ( $this->featuredFile !== null && $this->featuredFile->name !== '' ) {
$save_path = \Yii::getAlias ( '@backend' ) . '/web/uploads/' . $ymd . '/';
if ( !file_exists ( $save_path ) ) {
mkdir ( $save_path , 0777 , true );
}
if ( !$this->featuredFile->saveAs ( $save_path . $fileName ) ) {
$this->addError ( 'featuredFile' , 'File could not be uploaded' );
throw new \Exception ( 'File upload error' );
}
}
}
然后在模型中添加另一种方法,以在更新时上载新文件时删除旧文件
之后,将创建和更新操作更改为以下内容,以便它们使用事务块进行数据库操作
public function actionCreate() {
$model = new TrendingNews(['scenario'=>'insert']);
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->featuredFile = UploadedFile::getInstance ( $model , 'featuredFile' );
if ( $model->featuredFile !== null ) {
$ymd = date ( "Ymd" );
$fileName = Yii::$app->security->generateRandomString ( 20 ) . '.' . $model->featuredFile->extension;
$model->image_url = $ymd . '/' . $fileName;
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( 'Error Occoured' );
}
$model->upload ( $ymd , $fileName );
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( \Exception $ex ) {
$transaction->rollBack ();
}
}
return $this->render ( 'create' , [
'model' => $model ,
] );
}
public function actionUpdate( $id ) {
$model = $this->findModel ( $id );
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->featuredFile = UploadedFile::getInstance ( $model , 'featuredFile' );
//$oldFile = '';
$oldFile = $model->image_url;
if ( $model->featuredFile !== null ) {
$ymd = date ( "Ymd" );
$fileName = Yii::$app->security->generateRandomString ( 20 ) . '.' . $model->featuredFile->extension;
$model->image_url = $ymd . '/' . $fileName;
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( 'Model error' );
}
$model->upload ( $ymd , $fileName );
$model->unlinkOldFile ( $oldFile );
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( Exception $ex ) {
$transaction->rollBack ();
}
}
return $this->render ( 'update' , [
'model' => $model ,
] );
}
您还需要为您的模型TrendingNews添加代码。featuredFile是模型中的数据库字段还是公共属性?你说它在更新中起作用,更新功能在哪里/action@MuhammadOmerAslam我已更新模型类保存新记录时是否需要上载文件?因为你在模型和控制器中的代码是混乱和矛盾的,在新记录中需要文件谢谢你的时间@Muhammad。我仍然在插入新记录时遇到验证错误。请上传一个文件。对不起,我没有设置插入的场景。请参阅修订的actionCreate第一行,并将场景设置为$model=new TrendingNews['scenario'=>'insert'];,告诉我它是否有效
public function upload( $ymd , $fileName ) {
if ( $this->featuredFile !== null && $this->featuredFile->name !== '' ) {
$save_path = \Yii::getAlias ( '@backend' ) . '/web/uploads/' . $ymd . '/';
if ( !file_exists ( $save_path ) ) {
mkdir ( $save_path , 0777 , true );
}
if ( !$this->featuredFile->saveAs ( $save_path . $fileName ) ) {
$this->addError ( 'featuredFile' , 'File could not be uploaded' );
throw new \Exception ( 'File upload error' );
}
}
}
public function unlinkOldFile( $filename ) {
if ( $filename !== '' ) {
$save_path = \Yii::getAlias ( '@backend' ) . '/web/uploads/' . $filename;
unlink ( $save_path );
}
}
public function actionCreate() {
$model = new TrendingNews(['scenario'=>'insert']);
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->featuredFile = UploadedFile::getInstance ( $model , 'featuredFile' );
if ( $model->featuredFile !== null ) {
$ymd = date ( "Ymd" );
$fileName = Yii::$app->security->generateRandomString ( 20 ) . '.' . $model->featuredFile->extension;
$model->image_url = $ymd . '/' . $fileName;
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( 'Error Occoured' );
}
$model->upload ( $ymd , $fileName );
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( \Exception $ex ) {
$transaction->rollBack ();
}
}
return $this->render ( 'create' , [
'model' => $model ,
] );
}
public function actionUpdate( $id ) {
$model = $this->findModel ( $id );
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->featuredFile = UploadedFile::getInstance ( $model , 'featuredFile' );
//$oldFile = '';
$oldFile = $model->image_url;
if ( $model->featuredFile !== null ) {
$ymd = date ( "Ymd" );
$fileName = Yii::$app->security->generateRandomString ( 20 ) . '.' . $model->featuredFile->extension;
$model->image_url = $ymd . '/' . $fileName;
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( 'Model error' );
}
$model->upload ( $ymd , $fileName );
$model->unlinkOldFile ( $oldFile );
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( Exception $ex ) {
$transaction->rollBack ();
}
}
return $this->render ( 'update' , [
'model' => $model ,
] );
}