Viewed   118 times

I have experience doing this with single file uploads using <input type="file">. However, I am having trouble doing uploading more than one at a time.

For example, I'd like to be able to select a series of images, then upload them to the server, all at once.

It would be great to use a single file input control, if possible.

Does anyone know how to accomplish this? Thanks!

 Answers

3

This is possible in HTML5. Example (PHP 5.4):

<!doctype html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <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
                }
            }
        ?>
    </body>
</html>

Here's what it looks like in Chrome after selecting 2 items in the file dialog:

And here's what it looks like after clicking the "Upload" button.

This is just a sketch of a fully working answer. See PHP Manual: Handling file uploads for more information on proper, secure handling of file uploads in PHP.

Wednesday, August 24, 2022
4
foreach ($_POST as $i => $v) {
  if (preg_match("/^Qd+$/",$i)) {
    // Here we insert value $v to database
  }
}
Saturday, September 3, 2022
 
jpaugh
 
5

Percent-encode the query parameter. In JavaScript, you can use the encodeURIComponent function.

Saturday, August 20, 2022
 
zuhair
 
3

I have used this code for video upload, you can use it for your audio upload.

NSString *str = [NSString stringWithFormat:@"%@/uploadVideoIphone.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];      
NSString *filePath = [urlvideo path];
[request addFile:filePath forKey:@"uploadfile"];                        
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:500];
[request startSynchronous];
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseString %@",[request responseString]);
Wednesday, November 30, 2022
 
1

See questions I asked in comment at top.

Assuming you're using GNU sed, and that you're trying to add the trailing / to your tags to make XML-compliant <img /> and <input />, then replace the sed expression in your command with this one, and it should do the trick: '1h;1!H;${;g;s/(img|input)( [^>]*[^/])>/12/>/g;p;}'

Here it is on a simple test file (SO's colorizer doing wacky things):

$ cat test.html
This is an <img tag> without closing slash.
Here is an <img tag /> with closing slash.
This is an <input tag > without closing slash.
And here one <input attrib="1" 
    > that spans multiple lines.
Finally one <input
  attrib="1" /> with closing slash.

$ sed -n '1h;1!H;${;g;s/(img|input)( [^>]*[^/])>/12/>/g;p;}' test.html
This is an <img tag/> without closing slash.
Here is an <img tag /> with closing slash.
This is an <input tag /> without closing slash.
And here one <input attrib="1" 
    /> that spans multiple lines.
Finally one <input
  attrib="1" /> with closing slash.

Here's GNU sed regex syntax and how the buffering works to do multiline search/replace.

Alternately you could use something like Tidy that's designed for sanitizing bad HTML -- that's what I'd do if I were doing anything more complicated than a couple of simple search/replaces. Tidy's options get complicated fast, so it's usually better to write a script in your scripting language of choice (Python, Perl) that calls libtidy and sets whatever options you need.

Sunday, September 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :