Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-16-2012, 09:24 PM   PM User | #1
cerberus478
New to the CF scene

 
Join Date: Aug 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cerberus478 is an unknown quantity at this point
undefined variable

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>
cerberus478 is offline   Reply With Quote
Old 08-17-2012, 03:26 AM   PM User | #2
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,153
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
Are you using $_SESSION at all? When you're using variables across multiple pages you need to use session to pass the data from page to page.
PHP Code:
to define it
$_SESSION
['name'] = $name;
to pass it to the other page
$name 
$_SESSION['name']; 
DrDOS is offline   Reply With Quote
Old 08-17-2012, 12:23 PM   PM User | #3
cerberus478
New to the CF scene

 
Join Date: Aug 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cerberus478 is an unknown quantity at this point
I did that and I still had the same problem. Unless I put it in the wrong place.
Because I put the session at the end.
cerberus478 is offline   Reply With Quote
Old 08-17-2012, 03:39 PM   PM User | #4
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,153
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
When you're using session you have to put the function

session_start();

at the top of every page, so that the page will be included in the session.
DrDOS is offline   Reply With Quote
Old 08-22-2012, 10:11 AM   PM User | #5
cerberus478
New to the CF scene

 
Join Date: Aug 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cerberus478 is an unknown quantity at this point
Sorry for replying so late.

I put session_start(); on every page and I get the error

Quote:
A PHP Error was encountered

Severity: Notice

Message: A session had already been started - ignoring session_start()

Filename: views/view.php

Line Number: 1
cerberus478 is offline   Reply With Quote
Old 08-22-2012, 04:37 PM   PM User | #6
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,153
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
Quote:
Originally Posted by cerberus478 View Post
Sorry for replying so late.

I put session_start(); on every page and I get the error
OK, then change it to
@session_start();
The @ tells it to ignore the error

Otherwise does it work all right.
DrDOS is offline   Reply With Quote
Old 08-22-2012, 04:46 PM   PM User | #7
cerberus478
New to the CF scene

 
Join Date: Aug 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cerberus478 is an unknown quantity at this point
It's still the same
cerberus478 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:18 PM.


Advertisement
Log in to turn off these ads.