Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6

    multiple file upload script help ?


    How do i turn this script to upload multiple files ?

    fileupload.php

    PHP Code:
    <?php
    include_once 'config.php';
    ?>

    <?php
    session_start
    ();
    require_once 
    'class.user.php';
    $user_home = new USER();

    if(!
    $user_home->is_logged_in())
    {
        
    $user_home->redirect('index.php');
    }

    ?>

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>File Uploading With PHP and MySql</title>
    <link rel="stylesheet" href="style.css" type="text/css" />

    </head>
    <body>
    <div id="header">
    <label>File Uploading With PHP and MySql</label>
    </div>

    <div class="centeralign"> <P ALIGN=CENTER>Click <a href="http://xxxx.xx/view.php">here</a> to go to view.php </P></div>
    <div id="body">
        <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" multiple="" /> 
        <button type="submit" name="btn-upload">upload</button>
        </form>
        <br /><br />
        <?php
        
    if(isset($_GET['success']))
        {
            
    ?>
            <label>File Uploaded Successfully...  <a href="view.php">click here to view file.</a></label>
            <?php
        
    }
        else if(isset(
    $_GET['fail']))
        {
            
    ?>
            <label>Problem While File Uploading !</label>
            <?php
        
    }
        else
        {
            
    ?>
            <label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</label>
            <?php
        
    }
        
    ?>
    </div>
    <div id="footer">

    </div>
    </body>
    </html>
    upload.php

    PHP Code:
     <?php
    include_once 'config.php';
    if (
    $_SERVER['REQUEST_METHOD'] == "POST")
    {    
         
        
    $file rand(1000,100000)."-".$_FILES['file']['name'];
        
    $file_loc $_FILES['file']['tmp_name'];
        
    $file_size $_FILES['file']['size'];
        
    $file_type $_FILES['file']['type'];
        
    $folder="uploads/";
        
        
    // new file size in KB
        
    $new_size $file_size/1024;  
        
    // new file size in KB
        
        // make file name in lower case
        
    $new_file_name strtolower($file);
        
    // make file name in lower case
        
        
    $final_file=str_replace(' ','-',$new_file_name);
        
        if(
    move_uploaded_file($file_loc,$folder.$final_file))
        {
            
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            
    mysqli_query($conn $sql);
            
    ?>
            <script>
            alert('successfully uploaded');
            window.location.href='fileupload.php?success';
            </script>
            <?php
        
    }
        else
        {
            
    ?>
            <script>
            alert('error while uploading file');
            window.location.href='fileupload.php?fail';
            </script>
            <?php
        
    }
    }
    ?>
    Right now the problem is it selects all the files like this .





    But only successfully uploads one file at a time .

    How can i make it upload all the file i select ?

  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    Rename the input to be "file[]". The multiple files wil be stored in $_FILES as before, except using an array: the "name" will be $_FILES["file"][...]["name"], "tmp_name" will be $_FILES["file"][...]["tmp_name"], and so on, where each ... is a normal array index (counting from zero).
    Then adjust your code so that it does the same stuff as it does now except with most of it inside a loop. For example, a foreach:
    PHP Code:
    foreach (array_keys($_FILES["file"]) as $n) {
        
    $file rand(1000,100000)."-".$_FILES["file"][$n]["name"];
        
    // ... 
    You'll have to fix your success/error message so that it can show more than one at a time.


    And you're vulnerable to SQL injection, your uploading code doesn't check for upload errors, and neither does it check for valid file types.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    so this line becomes ,

    PHP Code:
    <input type="file" name="files[]" multiple="" /> 
    and the php code becomes ,
    PHP Code:
    foreach (array_keys($_FILES["file"]) as $n) {
    $file rand(1000,100000)."-".$_FILES['file'][$n]['name'];
        
    $file_loc $_FILES['file'][$n]['tmp_name'];
        
    $file_size $_FILES['file'][$n]['size'];
        
    $file_type $_FILES['file'][$n]['type'];
        
    $folder="uploads/"
    ??
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    sorry for the late reply , i was a bit busy with work .

    I am getting these errors .

    PHP Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>File Uploading With PHP and MySql</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head>
    <body>
    <div id="header">
    <label>File Uploading With PHP and MySql</label>
    </div>
    <div id="body">
        <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple="" />
        <button type="submit" name="btn-upload">upload</button>
        </form>
        <br /><br />
        <?php
        
    if(isset($_GET['success']))
        {
            
    ?>
            <label>File Uploaded Successfully...  <a href="view.php">click here to view file.</a></label>
            <?php
        
    }
        else if(isset(
    $_GET['fail']))
        {
            
    ?>
            <label>Problem While File Uploading !</label>
            <?php
        
    }
        else
        {
            
    ?>
            <label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</label>
            <?php
        
    }
        
    ?>
    </div>
    <div id="footer">

    </div>
    </body>
    </html>
    PHP Code:
    <?php
    include_once 'dbconfig.php';
    if(isset(
    $_POST['btn-upload']))
    {    
        
     foreach (
    array_keys($_FILES["files"]) as $n) {
        
    $file=rand(1000,100000)."-".$_FILES['files']['name'][$n];
        
    $file_loc $_FILES['files']['tmp_name'][$n];
        
    $file_size $_FILES['files']['size'][$n];
        
    $file_type $_FILES['files']['type'][$n];
        
    $folder="uploads/";  
            
       


        
        
    // new file size in KB
        
    $new_size $file_size/1024;  
        
    // new file size in KB
        
        // make file name in lower case
        
    $new_file_name strtolower($file);
        
    // make file name in lower case
        
        
    $final_file=str_replace(' ','-',$new_file_name);
        
        if(
    move_uploaded_file($file_loc,$folder.$final_file))
        {
            
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            
    mysqli_query($conn $sql);
            
    ?>
            <script>
            alert('successfully uploaded');
            window.location.href='fileupload.php?success';
            </script>
            <?php
        
    }
        else
        {
            
    ?>
            <script>
            alert('error while uploading file');
            window.location.href='fileupload.php?fail';
            </script>
            <?php
        
    }
     }
    }
    ?>


  10. #6
  11. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    Ah.
    PHP Code:
    foreach (array_keys($_FILES['files']['name']) as $n) { 
    I really do hate that array structure.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    Thanks for the reply requinix .

    i am encountering two problems here ,

    one is it shows me "successfully uploaded" but nothing gets uploaded due to an error like this .




    PHP Code:
     <?php
    include_once 'dbconfig.php';
    if(isset(
    $_POST['btn-upload']))
    {    
        
     foreach (
    array_keys($_FILES["files"]) as $n) {
        
    $file=rand(1000,100000)."-".$_FILES['files']['name'][$n];
        
    $file_loc $_FILES['files']['tmp_name'][$n];
        
    $file_size $_FILES['files']['size'][$n];
        
    $file_type $_FILES['files']['type'][$n];
        
    $folder="uploads/";  
            
       


        
        
    // new file size in KB
        
    $new_size $file_size/1024;  
        
    // new file size in KB
        
        // make file name in lower case
        
    $new_file_name strtolower($file);
        
    // make file name in lower case
        
        
    $final_file=str_replace(' ','-',$new_file_name);
        
        if(
    move_uploaded_file($file_loc,$folder.$final_file))
        {
            
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            
    mysqli_query($conn $sql);
            
    ?>
            <script>
            alert('successfully uploaded');
            window.location.href='fileupload.php?success';
            </script>
            <?php
        
    }
        else
        {
            
    ?>
            <script>
            alert('error while uploading file');
            window.location.href='fileupload.php?fail';
            </script>
            <?php
        
    }
     }
    }
    ?>



    please help
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    Thanks a lot requinix .

    Its working in my localhost now .

    i changed the connection config file and it worked
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    i have few more problems . please help .

    this code is working in my localhost

    PHP Code:
     <?php
    include_once 'config.php';
    if(isset(
    $_POST['btn-upload']))
    {    
        
    foreach (
    array_keys($_FILES['files']['name']) as $n) { 
        
    $file=rand(1000,100000)."-".$_FILES['files']['name'][$n];
        
    $file_loc $_FILES['files']['tmp_name'][$n];
        
    $file_size $_FILES['files']['size'][$n];
        
    $file_type $_FILES['files']['type'][$n];
        
    $folder="uploads/";  
            
       


        
        
    // new file size in KB
        
    $new_size $file_size/1024;  
        
    // new file size in KB
        
        // make file name in lower case
        
    $new_file_name strtolower($file);
        
    // make file name in lower case
        
        
    $final_file=str_replace(' ','-',$new_file_name);
        
        if(
    move_uploaded_file($file_loc,$folder.$final_file))
        {
            
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            
    mysqli_query($conn $sql);
            
    ?>
            <script>
            alert('successfully uploaded');
            window.location.href='fileupload.php?success';
            </script>
            <?php
        
    }
        else
        {
            
    ?>
            <script>
            alert('error while uploading file');
            window.location.href='fileupload.php?fail';
            </script>
            <?php
        
    }
     }
    }
    ?>
    but its not working in my paid hosting server .

  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2016
    Posts
    2
    Rep Power
    0
    Do you have 'uploads' folder in the server? Also you have to make sure it's writable.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    Thanks for the reply ,

    it somehow worked after i changed some file permission issues

    but the thing is sometimes this same script doesn't upload anything at all , i don't know why its happening like that

    files too large maybe ?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    90
    Rep Power
    72
    Before processing an uploaded file, check $_FILES['files']['error'] value
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    I am not sure how to do that . i put together this website from two tutorials i found online , and i am learning from these examples too .










    thanks for the suggestions , i will check those methods .

    i am also planning to take PHP a bit more seriously . i want to learn these things from scratch , not sure where to start ?

    Last edited by digimon; November 22nd, 2016 at 11:13 AM.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    90
    Rep Power
    72
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    Thanks

    any other place or books ?

    Comments on this post

    • DSmabismad disagrees : You were given the best place and best book. Somehow you must find a way of using the information you are being given.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo