upload, save and edit photo using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fariba123
    New Member
    • Oct 2006
    • 17

    upload, save and edit photo using php

    hi, can i ask for a very simple example php code on how to save a photo from a form to the database or something like that? and how to get it and display it in the page? i just want a very simple example so that i can understand. i am new with php.

    thanks
    fariba
  • brid
    New Member
    • Oct 2006
    • 13

    #2
    Hi it's my very simple example (4 files named as typed in comments):

    First create a database (images) and table in it:
    --
    -- Database: `images`
    --
    -- Table structure for table `image`
    --

    CREATE TABLE `image` (
    `id` int(11) NOT NULL auto_increment,
    `type` varchar(16) NOT NULL default '',
    `stream` blob NOT NULL,
    KEY `id` (`id`)
    ) TYPE=MyISAM;

    more create files:

    [PHP]
    <!-- upload_image.ph p-->

    <form action="insert_ image.php" method="post" enctype="multip art/form-data" name="form">
    <input type="file" name="file" value="*.*" size="50">
    <input type="submit" value="Submit">
    </form>
    [/PHP]
    [PHP]
    <!-- insert_image.ph p-->
    <?

    $filename = $_FILES['file']['tmp_name'];
    if (($handle = fopen($filename , "rb"))) {
    $stream = fread($handle, filesize($filen ame));
    fclose($handle) ;
    unlink($_FILES['file']['tmp_name']);
    $type = $_FILES['file']['type'];

    $dbh = mysql_connect(" localhost", "user", "pass");
    mysql_select_db ("images", $dbh);

    $qstr = sprintf("INSERT INTO `image` VALUES ('', '%s', '%s')",
    mysql_real_esca pe_string($type ),
    mysql_real_esca pe_string($stre am));
    mysql_query($qs tr, $dbh) or die(mysql_error ());
    }
    header("Locatio n: show_image.php" );
    ?>
    [/PHP]
    [PHP]
    <!-- show_image.php-->

    <?

    $dbh = mysql_connect(" localhost", "user", "pass");
    mysql_select_db ("images", $dbh);

    $qstr = "SELECT `id` FROM `image`";
    $res = mysql_query($qs tr, $dbh) or die(mysql_error ());
    while ($row = mysql_fetch_ass oc($res)) {
    echo 'Image: <img src="image.php? id='.$row["id"].'"><br>';
    }

    ?>
    <a href="upload_im age.php">Upload more...</a>
    [/PHP]
    [PHP]
    <!-- image.php-->

    <?
    $dbh = mysql_connect(" localhost", "user", "pass");
    mysql_select_db ("images", $dbh);

    $qstr = "SELECT * FROM `image` WHERE `id`=".intval($ _REQUEST["id"]);
    $result = mysql_query($qs tr, $dbh);

    if (mysql_num_rows ($result) > 0) {
    $row = mysql_fetch_ass oc($result);
    $type = $row["type"];
    header("Content-type: ".$type);
    echo $row["stream"];
    }
    ?>

    [/PHP]

    P.S.
    Images no more than 64Kb otherwise field `stream` must be another type.
    In strings mysql_connect(" localhost", "user", "pass") change "user", "pass" for your value.

    Comment

    Working...