简体   繁体   English

从联系表单提交数据时,为什么会收到“无法打开流”错误?

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

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: 我创建了一个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

Line 10 refers, obviously, to the require_once line in my PHP, however in my file it is encased in HTML tags. 显然,第10行引用了我的PHP中的require_once行,但是在我的文件中,该行包含在HTML标记中。

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. 我相信这个错误告诉我该文件不存在,但是确实存在,并且我可以在那里毫无问题地浏览。

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. 我的HTML文件和'swift.php'文件本身都位于/Applications/XAMPP/xamppfiles/htdocs/testsite/因此我不确定为什么会出现此错误。 I have tried the following: 我尝试了以下方法:

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

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. 我还尝试了一些对当前文件路径的排列(例如../东西,包括完整的文件路径,但也没有成功)。

My HTML (form.html) is: 我的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>

My PHP ('sendmessage.php') is (again, not encased in HTML and BODY tags here): 我的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 />';
?>

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. “ GENERATED PASSWORD”(通用密码)是您使用Google的两步验证时所需的特定于应用程序的密码,但是这里已将其删除。

I have looked at the following questions here , here , and here but I appear to have carried out the advice given. 我已经在这里这里这里研究了以下问题,但是我似乎已经执行了建议。

I have tried amending 'require_once("/swift.php");' 我尝试过修改'require_once("/swift.php");' to 'require_once("swift.php");' '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

The difference is the addition of a 'classes' subfolder now in the path. 区别在于现在在路径中添加了“ classes”子文件夹。 Putting 'swift.php' in this folder gives me the original error. 'swift.php'放入此文件夹会给我原始错误。

Line 20 for reference is: 供参考的第20行是:

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

I have looked to check the following code - 我已经看过检查以下代码-

In sendmessage.php on line 10: 在第10行的sendmessage.php中:

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

In swift.php on line 20: 在第20行的swift.php中:

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. 每当我添加其中一个文件夹时,它似乎都在寻找一个额外的“ classes”文件夹。

try to change, 尝试改变

require_once("/swift.php");

to

require_once("swift.php");

The error message says: 错误消息显示:

Warning: require_once(/swift.php): failed to open stream: No such file or directory 警告:require_once(/swift.php):无法打开流:没有这样的文件或目录

You claim: 您声称:

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. 我相信此错误告诉我该文件不存在,但确实存在,并且我可以在那里浏览而没有任何问题。

And your alleged proof is: 您所指控的证据是:

Both my HTML file and the 'swift.php' file itself is in /Applications/XAMPP/xamppfiles/htdocs/testsite/ 我的HTML文件和“ swift.php”文件本身都位于/ Applications / XAMPP / xamppfiles / htdocs / testsite /

But that does not make sense. 但这没有意义。
/swift.php is an absolute path and has nothing to do with the folder you're looking in. /swift.php是绝对路径,与您要查找的文件夹无关。

Take out the / if you wanted a relative path: 如果需要相对路径,请取出/

require_once("/swift.php");

To summarize it: 总结一下:

/Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/swift.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php

In sendmessage.php on line 10: 在第10行的sendmessage.php中:

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

In swift.php on line 20: 在第20行的swift.php中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM