Of course you should use the file infos from the database cause your first solution with absolute paths D:\… will only work on your local dev machine but not on a hosting environment with a different OS and/or directory structure.
I prefer to save the path and the filename in seperate db fields. It’s then easier to display only the filename.
Furthermore, I would suggest you to use / instead of \ directory seperators.
See the following page with further informations about that.
To be sure the image display logic work replace the $_GET[‘id’] with a hardcoded primary key which is available in your database.
$model=$this->loadmodel(1); // replace 1 with an existing row in your db
Then try if you see the image.
So, the error says undefined index:id which for me is a indication for that you don’t pass correctly the $model to the view (e.g _form.php).
The fact that the $_GET output is empty fortified my suspicion.
Please post the complete action’s that are displaying the views which are not working. Also post the renderPartial part of the view which is loading the _form.php. Maybe you pass the $model to the view which call renderPartial(’_form’) but don’t pass the $model also to the _form.php view so there it is not available which would explain the error message.
And please use code tags ([code ] [ /code] without spaces) to post code cause it is much more readable, thanks.
Also try to learn to debug errors better by yourself (do print_r’s for different var’s and look at the values; replace variables with hardcoded values to see if it works in principle; try to debug the code using breakpoints with your IDE e.g netbeans,…). I bet it is a really simple bug in your code which you normally should find by yourself at the latest after our hints here in the forum.
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.
my controller code:
<?php
class PicController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','displaySavedImage'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Pic;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Pic']))
{
$model->attributes=$_POST['Pic'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
$this->redirect(array('view','id'=>$model->id)
);
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Pic']))
{
$model->attributes=$_POST['Pic'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'image'))
{
$this->file_name=$file->name;
$this->file_type=$file->type;
$this->file_size=$file->size;
$this->file_content=file_get_contents($file->tempName);
}
return parent::beforeSave();
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Pic');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Pic('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Pic']))
$model->attributes=$_GET['Pic'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=Pic::model()->findByPk((int)$id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
public function actionDisplaySavedImage()
{
$model=$this->loadModel(1);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.$model->file_size);
header('Content-Type: '.$model->file_type);
header('Content-Disposition: attachment; filename='.$model->file_name);
echo $model->file_content;
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='pic-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}