简体   繁体   中英

PHP allow only certain file uploads and also allow a user to PICK more than 1 file at a time to upload

I somewhat have the coding, but I am missing this part. The following PHP only allows gif, jpeg, jpg, doc, and pdf files to upload. I have a max file size of no more than 5 megs. Currently the coding is working as I wanted it to, WITH and exception. I want to user to be able to PICK lets say 5, 6, or 7 files at a time and then press the upload button 1 time. I thought what I could do was just copy the input type='file' name='file_upload'> 5 times..that does work, but when I attempt to press the upload button, it waits a few seconds, and then pops up with my error code 'An error ocurred when uploading.'

If someone can assist, I would greatly appreciate it. I am sure the coding could be much cleaner, but as you can see I am pretty new (like many) to php.

Thanks In Advance Tony

HTML CODING:

<form method='post' enctype='multipart/form-data' action='../edocs.php'>
<input type='file' name='file_upload'><br>
<input type='submit' value="Upload" ><br><input type="reset" >



<?php
if($_FILES['file_upload']['error'] > 0){
die('An error ocurred when uploading.');
}
if($_FILES['file_upload']['type']!= 'image/gif')
if($_FILES['file_upload']['type']!= 'image/jpeg')
if($_FILES['file_upload']['type']!= 'image/jpg')
if($_FILES['file_upload']['type']!= 'application/msword')
if($_FILES['file_upload']['type']!= 'application/pdf'){
die('Unsupported filetype uploaded.  You need to change your file type

you can try like this, it will work.

    <form method="post" enctype="multipart/form-data">
        <input type="file" name="my_file[]" multiple>
        <input type="submit" value="Upload">
    </form>

    <?php
        if (isset($_FILES['my_file'])) {
            $myFile = $_FILES['my_file'];
            $fileCount = count($myFile["name"]);

            for ($i = 0; $i < $fileCount; $i++) {
                ?>
                    <p>File #<?= $i+1 ?>:</p>
                    <p>
                        Name: <?= $myFile["name"][$i] ?><br>
                        Temporary file: <?= $myFile["tmp_name"][$i] ?><br>
                        Type: <?= $myFile["type"][$i] ?><br>
                        Size: <?= $myFile["size"][$i] ?><br>
                        Error: <?= $myFile["error"][$i] ?><br>
                    </p>
                <?php
            }
        }
    ?>

Or you can try http://www.uploadify.com/documentation/

First change this

multiple attribute for file and name of the file as array.

<input type='file' name='file_upload[]' multiple><br>
                                     ^   ^// this will allow the user to select multiple files.

Then for $_FILES use foreach loop

foreach ($_FILES['file']['name'] as $filename)
{
    $temp="path to upload the file";
    $tmp=$_FILES['file']['tmp_name'][$count];
    $count=$count + 1;
    $temp=$temp.basename($filename);
    move_uploaded_file($tmp,$temp);
    $temp='';
    $tmp='';
}

Refer this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM