• Add Pictures With PHP

      0 comments

    I found code that you can use to be able to upload pictures to a MySQL server with PHP. This is useful code to use if you are making a Contact Management System or a back end system for yourself or your client so that they can upload photos to a database which will then be echoed back to your server. I put this together using phpMyAdmin, MySQL and Dreamweaver CS4.

    First thing you need to do is to go in to your phpMyAdmin program. Click the tab SQL and then cut and paste in this text to create the proper database for the php code below:

    CREATE TABLE IF NOT EXISTS `files` (
    `fid` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘unique id’,
    `name` varchar(30) NOT NULL COMMENT ‘file name’,
    `type` varchar(30) NOT NULL COMMENT ‘MIME type’,
    `size` int(11) NOT NULL COMMENT ‘file size’,
    `content` mediumblob NOT NULL COMMENT ‘actual file’,
    `description` varchar(100) NOT NULL,
    PRIMARY KEY (`fid`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT=’Uploaded files’ AUTO_INCREMENT=7 ;

    Open an html page and save it as “form.html”. Then place in this code between the body tags:

    <form action=”upload.php” method=”post” enctype=”multipart/form-data” name=”uploadform”>
    <input type=”hidden” name=”MAX_FILE_SIZE” value=”350000″>
    <input name=”picture” type=”file” id=”picture” size=”50″>
    <input name=”upload” type=”submit” id=”upload” value=”Upload Picture!”>
    </form>

    Now open a new php page and save it as upload.php making sure you are saving it in the same folder as the form.html.

    <?php require_once(‘your connection‘); ?>

    <?php

    // if something was posted, start the process…
    if(isset($_POST['upload']))
    {

    // define the posted file into variables
    $name = $_FILES['picture']['name'];
    $tmp_name = $_FILES['picture']['tmp_name'];
    $type = $_FILES['picture']['type'];
    $size = $_FILES['picture']['size'];

    // get the width & height of the file (we don’t need the other stuff)
    list($width, $height, $typeb, $attr) = getimagesize($tmp_name);

    // if width is over 600 px or height is over 500 px, kill it
    if($width>600 || $height>500)
    {
    echo $name . “‘s dimensions exceed the 600×500 pixel limit.”;
    echo  “<a href=\”form.php\”>Click here to try again.</a>”;
    die();
    }

    // if the mime type is anything other than what we specify below, kill it
    if(!(
    $type==’image/jpeg’ ||
    $type==’image/png’ ||
    $type==’image/gif’
    )) {
    echo $type .  ” is not an acceptable format.”;
    echo  “<a href=\”form.php\”>Click here to try again.</a>”  ;
    die();
    }

    // if the file size is larger than 350 KB, kill it
    if($size>’350000′) {
    echo $name . ” is over 350KB. Please make it smaller.”;
    echo “<a href=\”form.php\”>Click here to try again. </a>”  ;

    die();
    }
    // if your server has magic quotes turned off, add slashes manually
    if(!get_magic_quotes_gpc()){
    $name = addslashes($name);
    }

    // open up the file and extract the data/content from it
    $extract = fopen($tmp_name, ‘r’);
    $content = fread($extract, $size);
    $content = addslashes($content);
    fclose($extract);  ?>

    <?php  // connect to the database
    include(‘your connection‘); ?>
    <?php
    // the query that will add this to the database
    $addfile = “INSERT INTO files (name, size, type, content, description ) “.
    “VALUES (‘$name’, ‘$size’, ‘$type’, ‘$content’, ‘$description’)”;

    mysql_query($addfile) or die(mysql_error());

    // get the last inserted ID if we’re going to display this image next
    $inserted_fid = mysql_insert_id();

    mysql_close();
    // display the image
    ?>
    <div align=”center”>
    <strong><? echo $name; ?><br>
    </strong><img name=”<? echo $name; ?>” src=”getpicture.php?fid=<? echo $inserted_fid; ?>” alt=”Unable to view image #<? echo $inserted_fid; ?>”>
    <br>
    <a href=”upload.php”>upload more images</a>
    </div>
    <p>
    <?
    // we still have to close the original IF statement. If there was nothing posted, kill the page.
    }else{die(“No uploaded file present”);
    }
    ?>

    Now open a new php file and save it as getpicture.php and drop in this code:

    <?php

    if(isset($_GET['fid']))
    {
    // connect to the database

    include "your connection";

    // query the server for the picture
    $fid = $_GET['fid'];
    $query = “SELECT * FROM files WHERE fid = ’$fid’”;
    $result = mysql_query($query) or die(mysql_error());

    // define results into variables
    $name=mysql_result($result,0,”name”);
    $size=mysql_result($result,0,”size”);
    $type=mysql_result($result,0,”type”);
    $content=mysql_result($result,0,”content”);

    // give our picture the proper headers…otherwise our page will be confused
    header(“Content-Disposition: attachment; filename=$name”);
    header(“Content-length: $size”);
    header(“Content-type: $type”);
    echo $content;

    mysql_close();
    }else{
    die(“No file ID given…”);
    }
    ?>

    Now that should do it for you.  From there you can set this up to work in several different formats but now you have the ability to upload photos and then have them show. Any questions please let me know.

    Write a comment




SEO Powered by Platinum SEO from Techblissonline