...

View Full Version : undefined variable



cerberus478
08-16-2012, 09:24 PM
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



A PHP Error was encountered

Severity: Notice

Message: Undefined variable: name

Filename: views/view.php

Line Number: 4



A PHP Error was encountered

Severity: Notice

Message: Undefined variable: filePath

Filename: views/view.php

Line Number: 5


This is my test controller


<?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


<?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


<?php
class Test_model extends CI_Model {

public function __construct()
{
$this->load->database();
}
}
?>


This is my view_model


<?php
class View_model extends CI_Model {

public function __construct()
{
$this->load->database();
}
}
?>


This is my test view




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


<html>
<head></head>
<body>
<?php echo $name ?>
<img src="<?php echo $filePath ?>">
</body>
</html>

DrDOS
08-17-2012, 03:26 AM
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.


to define it
$_SESSION['name'] = $name;
to pass it to the other page
$name = $_SESSION['name'];

cerberus478
08-17-2012, 12:23 PM
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.

DrDOS
08-17-2012, 03:39 PM
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.

cerberus478
08-22-2012, 10:11 AM
Sorry for replying so late.

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



A PHP Error was encountered

Severity: Notice

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

Filename: views/view.php

Line Number: 1

DrDOS
08-22-2012, 04:37 PM
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.

cerberus478
08-22-2012, 04:46 PM
It's still the same



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum