0

我在传输文件时遇到问题。我在做什么错为什么我不能上传两个文件?我可以发送一个 ($_FILES['filename'] 当我添加这个 ($_FILES['filename']['filename2'] 不起作用。也许还有另一种发送 2 个文件的方法?

php

<?php # Script - upload_image.php

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

    // Check for an uploaded file:
    if (isset($_FILES['filename']['filename2'])) {

        // Validate the type. Should be JPEG or PNG.
        $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
        if (in_array($_FILES['filename']['filename2']['type'], $allowed)) {

            // Move the file over.
            if (move_uploaded_file ($_FILES['filename']['filename2']['tmp_name'], "../images/{$_FILES['filename']['filename2']['name']}")) {
                echo '<p><em>The file has been uploaded!</em></p>';
            } // End of move... IF.

        } else { // Invalid type.
            echo '<p class="error">Please upload a JPEG or PNG image.</p>';
        }

    } // End of isset($_FILES['upload']) IF.

    // Check for an error:

    if ($_FILES['filename']['filename2']['error'] > 0) {
        echo '<p class="error">The file could not be uploaded because: <strong>';

        // Print a message based upon the error.
        switch ($_FILES['filename']['filename2']['error']) {
            case 1:
                print 'The file exceeds the upload_max_filesize setting in php.ini.';
                break;
            case 2:
                print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
                break;
            case 3:
                print 'The file was only partially uploaded.';
                break;
            case 4:
                print 'No file was uploaded.';
                break;
            case 6:
                print 'No temporary folder was available.';
                break;
            case 7:
                print 'Unable to write to the disk.';
                break;
            case 8:
                print 'File upload stopped.';
                break;
            default:
                print 'A system error occurred.';
                break;
        } // End of switch.

        print '</strong></p>';

    } // End of error IF.

    // Delete the file if it still exists:
    if (file_exists ($_FILES['filename']['filename2']['tmp_name']) && is_file($_FILES['filename']['filename2']['tmp_name']) ) {
        unlink ($_FILES['filename']['filename2']['tmp_name']);
    }

} // End of the submitted conditional.
?>

形式

<form enctype="multipart/form-data" action="photo_upload_6.php" method="post">

    <input type="hidden" name="MAX_FILE_SIZE" value="524288">

    <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>

       <li>
        <label for="filename">Image:</label>
        <input name="filename" type="file" id="filename" tabindex="10">
       </li>

       <li>
        <label for="filename2">Image2:</label>
        <input name="filename2" type="file" id="filename2" tabindex="15">
       </li>

    </fieldset>
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>
    <input type="hidden" name="submitted" value="TRUE" />
</form>
4

1 回答 1

0

try this

HTML Part. You can use input name as array with same name

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="upload[]" type="file" /><br />
  <input name="upload[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

PHP Code

<?php
for ($i = 0; $i < count($_FILES['upload'][name]); $i++) {
    $fileLink = $_FILES['upload']['name'][$i];
    $fileType = $_FILES['upload']['type'][$i];
    $fileSize = ($_FILES['upload']['type'][$i]) / 1024;
    $source = $fileLink;
    move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $source);
}
?>

use loop and check count by count($_FILES['upload'][name])

Please let me know the result

于 2013-10-31T11:38:34.147 回答