SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    echoing a stored variable

    hey all.i m trying to echo back a picture i uploaded with a script but i m failing to do that. am i missing something or doing something wrong here is the code
    Code PHP:
    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
     
        if (file_exists("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    All those who wander aren't lost.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With file_exists and move_uploaded_file, the path needs to be the full path to the file / directory.

    PHP Code:
        if (file_exists("/put/your/full/path/to/upload/" $_FILES["file"]["name"]))
          {
          echo 
    $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          
    move_uploaded_file($_FILES["file"]["tmp_name"],
          
    "/put/your/full/path/to/upload/" $_FILES["file"]["name"]);
          
    //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          


  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    its showing the path but i wanna echo the image
    All those who wander aren't lost.

  4. #4
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question

    hey folks i m echoing a stored image in database. which when tried showed me boxes and stuff not the image. here is my code
    Code PHP:
    <?php /*?>Database conection<?php */?>
    <?php
    $con = mysql_connect(hidden);
    if(!$con){
    echo "cannot connect to the database" .mysql_error();
    }
    $grab = mysql_select_db("foodpak");
    if(!$grab){
    echo "cannot select the database" .mysql_error();
    }
    ?>
    <?php
    $result = mysql_query("SELECT * FROM description");
    while ($row= mysql_fetch_array($result))
    {
    echo $row['image'];
    }
    ?>
    shld i be doing something else coz i created the table using phpmyadmin n set the value to mediumblob. wht am i missing?
    All those who wander aren't lost.

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header('Content-Type: image/gif'); 
    change that depending on image type

  6. #6
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    where do i put it?
    All those who wander aren't lost.

  7. #7
    SitePoint Member
    Join Date
    May 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    //
    //Database Connection
    //
    $con mysql_connect(hidden);
    if(!
    $con){
    echo 
    "cannot connect to the database" .mysql_error();
    }
    $grab mysql_select_db("foodpak");
    if(!
    $grab){
    echo 
    "cannot select the database" .mysql_error();
    }

    $result mysql_query("SELECT * FROM description");
    //since you can only output one image from blob at a time
    if($row mysql_fetch_array($result))
    {
    header('Content-type: '.$row['image_type']);
    echo 
    $row['image'];
    }
    ?>
    something like that

  8. #8
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hey folks i m trying to display my images from DB on webpage. i m successful for getting only one image to show up where as second image is in my db but it isnt showing here is the code i m using to echo stored images in database
    Code PHP:
    <?php
    header('Content-Type: image/gif'); 
     
    $con = mysql_connect(suprresed);
    if(!$con){
    echo "Error connecting" .mysql_error();
    }
    $grab = mysql_select_db("foodpak");
    if(!$grab){
    echo "Error selecting database"  .mysql_error();
    }
    $result = mysql_query("SELECT * FROM Listing");
    while ($row=mysql_fetch_array($result))
    {
    echo $row[image]."<br/>";
    }
    ?>
    All those who wander aren't lost.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Each file can only output one image. You would need to run the query separately from reading the image contents.

    PHP Code:
    $con mysql_connect(suprresed);
    if(!
    $con){
    echo 
    "Error connecting" .mysql_error();
    }
    $grab mysql_select_db("foodpak");
    if(!
    $grab){
    echo 
    "Error selecting database"  .mysql_error();
    }
    $result mysql_query("SELECT * FROM Listing");
    while (
    $row=mysql_fetch_array($result))
    {
    echo 
    '<img src="/fetch_image.php?name='$row[image].'" /><br/>';

    fetch_image.php
    PHP Code:
    $errorImage 'image_not_exists.gif';
    $dir 'path/to/image/directory';

    $im = isset($_GET['name'])?$_GET['name']:$errorImage;
    $path file_exists($dir.'/'.$file)?$dir.'/'.$name:$dir.'/'.$errorImage;

    header('Content-Type: image/gif'); 

    // read and output image file 

  10. #10
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    thnx, do u any script or can find anything that can make a form in which i haver few text field and option to upload image and it can be shown on a webpage? i m creating a project and this image thing is driving me nuts coz as if i submit the form i created into mysql. the image field which is blob.remains size 0.maybe i m sending it directly into mysql instead of storing it in a folder coz i donno how to do much of image store and retrieve in mysql and i have deadline to meet. or if any1 here can make me a small simple form which upload text fields and a image into DB and retrieve it back on a given page
    All those who wander aren't lost.

  11. #11
    SitePoint Member
    Join Date
    May 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My 2 cents...

    You don't want to store images in a db, you want them stored as actual files. You should use the db to store the location of the images but not the actual images. Storing them in a db complicates everything and makes it really slow.

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Yeah, you shouldn't store images inside the db. If you need them to be protected then store the actual files outside the site root and only grant privileges based on session data perhaps. Then again I'm not certain of the requirements of your system so that would only be a speculation based on what has been said here.

  13. #13
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i made a form and in that form r some text fields and one image uploader. the inserted data in the form r sent to database but when i see in database the image field which is longblob is 0kb.
    All those who wander aren't lost.


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
  •