diplay stored images

Discussion in 'PHP' started by tldmic, Feb 21, 2010.

  1. #1
    hi all,

    I am having trouble with displaying the imaged stored.The code below can store image in the direcctory c:/images/ and the name is stored in mysql database.Now the problems comes when I want to display it. There are several methods I have tried with no success,

    any ideas?,

    please help
    thanks
    tldmic

    <html>
    <head>
    <title>uploader</title>
    </head>
    <body>
    <h4>file upload</h4>

    <?php

    //This is the directory where images will be saved
    $target = "c:/images/";
    $target = $target . basename( $_FILES['uploadedfile']['name']);


    //This gets all the other information from the form
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $pic=($_FILES['uploadedfile']['name']);

    // Connects to your Database
    $server = "localhost"; // this is the server address and port
    $username = "root"; // change this to your mysql username
    $password = "mike"; // change this to your mysql password

    $link = mysql_connect ($server, $username, $password)
    or die ("could not connect");

    mysql_select_db("upload") or die(mysql_error()) ;

    //Writes the information to the database
    mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ;



    if (
    ( ($_FILES["uploadedfile"]["type"] == "image/pjpeg")
    || ($_FILES["uploadedfile"]["type"] == "image/jpeg")
    || ($_FILES["uploadedfile"]["type"] == "image/gif"))
    && ($_FILES["uploadedfile"]["size"] < 200000)) //size in bytes

    {

    if ($_FILES["uploadedfile"]["error"] > 0)
    { echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br />";}

    else{

    if (file_exists("C:/images/" . $_FILES["uploadedfile"]["name"]))
    {
    echo $_FILES["uploadedfile"]["name"] . " already exists. ";

    }
    else
    {
    echo "Upload: " . $_FILES["uploadedfile"]["name"] . "<br />";
    echo "Type: " . $_FILES["uploadedfile"]["type"] . "<br />";
    echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["uploadedfile"]["tmp_name"] . "<br />";
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
    "c:/images/" . $_FILES["uploadedfile"]["name"]);
    echo "Stored in: " . "C:/images/" . $_FILES["uploadedfile"]["name"];
    }
    }
    }
    else
    {
    echo "Invalid file";
    }



    ?>
    </body>

    <?
    mysql_close ($link);
    ?>

    </html>
     
    tldmic, Feb 21, 2010 IP
  2. tldmic

    tldmic Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hi guys
    after the statement

    echo "Stored in: " . "C:/images/" . $_FILES["uploadedfile"]["name"];

    i tried to put

    echo "<img src= . 'C:/images/' . $_FILES['uploadedfile']['name']>";


    which gives error of "webpage cannot be displayed"


    please help me,

    thanks
     
    tldmic, Feb 21, 2010 IP
  3. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    1 - You should be doing all the file checks and moving the file before you even think of writing the database entry. I mean you are inserting the data and then checking the upload.

    2 - Are the files actually readable by the public? Might want to chmod the file after moving it.
     
    lovingwings36, Feb 21, 2010 IP
  4. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you just want to display all images:

    $g = glob("*.gif");
    Code (markup):
    And treat $g as an array.
     
    lovingwings36, Feb 21, 2010 IP