[英]Why am I receiving a 'failed to open stream' error when submitting data from a contact form?
我創建了一個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 />';
?>
“ GENERATED PASSWORD”(通用密碼)是您使用Google的兩步驗證時所需的特定於應用程序的密碼,但是這里已將其刪除。
我已經在這里 , 這里和這里研究了以下問題,但是我似乎已經執行了建議。
我嘗試過修改'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");
錯誤消息顯示:
警告:require_once(/swift.php):無法打開流:沒有這樣的文件或目錄
您聲稱:
我相信此錯誤告訴我該文件不存在,但確實存在,並且我可以在那里瀏覽而沒有任何問題。
您所指控的證據是:
我的HTML文件和“ swift.php”文件本身都位於/ 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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.