为什么我在从联系表单提交数据时收到“无法打开流"错误? [英] Why am I receiving a 'failed to open stream' error when submitting data from a contact form?
问题描述
我创建了一个 HTML5 联系表单,我目前正在使用 Swiftmailer 在本地主机 (XAMPP) 上测试该表单,单击提交按钮后,我收到以下错误:
I have created a HTML5 contact form that I'm currently testing on a local host (XAMPP) using Swiftmailer, and after clicking the submit button I get the following error:
Warning: require_once(/swift.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php on line 10
第 10 行显然是指我的 PHP 中的 require_once 行,但在我的文件中,它包含在 HTML 标记中.
Line 10 refers, obviously, to the require_once line in my PHP, however in my file it is encased in HTML tags.
我相信这个错误告诉我该文件不存在,但它确实存在,我可以毫无问题地浏览那里.
I believe that this error is telling me that the file does not exist, but it does and I can browse there with no problems.
我的 HTML 文件和 'swift.php'
文件本身都在 /Applications/XAMPP/xamppfiles/htdocs/testsite/
所以我不确定为什么我收到这个错误.我尝试了以下方法:
Both my HTML file and the 'swift.php'
file itself is in /Applications/XAMPP/xamppfiles/htdocs/testsite/
so I'm unsure why I'm getting this error. I have tried the following:
require_once(realpath(dirname(__FILE__).'swift.php'));
没有成功.
我还尝试了许多当前文件路径的排列(即像 ../
这样的东西,包括完整的文件路径,也没有成功.
I have also tried a number of permutations of present the file path (ie things like ../
, and including the full file path, also with no success.
我的 HTML (form.html)
是:
My HTML (form.html)
is:
<form id="contactform" name="contact" method="post" action="sendmessage.php">
<label for="Name">Name: *</label><br>
<input type="text" name="Name" id="Name"><br>
<label for="Email">Email: *</label><br>
<input type="Email" name="Email" id="Email" class="txt"><br>
<label for="Message">Message: *</label><br>
<textarea name="Message" rows="20" cols="20" id="Message" class="txtarea"></textarea><br>
<button type="submit" id="submit-button">Send E-mail</button>
</form>
我的 PHP ('sendmessage.php')
是(同样,这里没有包含在 HTML 和 BODY 标签中):
My PHP ('sendmessage.php')
is (again, not encased in HTML and BODY tags here):
require_once("/swift.php");
echo 'Mail sent <br />';
// Grab the post data
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$usermail = filter_var($_POST['usermail'], FILTER_SANITIZE_EMAIL);
$content = filter_var($_POST['content'], FILTER_SANITIZE_STRING);
// Construct the body of the email
$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;
// Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
->setUsername('foobar@googlemail.com')
->setPassword('GENERATED PASSWORD');
echo 'line 26 <br />';
// Create the mailer using the created transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('This is the subject')
->setFrom(array('foobar@googlemail.com' => 'Feedback Received From User'))
->setTo(array('somemail@gmail.com', 'foobar@googlemail.com' => 'Lead Recipients'))
->setSubject('Here is your Feedback subject')
->setBody($data, 'text/html');
echo 'line 34 <br />';
// Send the message
$result = $mailer -> send($message);
echo $result;
echo 'line 39<br />';
?>
生成的密码"是您使用 Google 的两步验证时所需的特定于应用程序的密码,但这里当然已将其删除.
The 'GENERATED PASSWORD' is the app specific one required when you use Google's Two Step Verification, but this has of course been removed here.
我在这里查看了以下问题,此处,以及这里 但我似乎已经执行了给出的建议.
I have looked at the following questions here, here, and here but I appear to have carried out the advice given.
我尝试将 'require_once("/swift.php");'
修改为 'require_once("swift.php");'
,但这给了我以下错误:
I have tried amending 'require_once("/swift.php");'
to 'require_once("swift.php");'
, however this gives me the following error:
Warning: require(/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/testsite/swift.php on line 20
不同之处在于现在在路径中添加了一个classes"子文件夹.将 'swift.php'
放在这个文件夹中会给我原始错误.
The difference is the addition of a 'classes' subfolder now in the path. Putting 'swift.php'
in this folder gives me the original error.
第 20 行供参考:
$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;
我查看了以下代码 -
I have looked to check the following code -
在第 10 行的 sendmessage.php 中:
In sendmessage.php on line 10:
require_once(dirname(__FILE__)."/swift.php");
在第 20 行的 swift.php 中:
In swift.php on line 20:
require(dirname(__FILE__)."/classes/Swift.php");
这些都是存在的并且是正确的.检查并重新运行后的错误是:
and these are present and correct. The error following checking and re-running is:
Warning: require(/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/classes/Swift.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php on line 20
每次我添加其中一个文件夹时,它似乎都在寻找一个额外的类"文件夹.
It appears to be looking for an extra 'classes' folder every time I add one of those folders.
推荐答案
总结一下:
/Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/swift.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php
在第 10 行的 sendmessage.php 中:
In sendmessage.php on line 10:
require_once(dirname(__FILE__)."/swift.php");
在第 20 行的 swift.php 中:
In swift.php on line 20:
require(dirname(__FILE__)."/classes/Swift.php");
这篇关于为什么我在从联系表单提交数据时收到“无法打开流"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!