PHP實(shí)現(xiàn)ftp上傳文件示例
更新時間:2014年08月21日 14:46:31 投稿:shichen2014
這篇文章主要介紹了PHP實(shí)現(xiàn)ftp上傳文件的方法,是非常實(shí)用的技巧,需要的朋友可以參考下
FTP上傳是PHP實(shí)現(xiàn)的一個常見且非常重要的應(yīng)用技巧,今天就來與大家分享一下PHP實(shí)現(xiàn)FTP上傳文件的簡單示例。希望對大家的PHP學(xué)習(xí)能帶來一定的幫助。
主要代碼如下:
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創(chuàng)建目錄
make_directory($conn,$path);
//利用ftp選擇進(jìn)入目錄
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權(quán)限設(shè)置
感興趣的朋友可以測試運(yùn)行或改寫本文所述代碼,加深理解的同時可以讓代碼功能更加完善。
相關(guān)文章
PHP獲取文件擴(kuò)展名的常用方法小結(jié)【五種方式】
這篇文章主要介紹了PHP獲取文件擴(kuò)展名的常用方法,結(jié)合實(shí)例形式總結(jié)分析了php獲取文件擴(kuò)展名的五種常見操作技巧,需要的朋友可以參考下2018-04-04
php+html5基于websocket實(shí)現(xiàn)聊天室的方法
這篇文章主要介紹了php+html5基于websocket實(shí)現(xiàn)聊天室的方法,實(shí)例分析了php結(jié)合html5的websocket通訊的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
php引用和拷貝的區(qū)別知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于php引用和拷貝的區(qū)別以及相關(guān)知識點(diǎn)總結(jié),需要的朋友們學(xué)習(xí)下。2019-09-09
PHP實(shí)現(xiàn)簡單鑒權(quán)的示例代碼
這篇文章主要為大家詳細(xì)介紹了php如何通過在header增加key,sign,timestamp來實(shí)現(xiàn)鑒權(quán),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2023-12-12

