以单一形式上传多个文件

发布于 2025-02-10 08:13:27 字数 3967 浏览 1 评论 0原文

我的网站上有一个表格,该表单要求用户在一些基本字段中输入数据,然后上传多个图像文件以提交其表格。

方程式的“形式”侧似乎很容易,只需将倍数=“多个”添加到输入字段。

但是...经过多次尝试的代码变化,我仍然不了解(或能够)使等式的php“ formhandler”侧来实际发送多个图像文件。

用代码玩弄真的很令人困惑,我把它弄得一团糟。

所以....

在此示例中,我在表单输入中留下了“多个”方面,但是...要清理我的烂摊子...我将PHP“ formhandler”代码剥离了,仅发送单个图像文件的工作正常。

关于修改php“ formhandler”正确发送多个图像的任何建议都是救生的,并且值得赞赏。

提前致谢!

表格:

<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">


</head>

<body>

<h1>The Form</h1>

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

<input type="text" id="userName" name="userName" class="inputField" placeholder=" Enter Your 
Name" required><br><br>

<input type="email" id="userEmail" name="userEmail" class="inputField" placeholder=" Enter 
Your Email Address" required><br><br><br>


<!-- Start Image Upload Section -->

<a>Select Multiple Images:</a><br><br>

<input type="file" id="file" name="userImages[]" multiple="multiple" required/><br><br><br><br>

<!-- End Image Upload Section -->


<input type="submit" id="submitButton" name="submitButton" value="Submit Form">


</body>
</html>

php“ formhandler”:

<?php

////////////////////////////////////////////////////////////////////////////

$filenameee =  $_FILES['userImages']['name'];
$fileName = $_FILES['userImages']['tmp_name'];
$name = $_POST['userName'];
$email = $_POST['userEmail'];

////////////////////////////////////////////////////////////////////////////

$composition =

"\r\n\n\nName: " . $name .
"\r\nEmail Address: " . $email;

////////////////////////////////////////////////////////////////////////////

//Whatever you want to subject of the email to be when you receive the form.

$subject ="Form Submission";

////////////////////////////////////////////////////////////////////////////

//User name and email will be displayed in header when form is received.

$fromname ="$name";
$fromemail = "$email";

////////////////////////////////////////////////////////////////////////////

//The email address at which you want to receive the form.

$mailto = '[email protected]';

////////////////////////////////////////////////////////////////////////////

//The Content Action

$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));

//A random hash will be necessary to send mixed content.

$separator = md5(time());

//Carriage return type (RFC)

$eol = "\r\n";

//Main header (multipart mandatory)

$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;


$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $composition . $eol;


$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

////////////////////////////////////////////////////////////////////////////

//Mail send action

@mail($mailto, $subject, $body, $headers);

////////////////////////////////////////////////////////////////////////////

//Page Re-direct after form submission

header('location: /formSent_confirmationPage.php');


?>

I have a form on my site which requires users to enter data in a few basic fields, and then upload multiple image files to submit with their form.

The 'form' side of the equation seems easy enough, by simply adding multiple="multiple" to the input field.

However... after many attempts at different variations of code, I am still not understanding (or able) to get the php 'formHandler' side of the equation to actually send multiple image files.

Toying with the code got really confusing, and I made an awful mess of it.

So....

For this example, I left the 'multiple' aspect in the form input, however... to clean up my mess... I stripped back the php 'formHandler' code to where I had it before, which works fine for sending just a single image file.

Any advice as to modify the php 'formHandler' to properly send multiple images would be a life-saver and much appreciated.

Thanks in advance!

The Form:

<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">


</head>

<body>

<h1>The Form</h1>

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

<input type="text" id="userName" name="userName" class="inputField" placeholder=" Enter Your 
Name" required><br><br>

<input type="email" id="userEmail" name="userEmail" class="inputField" placeholder=" Enter 
Your Email Address" required><br><br><br>


<!-- Start Image Upload Section -->

<a>Select Multiple Images:</a><br><br>

<input type="file" id="file" name="userImages[]" multiple="multiple" required/><br><br><br><br>

<!-- End Image Upload Section -->


<input type="submit" id="submitButton" name="submitButton" value="Submit Form">


</body>
</html>

The php 'formHandler':

<?php

////////////////////////////////////////////////////////////////////////////

$filenameee =  $_FILES['userImages']['name'];
$fileName = $_FILES['userImages']['tmp_name'];
$name = $_POST['userName'];
$email = $_POST['userEmail'];

////////////////////////////////////////////////////////////////////////////

$composition =

"\r\n\n\nName: " . $name .
"\r\nEmail Address: " . $email;

////////////////////////////////////////////////////////////////////////////

//Whatever you want to subject of the email to be when you receive the form.

$subject ="Form Submission";

////////////////////////////////////////////////////////////////////////////

//User name and email will be displayed in header when form is received.

$fromname ="$name";
$fromemail = "$email";

////////////////////////////////////////////////////////////////////////////

//The email address at which you want to receive the form.

$mailto = '[email protected]';

////////////////////////////////////////////////////////////////////////////

//The Content Action

$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));

//A random hash will be necessary to send mixed content.

$separator = md5(time());

//Carriage return type (RFC)

$eol = "\r\n";

//Main header (multipart mandatory)

$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;


$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $composition . $eol;


$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

////////////////////////////////////////////////////////////////////////////

//Mail send action

@mail($mailto, $subject, $body, $headers);

////////////////////////////////////////////////////////////////////////////

//Page Re-direct after form submission

header('location: /formSent_confirmationPage.php');


?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2025-02-17 08:13:27

将输入文件名更改为UserImages [],该文件允许填充数组并通过$ _FILES元素进行迭代($ fileName现在是一个数组)。
https://www.php.php.net/manual/enual/en/en/功能。file-upload.multiple.php

$filenameee =  $_FILES['userImages']['name'];
$sizes =  $_FILES['userImages']['size'];
$fileName = $_FILES['userImages']['tmp_name'];
foreach ($fileName as $tmpname) {
    echo "<br> Tmp name: " . $tmpname;
}
foreach ($filenameee as $name) {
    echo "<br> Chosen user name : " . $name;
}
foreach ($sizes as $size) {
    echo "<br> File size : " . $size/1024 . " Ko";
}

Change input file name to userImages[] that allows to populate an array and iterate through $_files element ($filename is now an array).
https://www.php.net/manual/en/features.file-upload.multiple.php

$filenameee =  $_FILES['userImages']['name'];
$sizes =  $_FILES['userImages']['size'];
$fileName = $_FILES['userImages']['tmp_name'];
foreach ($fileName as $tmpname) {
    echo "<br> Tmp name: " . $tmpname;
}
foreach ($filenameee as $name) {
    echo "<br> Chosen user name : " . $name;
}
foreach ($sizes as $size) {
    echo "<br> File size : " . $size/1024 . " Ko";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文