我创建了一个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'));
without success.
我还尝试了许多当前文件路径的排列(例如../
,包括完整的文件路径),也没有成功。
(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 />';
?>
"生成密码"是当你使用谷歌的两步验证时需要的应用程序特定的密码,但这当然已经被删除了。
我看了下面的问题,这里,这里和这里,但我似乎已经执行了给出的建议。
我已经尝试将'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;
我检查了下面的代码-
sendmessage.php第10行:
require_once(dirname(__FILE__)."/swift.php");
在swift.php第20行:
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'文件夹。
总结一下:
/Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/swift.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php
sendmessage.php第10行:
require_once(dirname(__FILE__)."/swift.php");
在swift.php第20行:
require(dirname(__FILE__)."/classes/Swift.php");
尝试改变,
require_once("/swift.php");
require_once("swift.php");
错误提示:
警告:require_once(/swift.php): failed to open stream: No such file or directory
你索赔:
我相信这个错误是告诉我文件不存在,但它确实存在,我可以在那里浏览没有问题。
而你所谓的证据是:
我的HTML文件和'swift.php'文件本身都在/Applications/XAMPP/xamppfiles/htdocs/testsite/
但这没有意义。/swift.php
是一个绝对路径,与你正在查找的文件夹无关。
去掉/
如果你想要一个相对路径:
require_once("/swift.php");