2011-09-14 16 views
6

मैं php का उपयोग कर एक sftp सर्वर से फ़ाइल डाउनलोड करने का प्रयास कर रहा हूं लेकिन मुझे फ़ाइल डाउनलोड करने के लिए कोई सही दस्तावेज़ नहीं मिल रहा है।PHP का उपयोग कर एसएफटीपी से फ़ाइल कैसे डाउनलोड करें?

<?php 
$strServer = "pass.com"; 
$strServerPort = "22"; 
$strServerUsername = "admin"; 
$strServerPassword = "password"; 
$resConnection = ssh2_connect($strServer, $strServerPort); 
if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)) { 
    $resSFTP = ssh2_sftp($resConnection); 
    echo "success"; 
} 
?> 

एक बार मेरे पास एसएफटीपी कनेक्शन खुला है, तो मुझे फ़ाइल डाउनलोड करने के लिए क्या करने की ज़रूरत है?

+1

तो सवाल यह है:

यहाँ एक उदाहरण है कि एक निर्देशिका स्कैन और रूट फ़ोल्डर में सभी फ़ाइलों को डाउनलोड करेगा? –

+0

@ बासज़ शीर्षक पढ़ें। –

+1

@OZ_: मुझे पता है ... मैंने इसे इस तरह संपादित किया। –

उत्तर

5

phpseclib, a pure PHP SFTP implementation का उपयोग करना:

<?php 
include('Net/SFTP.php'); 

$sftp = new Net_SFTP('www.domain.tld'); 
if (!$sftp->login('username', 'password')) { 
    exit('Login Failed'); 
} 

// outputs the contents of filename.remote to the screen 
echo $sftp->get('filename.remote'); 
?> 
3

बार जब आप अपने SFTP कनेक्शन खुला है, तो आप फ़ाइलों को पढ़ने के लिए और इस तरह fopen, fread, और fwrite के रूप में मानक PHP कार्यों का उपयोग कर लिख सकते हैं। आपको अपनी दूरस्थ फ़ाइल खोलने के लिए केवल ssh2.sftp:// संसाधन हैंडलर का उपयोग करने की आवश्यकता है।

// Assuming the SSH connection is already established: 
$resSFTP = ssh2_sftp($resConnection); 
$dirhandle = opendir("ssh2.sftp://$resSFTP/"); 
while ($entry = readdir($dirhandle)){ 
    $remotehandle = fopen("ssh2.sftp://$resSFTP/$entry", 'r'); 
    $localhandle = fopen("/tmp/$entry", 'w'); 
    while($chunk = fread($remotehandle, 8192)) { 
     fwrite($localhandle, $chunk); 
    } 
    fclose($remotehandle); 
    fclose($localhandle); 
} 
+0

PHP5.6 से शुरू हो रहा है, यह काम नहीं करेगा और चुपचाप असफल हो जाएगा:

 $remotehandle = fopen("ssh2.sftp://$resSFTP/$entry", 'r'); 
$ resSFTP को स्पष्ट रूप से int में परिवर्तित किया जाना चाहिए:
 $remotehandle = fopen('ssh2.sftp://' . intval($resSFTP) . '/$entry', 'r'); 

संबंधित मुद्दे