2013-04-23 131 views
2

我需要自动从本地计算机上传文件到远程服务器。我发现这里对下面的代码:使用php上传FTP文件

<?php 
require_once('ftp.php'); 

// 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.php是我的文件与FTP认证信息。连接工程,但我收到以下错误:

There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv 

编辑:我amnot知道这是否有差别或没有,但这里是我的$ remote_file和我的$文件:

$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded 
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv"; 

什么我在这里做错了吗?另外,如果文件位于本地服务器的映射驱动器上,是否可以执行此操作? 谢谢。

+0

检查远程目录是否有权限上传文件? – Ranjith 2013-04-23 12:54:37

+0

为了测试目的,远程目录拥有777的权限,并且我刷新了该目录并再次尝试了相同的错误,所以我认为这不是问题 – Jim 2013-04-23 12:57:25

+0

仍然是正确的方式。很可能用户没有正确的权限来访问远程服务器.. – Ranjith 2013-04-23 13:02:46

回答

1

第一件事:尝试设置被动模式。如果你坐在防火墙后面,你需要它。 (大概是什么情况)

ftp_pasv($conn_id, true); // after ftp_login 

其次,你必须改变为DIR第一:如果你想知道到底发生了什么对

ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/'); 
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII); 

,试图让错误消息error_get_last()$php_errormsg

+0

我看了看服务器的错误日志,并得到这个:[STDERR] PHP警告:ftp_put(C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv)[function.ftp-put]:无法打开流:没有这样的文件或目录在/ 19行/chroot/home/bookcell/bookcellaronline.com/html/testbcos/accounting/ftpUpload.php我想我注定不要让这个工作 – Jim 2013-04-23 14:52:06

+0

尝试使用'$ file = realpath获取本地文件( __DIR __)。DIRECTORY_SEPARATOR.'checkslastmonth.csv';'否则错误很明显:本地文件是否真的存在于该位置? – bitWorking 2013-04-23 14:57:54

+0

@Jim所以它看起来像你在服务器上运行脚本,并想从你上传文件是本地机器?难道你需要一个HTML上传脚本,而不是一个FTP上传? – bitWorking 2013-04-23 15:01:45