famyii
(Federicoariasmallek)
1
I’m trying to delete an image and I get the next error:
unlink(my.app.url/63_0.png) [<a href=‘function.unlink’>function.unlink</a>]: No such file or directory
Here is the code:
unlink(Yii::getPathOfAlias(‘webroot’).’/files/noticias/’.$model->id_noticia."_".$imagenAnterior);
The image is deleted but I still have the error.
Can anyone help me? Thanks!
luizcelso
(Luiz Celso)
2
You’re trying to delete the file by the URL, not ny the physical address.
Happy coding.
outrage
(Site Sense Web)
3
Are you running that code in a loop by any chance?
alirz23
(Alirz23)
4
can you post your code for that action
famyii
(Federicoariasmallek)
5
The image is an optional field, if the file is not empty, I have to delete the old image and save the new one.
The old image is deleted and the new is saved but I still have the error that I posted.
Thanks for your replies!
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if(isset($_POST['Noticia']))
{
$model->attributes=$_POST['Noticia'];
$model->archivo = CUploadedFile::getInstance($model, 'archivo');
if(!empty($model->archivo))
{
$imagenAnterior = $model->img;
$model->img = $model->archivo->name;
}
if($model->validate()){
if($model->save())
{
if(!empty($model->archivo))
{
unlink(Yii::getPathOfAlias('webroot').'/files/noticias/'.$model->id_noticia."_".$imagenAnterior);
$model->archivo->saveAs(Yii::getPathOfAlias('webroot').'/files/noticias/'.$model->id_noticia."_".$model->img);
}
$this->redirect(array('view','id'=>$model->id_noticia));
}
}
}
$this->render('update',array(
'model'=>$model,
));
}