Closed Thread
Page 16 of 22 FirstFirst ... 61415161718 ... LastLast
Results 151 to 160 of 214

Thread: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

  1. #151
    phillw's Avatar
    phillw is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    Northwest England (UK)
    Posts
    82
    Blog Entries
    1
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    A couple of things, that may help. A nice simple db.connect.php
    Code:
    <?php
    /////////////////////////////////////////////////////////////////////////////////
        //Database connection
        /////////////////////////////////////////////////////////////////////////////////
        
    $host="localhost";  // the path to the database, localhost, in this case, but also could be your MySQL 
                                    // provider. eg mysqldb5.your.host.com
        
    $user="user_name";  // your login name, for this part set up a  user with only sufficient privalidges to get the job done - NOT root !!!! 
        
    $password="Password"// the password you set - no password is dangerous !!! 
        
    $database='your_database_name';  // The database you want to read
         
    $link mysql_connect($host,$user,$password) or die("Unable to Connect to Server");
         
    mysql_select_db ($database)or die ('Unable to Select Database '); 
    /////////////////////////////////////////////////////////////////////////////////////////////

    ?>
    Call it at the start of routines that require access to the dbase. At the end of calls, don't forget to put in
    Code:
    mysql_close($link); 
    In my case I have it in phplibrary, so i call it thus ..

    Code:
    include './phplibrary/dbconnect.php';

    //php code

    mysql_close($link); 
    A second hint - if you're testing for if magic_quotes are in existance, force a quit and tell the fool not to use them if it finds them !!

    It causes problems, will really make Mr O'Reilly very fed up when it knarls his name up and is going to be
    i) depreciated
    ii) removed in further releases of php.

    The correct route, is the use of mysql_real_escape_string. I note that you're already aware of that command.

    Regards,

    Phill.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #152
    k1net1cs is offline Newbie
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    @kasha
    I tried fixing the code...you had some syntax issues in database.php and config.php.

    config.php
    Code:
    <?php

    //Db constants
    defined('DB_SERVER') ? null define("DB_SERVER""localhost"); // Host name
    defined('DB_USER') ? null define("DB_USER""root"); // Mysql username
    defined('DB_PASS') ? null define("DB_PASS""root"); // Mysql password
    defined('DB_NAME') ? null define("DB_NAME""User"); // Database name

    ?>
    database.php
    Code:
    <?php

    require_once("config.php");

    class 
    MySQLDatabase {
      private 
    $connection;
      
      function 
    __construct() {
        
    $this->open_connection();
      }
      
      public function 
    open_connection() {
        
    $this->connection mysql_connect(DB_SERVERDB_USERDB_PASS);
        
        if (!
    $this->connection) {
          die(
    "Database connection failed: " mysql_error());
        }
        else {
          
    $db_select mysql_select_db(DB_NAME$this->connection);
          
          if (!
    db_select)
            die(
    "Database selection failed: " mysql_error());
        }
      }

      public function 
    close_connection() {
        if(isset(
    $this->connection)) {
          
    mysql_close($this->connection);
          unset(
    $this->connection);
        }
      }
      
      public function 
    query ($sql){
        
    $result mysql_query($sql$this->connection);
        
    $this->confirm_query($result);

        return 
    $result;
      }
      
      public function 
    mysql_prep$value ){
        
    $magic_quotes_active get_magic_quotes_gpc();
        
    $new_enough_php function_exists ("mysql_real_escape_string");

        if(
    $new_enough_php) {
          if(
    $magic_quotes_active )
            
    $value stripslashes($value);
          
          
    $value mysql_real_escape_string($value);
        }
        else {
          if(
    $magic_quotes_active)
            
    $value addslashes($value);
        }
        return 
    $value;
      }
      
      public function 
    confirm_query ($result) { 
        if (!
    $result)
          die(
    "Database query failed: " .mysql_error());
      }
    }

    $database = new MySQLDatabase();
    $db =& $database;

    ?>

  4. #153
    kasha is offline Newbie
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    @k1net1cs

    Thanks i appreciate the help soo much! It actually works!

    Ur officially the awesomest person in the world

  5. #154
    WESSAmnz is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    hello
    i have a big problem here and i really dont know how to solve it
    i made a form for uploading images in a file called upload.php and here is the code
    Code:
    <html>
    <
    body>

    <
    form action="upload_file.php" method="post"
    enctype="multipart/form-data">
    <
    label for="file">Filename:</label>
    <
    input type="file" name="file" id="file" />
    <
    br />
    <
    input type="submit" name="submit" value="Submit" />
    </
    form>

    </
    body>
    </
    html
    then i made another file called upload_file.php and here is the code

    Code:
    <?php
        
    if ((($_FILES["file"]["type"] == "image/gif")
            || (
    $_FILES["file"]["type"] == "image/jpeg")
            || (
    $_FILES["file"]["type"] == "image/png")
            || (
    $_FILES["file"]["type"] == "image/pjpeg"))
            && (
    $_FILES["file"]["size"] < 100000)){
              
            if (
    $_FILES["file"]["error"] > 0){
                echo 
    "Return Code: " $_FILES["file"]["error"] . "<br />";
            
            }else{
                
    $imgname $_FILES["file"]["name"];
                
    $imgtype $_FILES["file"]["type"];
                
    $imgsize $_FILES["file"]["size"];
                
    $imgdata $_FILES["file"]["tmp_name"];
                
                   require_once(
    'Connections/config.php');    
                
    $db = @mysql_select_db($database_config$config) or die;
                                  
                if (
    strlen($image) < 149000){
                    
    mysql_query ("insert into image (img_name,img_type,img_size,img_data,img_date) values ('$imgname','$imgtype','$imgsize',\"".$imgdata."\",now())");
                       
                }
            }
          }else{
             echo 
    "Invalid file";
          }
        
    ?>
    and here is the table



    the problem is, i tried many codes for displaying this images not only yours i just got one error message

    Code:
    WarningCannot modify header information headers already sent by (output started at C:xampphtdocsadminUntitled-6.php:12in C:xampphtdocsadminUntitled-6.php  on line 16 
    any ideas coz im starting to be crazy of this problem\
    thank you

    PS. im still new in php so please explain

  6. #155
    Jaan Guest

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Are you trying to include this file that shows that picture in some file? If yes then it doesn't work.. because you have set your header as your file will be an image.

  7. #156
    k1net1cs is offline Newbie
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Quote Originally Posted by WESSAmnz View Post
    the problem is, i tried many codes for displaying this images not only yours i just got one error message

    Code:
    WarningCannot modify header information headers already sent by (output started at C:xampphtdocsadminUntitled-6.php:12in C:xampphtdocsadminUntitled-6.php  on line 16 
    any ideas coz im starting to be crazy of this problem\
    thank you

    PS. im still new in php so please explain
    What we need to see is the code that you used to get images from the database, not the one for putting them.
    My guess is that you're trying to include the .php file that you used to get the image in another .php file, or you're trying to get the image straight from the .html file using some php codes.

    Try reading my post a few pages back in this thread; click here.
    If you can't understand what I'm trying to say there, then feel free to post the codes that you used to get the images.

  8. #157
    WESSAmnz is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    hello again
    at the beginning i want to thank you for your concern
    what i want to do here...
    is making a form for the end user so he will be able to upload image to the database to put to be finally appeared in a magazine

    so i made the code for uploading the image to database as u see and i succeed in it my problem now is i cant display the picture
    here is some explanation




    so how i can do that all using only 4 pages
    2 for input image
    and 1 for post
    and one for display the whole post including the image ofcourse
    its somthing like in this webpage http://zakinettkmag.000a.biz/


    as u can see i made the first step only, the image uploaded successfully and its now in database im willing to step 2 which the image display
    i have a code to get the image from database

    Code:
    <?php
            
    require_once('../Connections/config.php');    
            
    $db = @mysql_select_db($database_config$config) or die;
            
    $gotten = @mysql_query("select * from pix order by pid desc limit 1");
            if (
    $row = @mysql_fetch_assoc($gotten)) {
                
    $title $row[pid];
                
    $bytes $row[imgdata];
                echo 
    "yes";
            } else {
                echo 
    "no";
            }

    // If this is the image request, send out the image

    if ($_REQUEST[gim] == 1) {
            
    header("Content-type: image/jpg");
            print 
    $bytes;
            exit ();
            }
        
        
    ?>
    here i got the problem with the header
    and the image appear as it opened by notepad
    so please help

    k1net1cs
    i read your post and try to study it but there is something missing beside as i told you im just a beginner so i couldn't know what i will do

    Code:
    HEADER;

    // Put the recursive call to generate the images here.
    // Maybe something like this:
    ?????????????????????????????????=======>like what i dont know
    // Place db connection code here.

    $sql "SELECT `fileId` FROM `fileStore`";

    $result mysql_query($sql); 





    thank you
    Last edited by WESSAmnz; 03-15-2010 at 09:50 PM.

  9. #158
    k1net1cs is offline Newbie
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Quote Originally Posted by WESSAmnz View Post
    Code:
    HEADER;

    // Put the recursive call to generate the images here.
    // Maybe something like this:
    ?????????????????????????????????=======>like what i dont know
    // Place db connection code here.

    $sql "SELECT `fileId` FROM `fileStore`";

    $result mysql_query($sql); 
    The 'recursive call', or the 'loop', is actually the lines under the last line you quoted.
    It's this one.

    Code:
    for ($row mysql_fetch_assoc($result)) {
      echo 
    "<img src=\"getfile.php?&id=".
           
    $row["fileId"].
           
    "\" /><br /><br />\n";

    It's simply generating a simple code line in HTML, like this :

    HTML Code:
    <img src="getfile.php?&id=1" /><br /><br />
    This file, imgshow.php, just generate a simple HTML page with images.
    The only thing it gets from the database is the fileId field.

    So, if you have 3 images with fileIDs of 1, 2 & 3, then what imgshow.php will generate is this :

    HTML Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
    
    <html>
      <head>
        <title>Image Displayer</title>
      </head>
      <body>
        <img src="getfile.php?&id=1" />
        <img src="getfile.php?&id=2" />
        <img src="getfile.php?&id=3" />
      </body>
    </html>
    As you can see, the src attribute only points to a .php file, namely the getfile.php that I wrote below imgshow.php on my original post.

    The reason there's a problem with headers in your original code is basically because you try to generate two files from one .php file, and that's why the headers, well, 'clashed' with each other.
    Of course, at first you'd think that you only call the headers function just once so, why is there a clash?
    Whenever you echo something, you actually have sent a header from the very first echo or print that you call.

    That is why I separated the codes for getting the images.
    imgshow.php generates the HTML, getfile.php fetches the images from the database.

    HTH.
    Last edited by k1net1cs; 03-16-2010 at 11:15 PM.

  10. #159
    kasha is offline Newbie
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    hey guys

    im trying to get my code to require_once certain files. But i think im writing it wrong. The files are in an admin folder in my localhost.

    How would i require once the files from localhost while working on the file in Admin?

    for example :
    Code:
    require_once("../functions.php");
    require_once(
    "../database.php");
    require_once(
    "../user.php");
    require_once(
    "../session.php"); 
    All the above files are in stored in localhost, they are included in the file login.php which is in the admin folder.

    or should it be :
    Code:
    ../admin/<filename
    Im so sorry if this makes no sense!

  11. #160
    pages is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Question Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

    Hi first i do realise this is an old tutorial but i am hopeful you still look over it from time to time.

    I am working on a sort of social networking project which would be required to to display profile pictures and the likes . I have it set up so on the registration form i embedded your upload file and all the data gets sent to the correct tables

    However when i try to display the data of my user profile page i can either see the persons data combined with Raw blob data or if i edit the content type i see only a broken image link.

    Code:
    <?php
    header
    ('Content-type: image/jpg');
        require_once(
    'auth.php');
        require_once(
    'config.php');
        require_once(
    'opendb.php');
    session_start();
        function 
    clean($str) {
                
    $str = @trim($str);
                if(
    get_magic_quotes_gpc()) {
                    
    $str stripslashes($str);
            }

        
    $user $_SESSION['SESS_USERNAME'];
       if(!
    $user)
            {
            echo 
    "<br><p>Blah blah you arent logged in </p><br>";
            }

        else
            {
            
    //We need to grab the login name variable from the URL.
            
    $user_id clean($_REQUEST['user_id']);
             
    $view_profile mysql_query("SELECT * FROM members WHERE login = '$user_id'");

    $user mysql_fetch_array($view_profile);
    $member_id$user['member_id'];
    $login $user['login'];
    $dob $user['dob'];
    $query mysql_query("SELECT * FROM tbl_images WHERE id='$member_id'");
    $row mysql_fetch_array($query);
                
    $content $row['image'];
                    print (
    "member_id is : $member_id <BR>");
                  print (
    "UserName for login is : $login<BR>");
                  print (
    "Date of birth is : $dob<br>");
                echo 
    $content;





    }
    ?>
    Thats the code for my user_Profile.php which currently displays the broken image link.
    if i remove header('Content-type: image/jpg');
    I see the persons details but for the image i only see raw data .

    I have tested this with the other code you supplied to check if the image was being uploaded correctly and it did display it using
    Code:
    <?php

    require_once('config.php');
        require_once(
    'opendb.php');

     
    $storedid "11";
      
    $id = (int)$storedid;

    if(!isset(
    $id) || empty($id)){
    die(
    "Please select your image!");
    }else{

    $query mysql_query("SELECT * FROM tbl_images WHERE id='$id'");
    $row mysql_fetch_array($query);
    $content $row['image'];

    header('Content-type: image/jpg');
    echo 
    $content;

    }

    ?>
    Any tips or advice would be appreciated.
    Thanks,
    Pages.

Closed Thread
Page 16 of 22 FirstFirst ... 61415161718 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2011, 11:29 AM
  2. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 01-04-2011, 08:37 PM
  3. Beginner Storing images in XML file - Part II - Displaying image
    By Jaan in forum PHP Tutorials
    Replies: 6
    Last Post: 01-04-2011, 08:35 PM
  4. display images sequentially using PHP and MySQL
    By jhanjon in forum PHP Development
    Replies: 2
    Last Post: 10-08-2009, 12:11 PM
  5. Replies: 2
    Last Post: 07-16-2009, 12:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

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