[英]How to download file via command line in php?
我想在所有客戶端計算機上粘貼一個腳本,該腳本將調用服務器上的php文件。
假設我的服務器路徑是www.google.com/support/lokesh.php
因此,我想將一個文件放到我所有的客戶端計算機上的php文件所在的位置(例如,如果從/home/lalu/myscript.sh調用),那么我的php代碼會將一個文件(additional.sh)放到/home/lalu/additional.sh
下面是我下載文件的代碼
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('google.com/support/lokesh.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('/home/lokesh/lalu.txt'));
readfile('/home/lokesh/lalu.txt');
//for sending mail fif only one user is available
exit;
我想將一個文件位置從調用服務器文件的位置粘貼到客戶端計算機上。
一種嘗試,帶有進度條。
#!/usr/bin/php
<?php
if (@$argv[1] != null){
echo "Retrieving http header...";
$header = get_headers("$argv[1]");
$pp = "0";
echo json_encode($header, JSON_PRETTY_PRINT);
$key = key(preg_grep('/\bLength\b/i', $header));
$type = key(preg_grep('/\bType\b/i', $header));
$http = substr($header[0], 9, 3);
$tbytes = @explode(" ",$header[$key])[1];
$type = @explode("/",explode(" ",$header[$type])[1])[1];
echo " Target size: ".floor((($tbytes / 1000)/1000))." Mo || ".floor(($tbytes/1000))." Kb";
$t = explode("/",$argv[1]);
$remote = fopen($argv[1], 'r');
$nm = $t[count($t)-1].".$type";
$local = fopen($nm, 'w');
$read_bytes = 0;
echo PHP_EOL;
while(!feof($remote)) {
$buffer = fread($remote, intval($tbytes));
fwrite($local, $buffer);
$read_bytes += 2048;
$progress = min(100, 100 * $read_bytes / $tbytes);
$progress = substr($progress,0 , 6) *4;
$shell = 10; /* Progress bar width */
$rt = $shell * $progress / 100;
echo " \033[35;2m\e[0m Downloading: [".round($progress,3)."%] ".floor((($read_bytes/1000)*4))."Kb ";
if ($pp === $shell){$pp=0;};
if ($rt === $shell){$rt=0;};
echo str_repeat("█",$rt).str_repeat("=",($pp++)).">@\r";
usleep(1000);
}
echo " \033[35;2m\e[0mDone [100%] ".floor((($tbytes / 1000)/1000))." Mo || ".floor(($tbytes/1000))." Kb \r";
echo PHP_EOL;
fclose($remote);
fclose($local);
}
該文件直接構建在當前文件夾中,而不是在temp目錄中。 這意味着可以在下載時讀取文件。 如果像大多數媒體格式一樣處理文件類型。
若要使用,請在命令行中將url作為第一個參數傳遞。
./pget https://site.download.mp4
你知道你想周;)
我認為,最簡單的方法是:
$fileContent = file_get_contents('/home/lokesh/lalu.txt');
如果我正確理解的話。
試試這個代碼。 在您的下載文件中:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/".$extension); // you can put here MIME type of your file
header("Content-Disposition: attachment; filename=\"" . basename($filePath) . "\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filePath));
set_time_limit(0);
readfile("$filePath");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.