Results 1 to 3 of 3

Thread: uploading images with php and mysql

  1. #1
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default uploading images with php and mysql

    The code works perfect as far as uploading the images.. however, im having issues when im looking at my mysql database.. If i choose to upload 3 images, the 3 images get uploaded to my directory... BUT in my mysql database, i have 3 new entries.. 1 is completely blank, the other 2 have the correct information in them...

    my code is as identical to yours for the most part.


    <?php

    //connect to mysql. You MUST edit these! (except localhost)
    mysql_connect("p41mysql153.secureserver.net", "DTPIMAGES", "PASSWORD") or die(mysql_error());
    mysql_select_db("DTPIMAGES") or die(mysql_error());
    //upload directory.
    //change to fit your need eg. files, upload .... etc.
    $upload_dir = "images/";
    //number of files to upload.
    $num_files = 3;
    //the file size in bytes.
    $size_bytes =2048000; //51200 bytes = 50KB.
    //Extensions you want files uploaded limited to.
    $limitedext = array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");


    //check if the directory exists or not.
    if (!is_dir("$upload_dir")) {
    die ("Error: The directory <b>($upload_dir)</b> doesn't exist");
    }
    //check if the directory is writable.
    if (!is_writeable("$upload_dir")){
    die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
    }


    //if the form has been submitted, then do the upload process
    //infact, if you clicked on (Upload Now!) button.
    if (isset($_POST['upload_form'])){

    echo "<h3>Upload results:</h3>";

    //do a loop for uploading files based on ($num_files) number of files.
    for ($i = 1; $i <= $num_files; $i++) {

    //define variables to hold the values.
    mysql_query("INSERT INTO users (description, file_title, email, name) VALUES('$description', '$file_title', '$email', '$name') ")
    or die(mysql_error());
    $new_file = $_FILES['file'.$i];
    $description = $_POST['description'];
    $file_title = $_POST['file_title'];
    $email = $_POST['email'];
    $name = $_POST['name'];
    $file_name = $new_file['name'];
    //to remove spaces from file name we have to replace it with "_".
    $file_name = str_replace(' ', '_', $file_name);
    $file_tmp = $new_file['tmp_name'];
    $file_size = $new_file['size'];

    #-----------------------------------------------------------#
    # this code will check if the files was selected or not. #
    #-----------------------------------------------------------#

    if (!is_uploaded_file($file_tmp)) {
    //print error message and file number.
    echo "File $i: Not selected.<br>";
    }else{
    #-----------------------------------------------------------#
    # this code will check file extension #
    #-----------------------------------------------------------#

    $ext = strrchr($file_name,'.');
    if (!in_array(strtolower($ext),$limitedext)) {
    echo "File $i: ($file_name) Wrong file extension. <br>";
    }else{
    #-----------------------------------------------------------#
    # this code will check file size is correct #
    #-----------------------------------------------------------#

    if ($file_size > $size_bytes){
    echo "File $i: ($file_name) Faild to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
    }else{
    #-----------------------------------------------------------#
    # this code check if file is Already EXISTS. #
    #-----------------------------------------------------------#

    if(file_exists($upload_dir.$file_name)){
    echo "File $i: ($file_name) already exists.<br>";
    }else{
    #-----------------------------------------------------------#
    # this function will upload the files. cool #
    #-----------------------------------------------------------#
    if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
    echo "File $i: ($file_name) Uploaded.<br>";
    }else{
    echo "File $i: Faild to upload.<br>";
    }#end of (move_uploaded_file).

    }#end of (file_exists).

    }#end of (file_size).

    }#end of (limitedext).

    }#end of (!is_uploaded_file).

    }#end of (for loop).
    # print back button.
    echo "<a href=\"$_SERVER[PHP_SELF]\">back</a>";
    ////////////////////////////////////////////////////////////////////////////////
    //else if the form didn't submitted then show it.
    }else{
    echo " <h3>Select files to upload!.</h3>
    Max file size = ". $size_bytes / 1024 ." KB";
    echo " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\" enctype=\"multipart/form-data\">";
    // show the file input field based on($num_files).
    for ($i = 1; $i <= $num_files; $i++) {
    echo "File $i: <input type=\"file\" name=\"file". $i ."\"><br>";
    }
    echo " <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\">

    Name: <input type=\"text\" name=\"name\"><br />
    File Title: <input type=\"text\" name=\"file_title\"><br />
    Email: <input type=\"text\" name=\"email\"><br />
    File Description: <textarea name=\"description\"></textarea><br />
    <input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\">
    </form>";
    }
    ?>



    this is what my mysql database looks like afterwards:

    "1" "file description" "File Title" "dustypatton@gmail.com" "Dustin"
    "2" "file description" "File Title" "dustypatton@gmail.com" "Dustin"
    "3"
    without the quotes of course

    does anyone have any idea what my problem is on here? ive pulled my hair out trying to figure it out!

    Dustin

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Just glancing at the code, you should rearrange the following:

    Code:
    //define variables to hold the values.
    mysql_query("INSERT INTO users (description, file_title, email, name) VALUES('$description', '$file_title', '$email', '$name') ")
    or die(mysql_error());
    $new_file = $_FILES['file'.$i];
    $description = $_POST['description'];
    $file_title = $_POST['file_title'];
    $email = $_POST['email'];
    $name = $_POST['name'];
    $file_name = $new_file['name'];
    //to remove spaces from file name we have to replace it with "_".
    $file_name = str_replace(' ', '_', $file_name);
    $file_tmp = $new_file['tmp_name'];
    $file_size = $new_file['size'];
    like so:

    Code:
    //define variables to hold the values.
    
    $new_file = $_FILES['file'.$i];
    $description = $_POST['description'];
    $file_title = $_POST['file_title'];
    $email = $_POST['email'];
    $name = $_POST['name'];
    $file_name = $new_file['name'];
    //to remove spaces from file name we have to replace it with "_".
    $file_name = str_replace(' ', '_', $file_name);
    $file_tmp = $new_file['tmp_name'];
    $file_size = $new_file['size']; 
    
    mysql_query("INSERT INTO users (description, file_title, email, name) VALUES('$description', '$file_title', '$email', '$name') ")
    or die(mysql_error());
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    dustifer (07-13-2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    that fixed it! you sir, are the man.

Bookmarks

Posting Permissions

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