Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: PHP Upload Form

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default PHP Upload Form

    Please read all before answering... if you do not understand make sure you ask... thanks!
    ok heres what I've got... I got a peice of code I got from PHPFreaks.com... its a multiple file uploader form... which works perfectly! The thing is I'm having trouble understanding to add on different fields like textarea and text box to so that I don't have a bigilion forms on my page...
    Here is the code for the upload form...
    PHP Code:
    <?php
    //upload directory.
    //change to fit your need eg. files, upload .... etc.
    $upload_dir "submitions/";
    //number of files to upload.
    $num_files 1;
    //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.
               
    $new_file $_FILES['file'.$i];
               
    $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\">
               <input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\">
               </form>"
    ;
    }
    ?>
    It works...
    All I am concentrating is how to get this to work...
    I wanna add 5 inputs on to it... not file inputs but different inputs:
    i wanna add:
    3 Textboxes (Named: Name, Email, and File Title)
    1 Textarea (Named: Description)
    and 1 checkbox (Named: TermsofService)
    I also want the form to validate the extra things i'm adding on when the submit button is pressed... Don't write something to check the file uploader part because it already validates to make sure that the file being uploaded is the right one... I don't have any idea how to do this... so please be very detailed as where peices go or just give the whole thing with everything I asked for... *giving everything together would really help me out!*... I know that you can write data on to a file using filewrite, I just don't know the code that will do it when you submit the form... Thanks!
    Last edited by Rockonmetal; 09-01-2007 at 08:44 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Ok, well you need a database to store all of you're info. I wrote it with this intent of you using mysql, if you're using another database, post a reply if you have some other type like postgres, dbsc, ms ODBC, etc.,

    PHP Code:

    <?php 

    //connect to mysql. You MUST edit these! (except localhost)
    mysql_connect("localhost""MYSQLUSER""MYSQL_PASS") or die(mysql_error());
    mysql_select_db("MYSQL_DB") or die(mysql_error());
    //upload directory. 
    //change to fit your need eg. files, upload .... etc. 
    $upload_dir "submitions/"
    //number of files to upload. 
    $num_files 1
    //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 files (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\"> 
               <input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\"> 
    Name: <input type=\"text\" name=\"name\"><br />
    File Title: <input type=\"text\" name=\"file_title\"><br />
    Email: <input type=\"text\" name=\"email\"><br />
    <br /><br />
    File Description: <textarea name=\"description\"></textarea><br />
    <input type=\"checkbox\">I agree to the 

    //here, you edit the url to your TOS page
    <a href=\"YOURURI\">TOS</a><br />
               </form>"


    ?>
    And then, run this query in your database administration system (usually phpMyAdmin)

    PHP Code:
    CREATE TABLE `users` ( 
      `
    idint(11NOT NULL auto_increment
      `
    descriptionvarchar(255collate latin1_general_ci NOT NULL
      `
    file_titlevarchar(255collate latin1_general_ci NOT NULL
      `
    emailvarchar(255collate latin1_general_ci NOT NULL
      `
    namevarchar(255collate latin1_general_ci NOT NULL
      
    PRIMARY KEY  (`id`) 
    ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=
    Good luck!

  3. #3
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    k, unfortunitly i don't got MySQL installed with Wamp5 *i know i don't know how to install it*

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I'll write a little script to give you an idea - you want this to be added to a text file somehow? Can I have some more info about that?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Ok, i know theres a code right here which adds it to a file. heres the code off of php.net
    Code:
    <?php
    $filename = 'test.txt';
    $somecontent = "$input1 all data from post into here...";
    
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    
        // In our example we're opening $filename in append mode.
        // The file pointer is at the bottom of the file hence
        // that's where $somecontent will go when we fwrite() it.
        if (!$handle = fopen($filename, 'a')) {
             echo "Cannot open file ($filename)";
             exit;
        }
    
        // Write $somecontent to our opened file.
        if (fwrite($handle, $somecontent) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
        }
    
        echo "Success, wrote data to file ($filename)";
    
        fclose($handle);
    
    } else {
        echo "The file $filename is not writable";
    }
    ?>
    I just don't know how to do it when the user presses upload

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    call the function that you have that in when the user presses upload

  7. #7
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    I know how to make a function, but not how to call it when the user presses upload... thats why I'm asking...

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    You would just not declare $subcontent within the function, and do it this way:

    PHP Code:
    function writeIt($somecontent){ 
         
    $filename 'test.txt';

        if (
    is_writable($filename)) {

            if (!
    $handle fopen($filename'a')) {
                 echo 
    "Cannot open file ($filename)";
                 exit;
            }

            if (
    fwrite($handle$somecontent) === FALSE) {
                echo 
    "Cannot write to file ($filename)";
                exit;
            }

            echo 
    "Success, wrote data to file ($filename)";

            
    fclose($handle);

        } else {
            echo 
    "The file $filename is not writable";
        }
     } 
    Then, you do the following when you want to use it:

    PHP Code:
    writeIt('Content to be written here...'); 
    I would put the content together beforehand, though, like this:

    PHP Code:
    $content 'Content to be wirrten here...just to be more organized...';
    writeIt($content); 
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  9. #9
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Ok i don't see anything that would says on submit or something like that...

  10. #10
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by Rockonmetal View Post
    Ok i don't see anything that would says on submit or something like that...
    you can do it a couple ways... but I think it would be best to call it after you have done the sanitization and cleaning...

    PHP Code:
    function _____ {
    // WHATEVER YOU DO TO CLEAN / PRE-PROCESS DATA

       
    if( !$errors ) {
          return 
    writeIt($somecontent);
      }
       else {
          
    // WHATEVER YOU WANT TO ERROR AS
      
    }

    the $errors is whatever you are storing your errors as... if there arent any pre-write errors it will go ahead and write the content, if there are errors then you insert your error handling code

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
  •