1. 程式人生 > >yii2 ftp 的常規操作 上傳 下載

yii2 ftp 的常規操作 上傳 下載

rec name connect dirname func username ould false tor

<?php
function make_directory($ftp_stream, $dir){
  // if directory already exists or can be immediately created return true
  if (ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;
  // otherwise recursively try to make the directory
  if (!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; } } $path = ‘fptfiles‘;//ftp服務器下的目錄 $putFilePath = ‘D:\ftpfiles\attack.txt‘; //
本地上傳的文件路徑 $conn = ftp_connect("ftpIpAddress") or die("Could not connect"); ftp_login($conn,"username","pwd"); //利用ftp創建目錄 make_directory($conn,$path); //利用ftp選擇進入目錄 ftp_chdir($conn,$path); //開始上傳 上傳到了 ftp根目錄下 fptfiles/attack.txt if(ftp_put($conn, ‘attack.txt‘, $putFilePath , FTP_ASCII)){ echo ‘put success‘; } else{ echo ‘put fail‘; } $getFilePath = ‘D:\ftpfiles\attackget.txt‘;// 本地下載的文件路徑 //ftp 下載 if(ftp_get($conn, $getFilePath, ‘attack.txt‘, FTP_ASCII)){ echo ‘get success‘; } else{ echo ‘get fail‘; } ftp_close($conn); //註意上傳端的ftp權限設置

yii2 ftp 的常規操作 上傳 下載