PHP实现ftp上传文件示例

2025-05-29 0 58

FTP上传是PHP实现的一个常见且非常重要的应用技巧,今天就来与大家分享一下PHP实现FTP上传文件的简单示例。希望对大家的PHP学习能带来一定的帮助。

主要代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35
function make_directory($ftp_stream, $dir){

// if directory already exists or can be immediately created return true

if ($this->ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;

// otherwise recursively try to make the directory

if (!$this->make_directory($ftp_stream, dirname($dir))) return false;

// final step to create the directory

return ftp_mkdir($ftp_stream, $dir);

}

function ftp_is_dir($ftp_stream, $dir){

// get current directory

$original_directory = ftp_pwd($ftp_stream);

// test if you can change directory to $dir

// suppress errors in case $dir is not a file or not a directory

if ( @ftp_chdir( $ftp_stream, $dir ) ) {

// If it is a directory, then change the directory back to the original directory

ftp_chdir( $ftp_stream, $original_directory );

return true;

} else {

return false;

}

}

$conn = ftp_connect("ftp地址") or die("Could not connect");

ftp_login($conn,"ftpname","password");

//利用ftp创建目录

make_directory($conn,$path);

//利用ftp选择进入目录

ftp_chdir($conn,$path);

//开始上传

if(ftp_put($conn,$info[0]['savename'],getcwd().$upload->savePath.$info[0]['savename'],FTP_BINARY)){

unlink(getcwd().$upload->savePath.$info[0]['savename']);

}

ftp_close($conn);

//注意上传端的ftp权限设置

感兴趣的朋友可以测试运行或改写本文所述代码,加深理解的同时可以让代码功能更加完善。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现ftp上传文件示例 https://www.kuaiidc.com/104397.html

相关文章

发表评论
暂无评论