2013-07-18 54 views
0

首先嚐試:下載大文件服務器使用PHP與任何方法

我已經使用這個代碼:(感謝user580950this answer

// define some variables 
$local_file = 'archive.tar'; 
$server_file = '/path/to/archive.tar'; 
$ftp_server = "1.2.3.4"; 
$ftp_user_name = "username"; 
$ftp_user_pass = "password"; 
$port_number = 123 

$conn_id = ftp_connect($ftp_server, $port_number); 

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// try to download $server_file and save to $local_file 
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) { 
    echo "Successfully written to $local_file\n"; 
} 
else { 
    echo "There was a problem\n"; 
} 
// close the connection 
ftp_close($conn_id); 

歸檔是一個很大的文件小於2GB

但幾秒後我看到There was a problem


第二個嘗試:

$url = 'http://site.com/archive.tar'; 

file_put_contents("archive.tar", fopen($url, 'r')); 

第三次嘗試:

$fh = fopen(basename($url), "wb"); 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_FILE, $fh); 
curl_exec($ch); 
curl_close($ch); 

第四次嘗試:五個一

$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the information 
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20 
curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_exec($ch); // get curl response 
curl_close($ch); 
fclose($fp); 

function download($file_source, $file_target) { 
    $rh = fopen($file_source, 'rb'); 
    $wh = fopen($file_target, 'w+b'); 
    if (!$rh || !$wh) { 
     return false; 
    } 

    while (!feof($rh)) { 
     if (fwrite($wh, fread($rh, 4096)) === FALSE) { 
      return false; 
     } 
     echo ' '; 
     flush(); 
    } 

    fclose($rh); 
    fclose($wh); 

    return true; 
} 


set_time_limit(0); 

var_dump(download($url, 'archive.tar')); // Returns bool(false) 

我要感謝很多人在SO使用自己的代碼,但仍沒有運氣。

+0

您是否檢查過您的'php.ini'文件以確保您的文件上傳已正確配置? – DevlshOne

+0

我生活在一個完美配置的'cpanel'共享主機(很多其他正常腳本運行時沒有任何問題)。我應該檢查什麼?我可以'phpinfo()'。 –

+1

查看本文中的建議。注 - 有一些'.htaccess'參數可以改變! https://drupal.org/node/97193 – DevlshOne

回答

相關問題