我已经尝试使用php中的ftp连接将文件上传到服务器并且它无法正常工作,它的连接却收到错误,例如“连接到XXXXXXXXXXX,用户XXXXXXXXXXXXX FTP上传失败了!”我已经尝试过以下代码请帮助纠正它,..
image.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php的
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$destination_file = "imagetest/123/".$_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
答案 0 :(得分:2)
我已经对您的代码进行了测试并且有一段时间难以让它运行,但对我来说有用的是使用/http_docs
或/public_html
之类的代码作为基础/根文件夹。
I.e。并进行了一些修改:
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$folder = "/http_docs/imagetest/123/";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// rest of code
<强> 旁注: 强>
不使用完整路径名。
即:/var/user/you/public_html/
它不会起作用。