2016-10-24 47 views
0

私はZIPファイルを外部サーバーから自分のサーバーにダウンロードしようとしています。 PHPスクリプトは次のとおりです:PHP:cURL ZIPファイルをダウンロード - 小さいファイルサイズ

<?php  
... 
# Some URL 
$URL = 'https://xyz.source.com/Path/where/ZIP-Files/file.zip'; 
# Destination 
$destination = fopen("../path/to/file.zip","w+"); 

# Check, if cURL is installed: 
if (!function_exists('curl_init')){ 
    die('cURL it not installed!'); 
} 

# New cURL Session 
$ch = curl_init(); 

# Set Options 
# Set Download-URL 
curl_setopt($ch, CURLOPT_URL, $URL); 
# unknown about Header 
# curl_setopt($ch, CURLOPT_HEADER, 0); 

# Set to "false", but I don't know why due to lack of explanation. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); 

# Set to "true", but I don't know why, due to lack of explanation. 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 

curl_setopt($ch, CURLOPT_FILE, $ziel); 

# curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
# curl_setopt($ch, CURLOPT_TIMEOUT, 1000); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); 

# Execution 
curl_exec($ch); 

# Check for errors 
if(curl_exec($ch) === false) { 
    echo 'Curl-Error: ' . curl_error($ch); 
} else { 
    echo 'cURL - no problems'; 
} 

# end of cURL-Session 
curl_close($ch); 


fclose($destination); 
... 
?> 

どうしましたか? - ファイルはありますが、サイズは小さいです。

目的地サーバーでは、予想よりも小さいサイズのファイルを取得します。小さなZIPファイルを開くことはできません。あなたは本当にそれが必要以上に、これはより複雑に作っている

+0

なぜ機能するのですか?あなたは '$ destination'をオープンしますが、未定義の変数である' $ ziel'でカールを指しています。 –

答えて

1

<?php  
... 
# Some URL 
$URL = 'https://xyz.source.com/Path/where/ZIP-Files/file.zip'; 
# Destination 
$destination = "../path/to/file.zip"; 

$filedata = file_get_contents($URL); 
file_put_contents($destination, $filedata); 
?> 

file_get_contents()file_put_contents()の使用方法のドキュメントを参照してください。

+0

ありがとうございます。ダウンロード作品!また、ドキュメンテーションへの役に立つリンクにも感謝します。 – Peter

+0

"file_get_contents($ URL)"でちょっと試してみたところ、このコマンドは完全なファイルをダウンロードするのではなく、その一部だけをダウンロードすることを確認しました。ダウンロードするZIPファイルは約1.5 MBですが、このコマンドでは1121バイト(!)しかダウンロードされません。ダウンロードしたZIPファイルは無用です。 代替手段はありますか? – Peter

+0

ダウンロードしているサーバーに問題があります。 – miken32

関連する問題