2

I use the Following code to upload a single file .With This code I can Upload a single file to the database .I want to make multiple files uploaded by selecting multiple files in a single input type file.What Changes should i make in the code to make it upload multiple files?

<?PHP
INCLUDE ("DB_Config.php");
$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{       
    $filename = $_FILES["uploaded_file"]["name"];
    $location = "E:\\test_TrainingMaterial/";
    $file_size = $_FILES["uploaded_file"]["size"];
    $path = $location . basename( $_FILES['uploaded_file']['name']);
    if(file_exists($path))
    {
        echo "File Already Exists.<br/>";
        echo "Please Rename and Try Again";
    }
    else
    {
        if($file_size < 209715200)
        {   
            $move = move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
            $result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
            if ($result) 
            {
                do {
                    if ($temp_resource = $mysqli->use_result()) 
                    {
                        while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
                            array_push($rows, $row);
                        }
                        $temp_resource->free_result();
                    }
                } while ($mysqli->next_result());
            }
            if($move)
            {
                echo "Successfully Uploaded";
            }
            else
            {
                echo "File not Moved";
            }
        }
        else
        {
            echo "File Size Exceeded";
        }
    }
}
else
{
    echo " Invalid File Type";
}
?>

The Html That is used is

<form id = "upload_form" method="post" enctype="multipart/form-data"  >
    <input type="file" name="uploaded_file" id="uploaded_file" style="color:black" /><br/>
</form>
1
  • Usually I'd advise to use an uploader like plupload (plupload.com). There are many more of these uploaders. Commented Feb 20, 2013 at 8:35

1 Answer 1

3

Basically you need to add to input name [] brackets and attribute "multiple"

<form id = "upload_form" method="post" enctype="multipart/form-data"  >
    <input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>

Now all uploaded file will be available via

$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]

and so on

More info at http://www.php.net/manual/en/features.file-upload.multiple.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.