Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    19

    Unanswered: Upload files to mysql database

    Hi,

    I would like to know about a script that makes the following:

    upload a file to a specific folder and store the name of the file in a mysql database.

    If the folder doesnt exist, create it.

    If the name of the file is the same of an existing file, dont replace, instead, rename with a unique name and then store the new name in the database.


    Restrict the upload by size, dimension (in case of an image), and extension.

    I have tried with "PURE PHP UPLOAD" but what he does, it just record the name that you asign in the form, but not the original name of the file, lets say: "photo1.gif"

    Finally, my goal is to upload photos and automaticaly, show this photos in the webpage, I would like to allow users to upload photos to the web page, but I want to record all the upload details in the database (original name of the file with extension, name asigned by the user, dimension, user ID, date of upload, category, etc.),

    For example, in ebay yo have your own space, yo can upload your own images and other people will be able to see this photos, that's exactly what I want.

    Am I clear ?

    Thanks.
    Last edited by Mysticav; 05-22-04 at 21:03.

  2. #2
    Join Date
    Jan 2004
    Posts
    35

    Talking Some info

    Here are some links that I have found benificial. It isn't exactly what you want, but its some what close. Only difference here is you are actually storeing it in a DB.

    http://www.stardeveloper.com/article...d.html?tid=761

    http://www.planet-source-code.com/vb...=1306&lngWId=8

    also, hotscripts.com might have some futher suggestions on this....

  3. #3
    Join Date
    Jun 2004
    Posts
    1
    <?php

    $my_max_file_size = "102400"; # in bytes
    $image_max_width = "800"; # in pixels
    $image_max_height = "800"; # in pixels
    // Sets the path where the uploaded file should go

    if(isset($path))
    $the_path = $path;
    else
    $the_path = "../uploadimages/front_page_images";

    $registered_types = array(
    "application/x-gzip-compressed" => ".tar.gz, .tgz",
    "application/x-zip-compressed" => ".zip",
    "application/x-tar" => ".tar",
    "text/plain" => ".html, .php, .txt, .inc (etc)",
    "image/bmp" => ".bmp, .ico",
    "image/gif" => ".gif",
    "image/pjpeg" => ".jpg, .jpeg",
    "image/jpeg" => ".jpg, .jpeg",
    "application/x-shockwave-flash" => ".swf",
    "application/msword" => ".doc",
    "application/vnd.ms-excel" => ".xls",
    "application/octet-stream" => ".exe, .fla (etc)"
    ); # these are only a few examples, you can add as many as you like
    $allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg");

    function form($error=false) {
    global $PHP_SELF,$my_max_file_size;

    if ($error) print $error . "<br><br>";

    /* print "\n<form ENCTYPE=\"multipart/form-data\" action=\"" . $PHP_SELF . "\" method=\"post\">";
    print "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
    print "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
    print "\n<P>Upload a file";
    print "\n<BR>NOTE: Max file size is " . ($my_max_file_size / 1024) . "KB";
    print "\n<br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"35\"><br>";
    print "\n<input type=\"submit\" Value=\"Upload\">";
    print "\n</form>";*/

    } # END form

    if (!ereg("^4",phpversion())) {
    function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
    for ($i=0; $i < count($haystack); $i++) {
    if ($haystack[$i] == $needle) {
    return true;
    }
    }
    }
    }


    function validate_upload($the_file,$the_filetype) {

    global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$r egistered_types;
    $error="";
    //$start_error = "\n<b>Error:</b>\n<ul>";
    // echo "This is the file type".$the_file_type;
    if ($the_file == "none") { # do we even have a file?

    $error .= "\n<li>You did not upload anything!</li>";

    } else { # check if we are allowed to upload this file_type

    if (!in_array($the_filetype,$allowed_types)) {
    $error .= "The file type that you uploaded is not allowed";
    }

    if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {

    $size = GetImageSize($the_file);
    list($foo,$width,$bar,$height) = explode("\"",$size[3]);

    if ($width > $image_max_width) {
    $error .= "\n<li>Your image should be no wider than " .
    $image_max_width . " Pixels</li>";
    }

    if ($height > $image_max_height) {
    $error .= "\n<li>Your image should be no higher than " .
    $image_max_height . " Pixels</li>";
    }

    }
    if ($error) {
    $error = $error . "\n</ul>";
    return $error;
    } else {
    return false;
    }

    }
    } # END validate_upload

    function list_files() {

    global $the_path;

    $handle = dir($the_path);
    print "\n<b>Uploaded files:</b><br>";
    while ($file = $handle->read()) {
    if (($file != ".") && ($file != "..")) {
    print "\n" . $file . "<br>";
    }
    }
    print "<hr>";
    }


    function upload($the_file,$the_filename,$the_filetype) {

    global $the_path;
    //echo "$the_file ====== file name:".$the_path . "/" . $the_filename;
    $error = validate_upload($the_file,$the_filetype);
    if ($error) {
    // form($error);
    return($error);
    } else { # cool, we can continue
    //echo "the file name is--".$the_file_name;
    if (!@copy($the_file, $the_path . "/" . $the_filename)) {
    form("\n<b>Something barfed, check the path to and ".
    "the permissions for the upload directory</b>");
    } else {
    // list_files();
    // form();
    return 0;
    }
    }
    } # END upload

    /*
    print "<html>\n<head>\n<title>Upload example</title>\n</head>\n<body>";
    if(!isset($task)){$task="";}



    switch($task) {
    case 'upload':
    upload($the_file);
    break;
    default:
    form();
    }


    print "\n</body>\n</html>"; */



    ?>


    And you can use the code for inserting
    // You have to include the imageupload function where you want to write the cade.

    //thumbnail is the name of the text box in the form

    // upload is the function

    $path = "../uploadimages/front_page_images";
    include "imageupload.php";

    if((isset($thumbnail)) && ($thumbnail!=""))
    {
    $error = upload($thumbnail,$thumbnail_name,$thumbnail_type) ;
    }

    if(!$error)
    {

    $dest = $thumbnail_name;
    $query = "insert into front_page_images (f_image_name,f_image_path)";
    $query .="values ('$fimagename','$dest')";
    if($result = mysql_query($query))
    {
    // do coding here
    }
    }
    Last edited by Jyotsna; 06-10-04 at 03:34.

Posting Permissions

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