HTML表单FTP上传失败求助:ftp_put报错找不到文件或目录
Hey there, let's break down why your FTP upload is failing with that frustrating "No such file or directory" error. The message clearly tells us that when your PHP script runs ftp_put(fichier.txt) on line 97, it can't locate the local file you're trying to upload. Here's how to diagnose and fix this step by step:
1. Double-Check the Local File Path
The most common issue here is an incorrect file path. PHP uses its current working directory (not necessarily the folder where your script lives) to resolve relative paths like fichier.txt.
- First, add this line right before your
ftp_put()call to see where PHP is looking:
Isecho "Current working directory: " . getcwd();fichier.txtactually stored in that directory? - To eliminate path ambiguity entirely, use an absolute path. You can use
__DIR__(which returns the directory of your current PHP script) to build a reliable path:$localFilePath = __DIR__ . '/fichier.txt'; // Adjust the relative part if the file is in a subfolder // Validate the file before uploading if (!file_exists($localFilePath)) { die("Error: Local file $localFilePath doesn't exist!"); } if (!is_readable($localFilePath)) { die("Error: PHP doesn't have permission to read $localFilePath!"); } // Now run ftp_put with the absolute path ftp_put($ftpConnection, 'remote_fichier.txt', $localFilePath, FTP_ASCII);
2. Verify File Permissions
Even if the file exists, the user running your PHP script (usually www-data for Apache, nginx for Nginx, or your system user for CLI scripts) might not have permission to read it.
- On Linux/macOS, run these commands in your terminal to adjust permissions:
# Give read access to the file chmod 644 /path/to/your/fichier.txt # Give execute access to the parent directory (so PHP can navigate to the file) chmod 755 /path/to/your/file/directory
3. Fix HTML Form Upload Logic (If Applicable)
If fichier.txt is supposed to be uploaded via an HTML form, you're probably referencing the wrong file path. Browser-uploaded files are stored in a temporary location, not under their original filename.
- Make sure your HTML form includes the
enctype="multipart/form-data"attribute (required for file uploads):<form method="POST" enctype="multipart/form-data"> <input type="file" name="user_upload" id="user_upload"> <button type="submit">Upload to FTP</button> </form> - In your PHP script, use the temporary path from the
$_FILESarray instead of the original filename:if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['user_upload'])) { // Check if the upload was successful if ($_FILES['user_upload']['error'] !== UPLOAD_ERR_OK) { die("File upload failed with error code: " . $_FILES['user_upload']['error']); } $tempFilePath = $_FILES['user_upload']['tmp_name']; $remoteFileName = 'fichier.txt'; // Name to use on the FTP server // Upload the temporary file ftp_put($ftpConnection, $remoteFileName, $tempFilePath, FTP_BINARY); }
4. Quick Debugging Hacks
- Always validate the local file before calling
ftp_put()—usingfile_exists()andis_readable()will catch issues early and avoid vague error messages. - Print the full local file path to the screen to confirm it's exactly what you expect:
echo "Attempting to upload: " . $localFilePath;
Start with verifying the file path first—this is almost always the root cause of this specific error. Let me know if you hit any snags while testing these fixes!
内容的提问来源于stack exchange,提问作者SkyDyz

