Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

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

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    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']; 

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    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.

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    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.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's still the same


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •