- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>FTP File Upload</title>
- </head>
- <body>
- <?php
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- // 获取表单提交的数据
- $ftp_server = $_POST['ftp_server'];
- $ftp_port = $_POST['ftp_port'];
- $ftp_username = $_POST['ftp_username'];
- $ftp_password = $_POST['ftp_password'];
- $remote_file_path = '/' . basename($_FILES['file']['name']);
-
- // 将文件临时存储在脚本所在目录
- $upload_dir = dirname(__FILE__) . '/';
- $local_file_path = $upload_dir . basename($_FILES['file']['name']);
- if (move_uploaded_file($_FILES['file']['tmp_name'], $local_file_path)) {
- echo "File is valid, and was successfully uploaded.\n";
- // 打开本地文件
- $file = fopen($local_file_path, "r");
- if ($file === false) {
- echo "Failed to open local file.";
- exit;
- }
- // 初始化cURL会话
- $ch = curl_init();
- // 设置cURL选项
- curl_setopt($ch, CURLOPT_URL, "ftp://$ftp_server$remote_file_path");
- curl_setopt($ch, CURLOPT_USERPWD, "$ftp_username:$ftp_password");
- curl_setopt($ch, CURLOPT_UPLOAD, 1);
- curl_setopt($ch, CURLOPT_INFILE, $file);
- curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file_path));
- curl_setopt($ch, CURLOPT_VERBOSE, 1);
- // 执行cURL会话
- $result = curl_exec($ch);
- // 检查是否有错误
- if ($result === false) {
- echo "cURL Error: " . curl_error($ch);
- } else {
- echo "File uploaded successfully!";
- }
- // 关闭cURL会话和文件
- curl_close($ch);
- fclose($file);
- // 删除临时文件
- unlink($local_file_path);
- } else {
- echo "Possible file upload attack!\n";
- }
- } else {
- // 显示上传表单
- ?>
- <form action="" method="post" enctype="multipart/form-data">
- <label for="ftp_server">FTP Server:</label>
- <input type="text" id="ftp_server" name="ftp_server" value="" required><br>
- <label for="ftp_port">FTP Port:</label>
- <input type="number" id="ftp_port" name="ftp_port" value="21" required><br>
- <label for="ftp_username">Username:</label>
- <input type="text" id="ftp_username" name="ftp_username" required><br>
- <label for="ftp_password">Password:</label>
- <input type="password" id="ftp_password" name="ftp_password" required><br>
- <label for="file">Choose file to upload:</label>
- <input type="file" id="file" name="file" required><br>
- <input type="submit" value="Upload File">
- </form>
- <?php
- }
- ?>
- </body>
- </html>
复制代码
这个我之前写的一个给别的空间上传文件的php文件,不需要ftp模块,只需要curl模块,你看能不能通过的一个chez空间向你的另一个chez空间上传文件。chez该不会连curl都没吧。要是能的话发出来给大家用 |