问题
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:
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.
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. 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:
<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):
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.
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");'
to '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. Putting 'swift.php'
in this folder gives me the original error.
Line 20 for reference is:
$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;
I have looked to check the following code -
In sendmessage.php on line 10:
require_once(dirname(__FILE__)."/swift.php");
In swift.php on line 20:
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.
回答1:
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:
require_once(dirname(__FILE__)."/swift.php");
In swift.php on line 20:
require(dirname(__FILE__)."/classes/Swift.php");
回答2:
try to change,
require_once("/swift.php");
to
require_once("swift.php");
回答3:
The error message says:
Warning: require_once(/swift.php): failed to open stream: No such file or directory
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/
But that does not make sense./swift.php
is an absolute path and has nothing to do with the folder you're looking in.
Take out the /
if you wanted a relative path:
require_once("/swift.php");
来源:https://stackoverflow.com/questions/31662334/why-am-i-receiving-a-failed-to-open-stream-error-when-submitting-data-from-a-c