-3

如何从单个 html 表单发布用户名、密码和多个二进制文件并使用 php 处理?我不允许使用ajax。

4

2 回答 2

6

首先在 PHP.net 上查看这些页面

  1. 文件上传信息
  2. move_uploaded_file

但是为了让你开始,这里有几个存根文件。

上传表单.html

<html>
<body>
    <form action="processStuff.php" enctype="multipart/form-data" method="POST">
        username: <input type="text" name="username" />
        password: <input type="password" name="password" />

        <p>
            <input type="file" name="uploadFile[]" /><br />
            <input type="file" name="uploadFile[]" /><br />
            <input type="file" name="uploadFile[]" /><br />
            <!-- Add as many of these as you want -->
        </p>

        <p>
            <input type="submit" />
        </p>
    </form>
</body>
</html>

processStuff.php

<pre>
<?php

    echo '<h2>Username & password</h2>'
    echo "Username: {$_POST['username']}\nPassword: {$_POST['password']}";
    echo '<hr />';

    echo '<h2>Uploaded files</h2>'  
    foreach($_FILES['uploadFile']['tmp_name'] as $i => $tempUploadPath) {
        if (empty($tempUploadPath)) {
            // this <input type="file" /> was "blank"... no file selected
        } else {
            // a file was uploaded
            echo '<strong>A file named "', $_FILES['uploadFile']['name'][$i], "\" was uploaded</strong>\n";
            echo "\ttemporarily stored at: ", $tempUploadPath, "\n";            
            echo "\tmime type: ", $_FILES['uploadFile']['type'][$i], "\n";
            echo "\tsize: ", $_FILES['uploadFile']['size'][$i], " bytes\n";         
            echo "\terror code", 
            ((empty($_FILES['uploadFile']['size'][$i]) 
                    ? '<em>no errror</em>' 
                    : $_FILES['uploadFile']['size'][$i])), 
            "\n\n";

    // do something useful with the uploaded file 
        // access it via $tempUploadPath and use move_uploaded_file() to move 
        //     it out of the temp path before you manipulate it in any way!!!!!
        // see http://us3.php.net/features.file-upload
        // and http://us3.php.net/manual/en/function.move-uploaded-file.php
        }       
    }

?>
</pre>

HTML 文件展示了如何设置enctype第二<form>个表单,展示如何访问提交的用户名和密码,最后如何循环遍历每个上传的文件。

如前所述,您必须尽快移动文件。它们被上传到临时位置,除非您处理它们,否则系统将删除它们。所以先把它们移到某个地方,然后用它们做任何你需要的事情。

希望这有帮助

阿林

于 2008-09-26T05:28:00.000 回答
1

您应该使用$_FILES超全局和move_uploaded_file()函数来查看哪些文件已成功上传,并将它们移动到最终位置,以防万一。

$_POST全局将包含提交的用户名和密码。

于 2008-09-26T04:16:07.177 回答