如何用PHP将表单数据保存到本地文本文件?提交遇404错误求助
Hey Alex, no worries at all—we’ve all stumbled through our first PHP form issues! Let’s walk through the most common reasons you’re seeing that "404 file or directory not found" error and fix them one by one:
1. Double-Check Your Form's action Attribute
The most likely culprit is that your form isn’t pointing to the correct script file. Make sure your form tag looks something like this:
<form action="process-form-data.php" method="POST">
- Watch for case sensitivity: Most web servers (like Apache on Linux) are case-sensitive. If your script is named
Process-Form-Data.phpbut youractionuses lowercase, it’ll throw a 404. - Relative path issues: If your form HTML is in a subfolder (e.g.,
public_html/contact/form.html), you’ll need to adjust the path. Use../process-form-data.phpto go up one folder, or use an absolute root path like/process-form-data.php(starts with a slash to point directly to the public_html root).
2. Verify File Locations via FTP
Log back into your FTP client and confirm:
- Both
process-form-data.phpandformdata.txtare directly inside thepublic_htmlfolder (not in a subfolder likescriptsoruploads). - The files actually finished uploading—sometimes FTP transfers fail silently, so re-upload them if you’re unsure.
3. Check for Filename Typos or Hidden Characters
It’s easy to make a small mistake here:
- Did you accidentally name the script
process-form-data.ph(missing the lastp)? Orprocessformdata.php(no hyphens)? - Avoid spaces in filenames—something like
process form data.phpwill cause a 404 because the server interprets the space as part of the URL.
4. Test the Script Directly in Your Browser
Try visiting the script’s URL directly (e.g., https://yourdomain.com/process-form-data.php).
- If you still get a 404: The file is definitely in the wrong place or misnamed.
- If you see a blank page or a PHP error: Great! The script is reachable—now we can fix the data-saving logic instead.
5. Check File Permissions (Less Common but Possible)
While 404 is a "not found" error, incorrect permissions can sometimes cause servers to block access and return 404 instead of a permission error. Set:
process-form-data.phpto644(owner read/write, group/others read-only)formdata.txtto666(world-writable, so the server can save data to it)- Folders containing the files to
755
Quick Test Script to Confirm Functionality
If you want to rule out script issues once the 404 is fixed, replace your process-form-data.php with this simple test version:
<?php // Confirm the script is reachable echo "Script is running! Let's handle your form data:<br>"; // Check if form was submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Sanitize input (basic protection) $name = htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); $email = htmlspecialchars($_POST['email'] ?? '', ENT_QUOTES); // Prepare data string $entry = "[" . date('Y-m-d H:i:s') . "] Name: $name | Email: $email\n"; // Attempt to write to file if (file_put_contents("formdata.txt", $entry, FILE_APPEND)) { echo "Success! Your data was saved."; } else { echo "Error: Could not write to formdata.txt. Check file permissions."; } } ?>
This script will tell you exactly if the data is saving or if there’s a permission issue with the text file.
内容的提问来源于stack exchange,提问作者Alex

