从联系表单提交数据时,为什么我收到“未能打开的流”错误?

时间:2015-07-27 20:10:37

标签: php swiftmailer

我创建了一个HTML5联系表单,我目前正在使用Swiftmailer在本地主机(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 />';
?>

“生成的密码”是​​您使用Google的两步验证时所需的特定应用,但这当然已在此处删除。

我查看了以下问题hereherehere,但我似乎已经提出了建议。

我已尝试将'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

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

3 个答案:

答案 0 :(得分:1)

尝试改变,

require_once("/swift.php");

require_once("swift.php");

答案 1 :(得分:1)

错误消息显示:

  

警告:require_once(/swift.php):无法打开流:没有这样的文件或目录

你声称:

  

我相信这个错误告诉我文件没有存在,但确实存在,我可以在那里浏览没有问题。

你所谓的证据是:

  

我的HTML文件和&#39; swift.php&#39;文件本身位于/ Applications / XAMPP / xamppfiles / htdocs / testsite /

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

如果您想要相对路径,请取出/

require_once("/swift.php");

答案 2 :(得分:1)

总结一下:

/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");