Asked  7 Months ago    Answers:  5   Viewed   37 times

I want to upload files using PHP but the problem is that I don't know how many files I will upload.

My question is how can I upload files if I use file[]?

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />

I will add just File box and I will use JavaScript to create more file input to upload but how to handle them in PHP?



See: $_FILES, Handling file uploads

        // Number of uploaded files
        $num_files = count($_FILES['file']['tmp_name']);

        /** loop through the array of files ***/
        for($i=0; $i < $num_files;$i++)
            // check if there is a file in the array
                $messages[] = 'No file uploaded';
                // copy the file to the specified dir 
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['file']['name'][$i].' uploaded';
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
Wednesday, March 31, 2021
answered 7 Months ago

This is what I use, you can customize it to suit your script:

Simply change the *path and *variables.

// Configuration - Your Options
$allowed_filetypes = array('.mov','.mp3','.mp4','.flv'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

// Check if the filetype is allowed, if not DIE and inform the user.
die('The file you attempted to upload is not allowed.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');

// Check if we can upload to the specified path, if not DIE and inform the user.
die('You cannot upload to the specified directory, please CHMOD it to 777.');

// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
echo 'There was an error during the file upload. Please try again.'; // It failed :(.

Wednesday, March 31, 2021
answered 7 Months ago

It was a problem with mod_fcgid.

The MaxRequestLen needs to be set higher.

The solution is here:

Saturday, May 29, 2021
answered 5 Months ago
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement("uploadfile"));

Hey, that's mine from somewhere :).

In case of the Zamzar web, it should work perfectly. You don't click the element. You just type the path into it. To be concrete, this should be absolutely ok:


In the case of the Uploadify web, you're in a pickle, since the upload thing is no input, but a Flash object. There's no API for WebDriver that would allow you to work with browser dialogs (or Flash objects).

So after you click the Flash element, there'll be a window popping up that you'll have no control over. In the browsers and operating systems I know, you can pretty much assume that after the window has been opened, the cursor is in the File name input. Please, make sure this assumption is true in your case, too.

If not, you could try to jump to it by pressing Alt + N, at least on Windows...

If yes, you can "blindly" type the path into it using the Robot class. In your case, that would be something in the way of:

Robot r = new Robot();
r.keyPress(KeyEvent.VK_C);        // C
r.keyPress(KeyEvent.VK_COLON);    // : (colon)
r.keyPress(KeyEvent.VK_SLASH);    // / (slash)
// etc. for the whole file path

r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end

It sucks, but it should work. Note that you might need these: How can I make Robot type a `:`? and Convert String to KeyEvents (plus there is the new and shiny KeyEvent#getExtendedKeyCodeForChar() which does similar work, but is available only from JDK7).

For Flash, the only alternative I know (from this discussion) is to use the dark technique:

First, you modify the source code of you the flash application, exposing internal methods using the ActionScript's ExternalInterface API. Once exposed, these methods will be callable by JavaScript in the browser.

Second, now that JavaScript can call internal methods in your flash app, you use WebDriver to make a JavaScript call in the web page, which will then call into your flash app.

This technique is explained further in the docs of the flash-selenium project. (, but the idea behind the technique applies just as well to WebDriver.

Saturday, June 5, 2021
answered 5 Months ago

If you want to upload large files, you'll definitely need to look into WCF Streaming Mode.

Basically, you can change the transfer mode on your binding; by default, it's buffered, i.e. the whole message needs to be buffered on the sender, serialized, and then transmitted as a whole.

With Streaming, you can define either one-way streaming (for uploads only, for downloads only) or bidirectional streaming. This is done by setting the transferMode of your binding to StreamedRequest, StreamedResponse, or just plain Streamed.

      <binding name="HttpStreaming" 

Then you need to have a service contract which either receives a parameter of type Stream (for uploads), or returns a value of type Stream (for downloads).

public interface IFileUpload
    bool UploadFile(Stream stream);

That should do it!

Thursday, June 17, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :