www.webdeveloper.com
Results 1 to 5 of 5

Thread: Upload documents to a user's folder using PHP and MySQL database

  1. #1
    Join Date
    Oct 2012
    Posts
    3

    Upload documents to a user's folder using PHP and MySQL database

    Greetings:

    I've been working on this code for quite some time now. If you can provide any assistance I would be most gracious.

    When an admin logs into the site he/she has the option to upload pdf documents to a users folder. So I have displayed all the users within the database along with a radio button beside each username. See code below...

    PHP Code:
    include "../includes/connect_to_mysql.php";
    $table 'users';

    // sending query
    $result mysql_query("SELECT id_user, name, email FROM {$table}");
    if (!
    $result) {
        die(
    "Query to show fields from table failed");
    }

    echo 
    "<h2>Your Customers</h2><br />";
    echo 
    "<table width='90%' cellpadding='20' cellspacing='11' border='1'><tr>";

    $num=mysql_numrows($result);

    $i=0;
    while (
    $i $num) {

    $id_user=mysql_result($result,$i,"id_user");
    $name=mysql_result($result,$i,"name");
    $email=mysql_result($result,$i,"email");

    echo 
    "<form name='form3' enctype='multipart/form-data' method='post' action='upload.php'>";
            
    echo 
    "<td style='border-color:#8db2f5; padding-left:10px; padding-right:10px'>";

    echo 
    "<input type='radio' name='email' >";

    echo 
    "$name";
    echo 
    "</td>";

    echo 
    "<td style='border-color:#8db2f5; padding-left:10px; padding-right:10px'>";

    echo 
    "$email";
    echo 
    "</td>";

    echo 
    "</tr>\n";

    $i++;

    }

    echo 
    "</table>";
    mysql_free_result($result); 
    The code above displays the users successfully. Now I would like the admin to select the user he wishes to upload the documents.

    Next the admin chooses the documents and proceed to submit/upload. See code below...

    PHP Code:
    $client_upload_doc ="<br /><br /><h2>Client Upload</h2><br />
        <fieldset>
            <legend>Multiple upload</legend>
            <p>Pick up some files to upload, and press 'upload' </p>
            
                <p><input type='file' size='32' name='my_field[]' value='' /></p>
                <p><input type='file' size='32' name='my_field[]' value='' /></p>
                <p><input type='file' size='32' name='my_field[]' value='' /></p>
                <p class='button'><input type='hidden' name='action' value='multiple' />
                <input type='submit' name='Submit' value='upload customer documents' /></p>
            </form>
        </fieldset>"
    ;
    echo 
    $client_upload_doc

    The Problem:
    While the upload process works perfectly when i assign it to a folder, I would like it to upload to the selected user's folder the admin has chosen using the radio button. See code below...


    I've tried this: Yet unsuccessful
    PHP Code:
    $email $_POST['email'];
    $the_user_email_folder "$email";


    $dir_dest = (isset($_GET['dir']) ? $_GET['dir'] :"members/$the_user_email_folder");

    $dir_pics = (isset($_GET['pics']) ? $_GET['pics'] : $dir_dest); 
    When I echo $email it I get an error. How can I capture $email and use it to upload to the folder on the server?


    This works Perfectly! (info@domain.net is the name of a folder on the server)
    PHP Code:
    $the_user_email_folder "info@ah-webdesign.net";

    $dir_dest = (isset($_GET['dir']) ? $_GET['dir'] :"members/$the_user_email_folder");

    $dir_pics = (isset($_GET['pics']) ? $_GET['pics'] : $dir_dest); 


    Thanks in advance

  2. #2
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    108
    Your form does not set $email up as a POST variable that I see.

    You can simplify things a bit too, by just writing echo " And include everything you want then use "; when you're done instead of doing it for every line. For example:

    PHP Code:
    include('../includes/connect_to_mysql.php')";
    $table = "users";

    // sending query
    $query = "SELECT id_usernameemail FROM $table ORDER BY name";
    $result = mysql_query($query) or die(mysql_error());


    echo "
    <h2>Your Customers</h2><br />
    <
    table width='90%' cellpadding='20' cellspacing='11' border='1'>
    ";


    while (
    $row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        
    $id_user = stripslashes($row['id_user']);
        
    $name = stripslashes($row['name']);
        
    $email = stripslashes($row['email']);

        echo "
        
    <form name='form3' enctype='multipart/form-data' method='post' action='upload.php'>
        <
    input type='hidden' name='id_user' value='$id_user'>
        <
    input type='hidden' name='email' value='$email'>

        <
    tr>        
        <
    td style='border-color:#8db2f5; padding-left:10px; padding-right:10px'>
        <
    input type='radio' name='name' value='$name'$name
        
    </td>

        <
    td style='border-color:#8db2f5; padding-left:10px; padding-right:10px'>
        
    $email
        
    </td>
        </
    tr>
        
    ";
    }

    echo "
    </table>

    <
    br /><br />

    <
    h2>Client Upload</h2><br />
    <
    fieldset>
    <
    legend>Multiple upload</legend>
    <
    p>Pick up some files to upload, and press 'upload' </p>
            
    <
    p><input type='file' size='32' name='my_field[]' value='' /></p>
    <
    p><input type='file' size='32' name='my_field[]' value='' /></p>
    <
    p><input type='file' size='32' name='my_field[]' value='' /></p>
     <
    class='button'><input type='hidden' name='action' value='multiple' />
    <
    input type='submit' name='Submit' value='upload customer documents' /></p>
    </
    fieldset>
    </
    form>
    "; 
    The above sets the id_user and email in a hidden field which makes them a POST variable.

    Using this type of "while eliminates the i++ stuff.

    See how much neater and easier to read it is without echo-ing every single line? By indenting everything between open and close curly brackets will help you to avoid errors from accidentally forgetting a closing bracket.

    ***
    Not sure why you're setting the multiple upload section to "$client_upload_doc" only to turn around and immediately echo it out. Seems like you could just leave off the "$client_upload_doc".
    ***

    Anyway, by putting the $email and $id_user in a hidden field in the form your... $email = $_POST['email'];
    $the_user_email_folder = "$email";

    ... (and id_user if you need it) will be set.

  3. #3
    Join Date
    Oct 2012
    Posts
    3
    Thanks a million Alan! I must say, this is a very simple version as i can read and understand more clearly. After reviewing your code and testing it out a few times, my code now works as i intended.

    Thanks again for taking the time to respond.

  4. #4
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    108
    I'm glad you were able to get it working. Is there any way to mark this thread resolved if it is resolved?

  5. #5
    Join Date
    Oct 2012
    Posts
    3

    Thumbs up Issue Resolved by Alan

    Thanks Alan!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles