PHP文件上传实现上传到另一台服务器
在php中我们如果要上传到另一台服务器就需要利用php一个ftp功能,在php中提供了 ftp_connect来连接服务器,然后把我们上传的数据传到远程服务器.
ftp_connect() 函数建立一个新的 FTP 连接,若成功,则返回一个连接标识,否则返回 false.
语法:ftp_connect(host,port,timeout)
host 必需,规定要连接的 FTP 服务器,可以是域名或 IP 地址,后面不应以斜线结尾,前面也不需要用 ftp:// 开头.
port 可选,规定 FTP 服务器的端口.
timeout 可选,规定该 FTP 服务器的超时时间,默认是 90 秒.
然后在台式机本地做了个测试,代码如下:
- $ftp_server = "192.168.1.100";
- $ftp_user_name = "laohu";
- $ftp_user_pass = "123456";
- $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
- $file = 'test.txt';
- $remote_file = '/test/a.txt';
-
- $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
- if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
- echo "文件移动成功";
- } else {
- echo "移动失败";
- }
- ftp_close($conn_id);
运行后,文件移动成功.
要的就是这个效果了,之后用台式机做程序服务器,上传附件时全用ftp方法上传至笔记本上,笔记本ip是105,相应代码如下:
- if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
- $ftp_server = "192.168.1.105";
- $ftp_user_name = "lesley";
- $ftp_user_pass = "123456";
- $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
- $file = $_FILES['uploadfile']['tmp_name'];
- $remote_file = '/test/'.$_FILES['uploadfile']['name'];
- $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
-
- if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
- echo "文件:".$_FILES['uploadfile']['name']."上传成功";
- } else {
- echo "上传失败";
- }
- ftp_close($conn_id);
- }
对应的前台页面代码如下:
- <form action="uploadfile.php" method="post" enctype="multipart/form-data">
- <input type="file" name="uploadfile" id="uploadfile" />
- <input type="submit" name="submit" value="submit" />
- </form>
运行后确实成功,下面看一个ftp文件上传类,代码如下:
- <?php
-
-
-
-
-
- class ftp
- {
- public $off;
- public $conn_id;
-
-
-
-
-
-
-
-
- function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)
- {
- $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP服务器连接失败");
- @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP服务器登陆失败");
- @ftp_pasv($this->conn_id,1);
- }
-
-
-
-
-
-
-
- function up_file($path,$newpath,$type=true)
- {
- if($type) $this->dir_mkdirs($newpath);
- $this->off = @ftp_put($this->conn_id,$newpath,$path,FTP_BINARY);
- if(!$this->off) echo "文件上传失败,请检查权限及路径是否正确!";
- }
-
-
-
-
-
-
-
- function move_file($path,$newpath,$type=true)
- {
- if($type) $this->dir_mkdirs($newpath);
- $this->off = @ftp_rename($this->conn_id,$path,$newpath);
- if(!$this->off) echo "文件移动失败,请检查权限及原路径是否正确!";
- }
-
-
-
-
-
-
-
-
- function copy_file($path,$newpath,$type=true)
- {
- $downpath = "c:/tmp.dat";
- $this->off = @ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);
- if(!$this->off) echo "文件复制失败,请检查权限及原路径是否正确!";
- $this->up_file($downpath,$newpath,$type);
- }
-
-
-
-
-
- function del_file($path)
- {
- $this->off = @ftp_delete($this->conn_id,$path);
- if(!$this->off) echo "文件删除失败,请检查权限及路径是否正确!";
- }
-
-
-
-
-
- function dir_mkdirs($path)
- {
- $path_arr = explode('/',$path);
- $file_name = array_pop($path_arr);
- $path_div = count($path_arr);
-
- foreach($path_arr as $val)
- {
- if(@ftp_chdir($this->conn_id,$val) == FALSE)
- {
- $tmp = @ftp_mkdir($this->conn_id,$val);
- if($tmp == FALSE)
- {
- echo "目录创建失败,请检查权限及路径是否正确!";
- exit;
- }
- @ftp_chdir($this->conn_id,$val);
- }
- }
-
- for($i=1;$i<=$path_div;$i++)
- {
- @ftp_cdup($this->conn_id);
- }
- }
-
-
-
-
- function close()
- {
- @ftp_close($this->conn_id);
- }
- }
-
- ?>
调用代码,代码如下:
- <?
-
-
-
-
-
- $ftp = new ftp('192.168.0.249',21,'hlj','123456');
- $ftp->up_file('aa.wav','test/13548957217/bb.wav');
-
-
-
- $ftp->close();
- ?>
需要注意,在用ftp_put方法时,第四个参数传送模式,需要用FTP_BINARY(二进制模式),用FTP_ASCII(文本模式)时,图片能上传但无法显示,其他文件重命名、中文乱码解决、上传权限控制等,就不在此提及了.
|