如何在php中通过命令行下载文件?

LOK*_*ESH 2 php file

我想在我的所有客户端计算机中粘贴一个脚本,该脚本调用我的服务器上的 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;
Run Code Online (Sandbox Code Playgroud)

我想从调用服务器文件的地方粘贴一个文件位置到客户端计算机。

NVR*_*VRM 5

一次尝试,带有进度条。

\n\n
#!/usr/bin/php\n<?php\nif (@$argv[1] != null){\n  echo "Retrieving http header..."; \n  $header = get_headers("$argv[1]");\n  $pp = "0";\n  echo json_encode($header, JSON_PRETTY_PRINT);\n  $key = key(preg_grep(\'/\\bLength\\b/i\', $header));\n  $type = key(preg_grep(\'/\\bType\\b/i\', $header));\n  $http = substr($header[0], 9, 3);\n  $tbytes = @explode(" ",$header[$key])[1];\n  $type = @explode("/",explode(" ",$header[$type])[1])[1];\n  echo " Target size: ".floor((($tbytes / 1000)/1000))." Mo || ".floor(($tbytes/1000))." Kb";\n  $t = explode("/",$argv[1]);\n  $remote = fopen($argv[1], \'r\');\n  $nm = $t[count($t)-1].".$type";\n  $local = fopen($nm, \'w\');\n  $read_bytes = 0;  \n  echo PHP_EOL;\n  while(!feof($remote)) {\n    $buffer = fread($remote, intval($tbytes));\n    fwrite($local, $buffer);\n    $read_bytes += 2048;\n    $progress = min(100, 100 * $read_bytes / $tbytes);\n    $progress = substr($progress,0 , 6) *4;\n    $shell = 10; /* Progress bar width */ \n    $rt = $shell * $progress / 100;\n    echo " \\033[35;2m\\e[0m Downloading: [".round($progress,3)."%] ".floor((($read_bytes/1000)*4))."Kb ";\n    if ($pp === $shell){$pp=0;};\n    if ($rt === $shell){$rt=0;};\n    echo str_repeat("\xe2\x96\x88",$rt).str_repeat("=",($pp++)).">@\\r";\n    usleep(1000);\n  }\n  echo " \\033[35;2m\\e[0mDone [100%]  ".floor((($tbytes / 1000)/1000))." Mo || ".floor(($tbytes/1000))." Kb   \\r";\n  echo PHP_EOL;\n  fclose($remote);\n  fclose($local);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n\n

该文件直接构建到当前文件夹中,而不是临时目录中。\n这意味着可以在下载时读取该文件。\n如果文件类型能够处理这种情况,就像大多数媒体格式一样。

\n\n

要使用它,请将 url 作为命令行中的第一个参数传递。

\n\n
./pget  https://site.download.mp4\n
Run Code Online (Sandbox Code Playgroud)\n\n

你知道你想调整它;)

\n