Hi
I'm trying to upload the filepath of an image into my database and then view it. So far I have managed to upload it but I can't view it. When I click on the link to view the image I get errors saying
Quote:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: name
Filename: views/view.php
Line Number: 4
|
Quote:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: filePath
Filename: views/view.php
Line Number: 5
|
This is my test controller
Code:
<?php
class Test extends CI_Controller {
/**
* Constructor
*
*/
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->load->model('test_model');
$this->load->helper('url');
}
public function index(){
$this->load->view('test');
$uploadDir = 'C:/wamp/www/test/upload/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
//$fileSize = $_FILES['userfile']['size'];
//$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$this->load->library('config');
//include '../library/config.php';
//$this->load->library('opendb');
//include '../library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO image (name, image ) ".
"VALUES ('$fileName', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
//include '../library/closedb.php';
echo "<br>Files uploaded<br>";
}
if(isset($_GET['image_id']))
{
include '../library/config.php';
include '../library/opendb.php';
$id = $_GET['image_id'];
$query = "SELECT name, path FROM image WHERE image_id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $filePath) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
//header("Content-length: $size");
//header("Content-type: $type");
readfile($filePath);
include '../library/closedb.php';
exit;
}
}
}
?>
This is my view controller
Code:
<?php
class View extends CI_Controller {
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->load->model('view_model');
}
public function index(){
$this->load->view('view');
if(isset($_GET['image_id']))
{
$this->load->library('config');
//include '../library/config.php';
//include '../library/opendb.php';
$id = $_GET['image_id'];
$query = "SELECT name, path FROM image WHERE image_id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $filePath) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
//header("Content-length: $size");
//header("Content-type: $type");
readfile($filePath);
//include '../library/closedb.php';
exit;
}
}
}
?>
This is my test_model
Code:
<?php
class Test_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
}
?>
This is my view_model
Code:
<?php
class View_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
}
?>
This is my test view
Code:
The HTML form we use is no different with the previous one since the real changes will take place in the PHP codes.
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
<?php echo anchor('view'); ?>
</form>
this is my view view
Code:
<html>
<head></head>
<body>
<?php echo $name ?>
<img src="<?php echo $filePath ?>">
</body>
</html>