Reference URL. 特別注意 ftp_put上傳兩個變數別弄錯, 說明如下:
$remote_file
The remote file path. 是目的地端, 也是FTP端的目錄Path.
$local_file
The local file path. 是程式執行端的本地端,來源檔.
mode
The transfer mode. Must be either FTP_ASCII or FTP_BINARY.
程式範例-Start
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
程式範例-End
Solution2:
/*
$ftp_server = "ftp.*****.com";
$ftp_user = "******";
$ftp_password = "*****";
*/
$ftp_server = "ftp.gnu.org";
$ftp_user = "anonymous";
$ftp_password = "none";
/* connect */
$ftp_connection = @ftp_connect($ftp_server);
if (!$ftp_connection) die('could not connect.');
/* login */
$ftp_login = @ftp_login($ftp_connection, $ftp_user, $ftp_password);
if (!$ftp_login) die('could not login.');
/* enter passive mode */
$ftp_passive = @ftp_pasv($ftp_connection, true);
if (!$ftp_passive) die('could not enable passive mode.');
/* get listing */
$ftp_listing = ftp_nlist($ftp_connection, ".");
foreach ($ftp_listing as $file){
echo "
".$file."
";}
ftp_close($ftp_connection);
Solution3:
function getFtpConnection($uri)
{
// Split FTP URI into:
// $match[0] = ftp://username:password@sld.domain.tld/path1/path2/
// $match[1] = username
// $match[2] = password
// $match[3] = sld.domain.tld
// $match[4] = /path1/path2/
preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $uri, $match);
// Set up a connection
$conn = ftp_connect($match[3]);
// Login
if (ftp_login($conn, $match[1], $match[2]))
{
// Change the dir
ftp_chdir($conn, $match[4]);
// Return the resource
return $conn;
}
// Or retun null
return null;
}
Solution4:
$Ftp_host='000.000.000.000';
$Ftp_port=21;
$Ftp_user='xxxxxx';
$Ftp_pass='aaaaaa';
$Ftp_dir='';
$ftp_link=ftp_connect($Ftp_host,$Ftp_port) or die("No Link");
$login = ftp_login($ftp_link ,$Ftp_user,$Ftp_pass);
ftp_pasv($ftp_link,true);
$alist = ftp_nlist ($ftp_link, "out"); //list out/ 內的所有檔案
while(list($l,$r)=each($alist)){
echo $l.','.$r; //$l=資料序0,1,2... , $r=檔名 out/aaa.xxx
}
@ftp_put($ftp_link, $putFile, $file_Name, FTP_BINARY); //上傳 $putFile=目地, $file_Name=來源
@ftp_get($ftp_link, $File_Name, $r, FTP_BINARY) //取回 $File_Name=目地, $r=來源
ftp_delete ($ftp_link,$r); //刪除檔案
Solution5:
// Download a file and store the data in $data
$data = file_get_contents('ftp://username:password@ftp.domain.tld/somefile.txt');
// Loop the contents of the root directory
$dp = opendir('ftp://username:password@ftp.domain.tld/');
while ($file = readdir($dp)) {
// Do stuff
}
closedir($dp);
// Upload a file
file_put_contents('ftp://username:password@ftp.domain.tld/somefile.txt','This is the file data');
// Upload a file from the local file system
$local = fopen('/path/to/my/file.ext','r');
$remote = fopen('ftp://username:password@ftp.domain.tld/somefile.txt','w');
stream_copy_to_stream($local,$remote);
Other Note: Ref URL.
#1 如需要PHP Create Folder 可以參考如下:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
#2 FTP Create Folder. Ref URL.// try to create the directory $dirif (ftp_mkdir($conn_id, $dir)) {
echo "successfully created $dir\n";
} else {
echo "There was a problem while creating $dir\n";
}
#3,// 確認目前位置
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
Reference:
1. 介紹 ftp-connect
http://php.net/manual/en/function.ftp-connect.php
2. 介紹ftp-login
http://php.net/manual/en/function.ftp-login.php
3. 介紹 PHP ftp_nlist 函式
http://www.w3school.com.cn/php/func_ftp_nlist.asp
4. Nice solution example. (Same the solution1 )
http://stackoverflow.com/questions/8302460/ftp-login-not-authorizing-through-php-methods
5. PHP Create Folder
http://stackoverflow.com/questions/2303372/create-a-folder-if-it-doesnt-already-exist
6. How To Copy Files Around FTP Using PHP
http://stackoverflow.com/questions/4853269/how-to-copy-files-around-ftp-using-php
沒有留言:
張貼留言