Results 1 to 3 of 3
  1. #1

    hummn

    How do i make a php script to upload images in the best way, i know how from tutorials but i want to know which way is best and why cause i want efficiancy and stuff, there is a simple way i know of that i like:
    save into database image code
    <?php
    /*get file from
    pc*/
    include("db.php");
    $FILE = $_POST[file_url];
    if($FILE == FALSE || $NAME== FALSE) {
    echo "NO FILE WAS SELECTED!";
    }
    else {
    mysql query("INSERT INTO images(img_code, img_name) VALUES('$FILE', '$NAME')", $db);
    }
    ?>
    generate image from database
    <?php
    /*read image from
    database*/
    include("db.php");
    $image=$_GET[image];
    if($image==TRUE) {
    header("Content-type: image/gif");
    $image_query = mysql_query("SELECT img_code FROM images WHERE img_name='$image'", $db);
    $image_code = mysql_fetch_array($image_query);
    echo "$image_code";
    }
    ?>

    but is that a good way to use

  2. #2
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,740
    If it works - it looks pretty good.

    I would just leave it.
    <<< Please see Forum Guidelines for signature setup. >>>

  3. #3
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    3,103
    PHP Code:
    if (    is_uploaded_file($_FILES['Filen_Name']['tmp_name']) &&     $image_details = @getImageSize($_FILES['File_Name']['tmp_name'])  &&    $image_details[2] == '2' ) {  // just for an example we tested if image is JPEG
         
    $img file_get_contents($_FILES['File_Name']['tmp_name']) ; 
         
    $query "INSERT INTO images (original_name, size , width, height, img) VALUES (
    '"
    .addslashes($_FILES['File_Name']['name'])."' , 
    '"
    .strlen($img)."' , '".$image_details[0]."' , '".$image_details[1].'"  , '".addslashes($img)."') " ;
    ..... 
    This is not very efficient as we read image 2 times, once for getImgeSize, and second time to get its contents, but more often then not real bottleneck in perfomance is not saving but reading images.

    This way we can do one read, and have all image details we might need ready to use.

Posting Permissions

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