尝试使用HTML表单和PHP将文件上载到FTP服务器。我的HTML表单是:
<!DOCTYPE html>
<html>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="files" id="files">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
uploader.php:
<?php
$conn_id = ftp_connect ( 'ftp.mydomain.com' );
$ftp_user_name="<username>";
$ftp_user_pass="<password>";
// 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!" ;
exit;
} else {
echo "Connected to for user $ftp_user_name" ;
}
if (empty($_FILES['files']['tmp_name'])) {
echo "No file!!!!!!!!!";
die();
}
// upload the file
$upload = ftp_put( $conn_id, "uploads/" , $_FILES['files']['tmp_name'] , FTP_BINARY );
// check upload status
if(!$upload){
echo "FTP upload has failed!" ;
} else {
echo "Successfully Uploaded." ;
}
?>
我得到的输出是Successfully Uploaded.
,但实际上没有文件可以在服务器中上传。
uploads/
目录位于同一个uploader.php目录中。试图将其改为uploads/somename.zip
表明uploads/somename.zip is not a valid directoty
。
ftp_put
的第二个参数是远程文件的路径,而不是本地目录的路径。
所以应该是这样的:
ftp_put($conn_id, "/remote/path/somename.zip", $_FILES['files']['tmp_name'], FTP_BINARY);
此外,您应该使用被动模式。见PHP ftp_put fails。