为什么我在从联系表单提交数据时收到 'failed to open stream' 错误?

Why am I receiving a 'failed to open stream' error when submitting data from a contact form?

我已经使用 Swiftmailer 创建了一个 HTML5 联系表单,目前我正在本地主机 (XAMPP) 上进行测试,点击提交按钮后出现以下错误:

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 标签中。

我认为这个错误告诉我该文件不存在,但它确实存在,我可以毫无问题地浏览那里。

我的 HTML 文件和 'swift.php' 文件本身都在 /Applications/XAMPP/xamppfiles/htdocs/testsite/ 中,所以我不确定为什么会收到此错误。我尝试了以下方法:

require_once(realpath(dirname(__FILE__).'swift.php')); 没有成功。

我也尝试了一些当前文件路径的排列(例如 ../,包括完整的文件路径,也没有成功。

我的HTML(form.html)是:

<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 标签中):

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 />';
?>

'GENERATED PASSWORD' 是您使用 Google 的两步验证时所需的特定应用程序,但这当然已在此处删除。

我查看了以下问题here, here, and here,但我似乎已经执行了给出的建议。

我尝试将 'require_once("/swift.php");' 修改为 'require_once("swift.php");',但是这给了我以下错误:

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' 放入此文件夹中会出现原始错误。

供参考的第20行是:

$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;

我查看了以下代码 -

在第 10 行的 sendmessage.php 中:

require_once(dirname(__FILE__)."/swift.php");

在第 20 行的 swift.php 中:

require(dirname(__FILE__)."/classes/Swift.php");

这些都是正确的。检查并重新运行后的错误是:

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

每次我添加其中一个文件夹时,它似乎都在寻找一个额外的 'classes' 文件夹。

努力改变,

require_once("/swift.php");

require_once("swift.php");

错误消息说:

Warning: require_once(/swift.php): failed to open stream: No such file or directory

您声称:

I believe that this error is telling me that the file does not exisit, but it does and I can browse there with no problems.

你所谓的证据是:

Both my HTML file and the 'swift.php' file itself is in /Applications/XAMPP/xamppfiles/htdocs/testsite/

但这没有意义。
/swift.php 是绝对路径,与您正在查找的文件夹无关。

如果需要相对路径,去掉/

require_once("/swift.php");

总结一下:

/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 中:

require_once(dirname(__FILE__)."/swift.php");

在第 20 行的 swift.php 中:

require(dirname(__FILE__)."/classes/Swift.php");