【发布时间】:2014-12-02 19:10:43
【问题描述】:
我想连接到一个远程 FTP 服务器并将图像文件上传到该服务器的某个特定位置。我能够连接到服务器,但无法上传文件。
每次函数 ftp_put() 都返回 false。我调试了很多代码,但我不明白我在哪里犯了错误。
以下是我的代码:
HTML 代码:
<form method="post" enctype="multipart/form-data" action="xyz.php">
<input type="file" name="student_image" id="student_image" />
</form>
PHP代码(xyz.php):
$t = time();
$allowed_image_extension = array("jpg","jpeg","gif","png","JPG","JPEG","GIF","PNG");
if(!empty($_FILES['student_image']['name'])) {
$ext = pathinfo($_FILES['student_image']['name'], PATHINFO_EXTENSION);
$student_image_name = 'student_'.$t.'.'.$ext;
$_POST['student_name'] = $student_image_name;
$ftp_server="52.237.5.85";
$ftp_user_name="myservercreds";
$ftp_user_pass="MyServerCreds";
$file = $_FILES['student_image']['name'];//tobe uploaded
$remote_file = "/Students/".$_POST['student_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
}
在调试过程中,除了函数ftp_put() 中的一处之外,我得到了 True 和资源。
【问题讨论】:
-
可以
ftp_chdir()到Students的目录吗? -
/Students/foo.txt?你的上传真的是在文件系统的根目录下吗?除非那是 chroot'ed ftp,否则您实际上是在尝试使用c:\students\foo.txt -
你需要在 /var/log/{your-ftp-log} 上拖尾 - 这可能是权限问题
标签: php file-upload ftp image-uploading remote-server