LOKESH
LOKESH

Reputation: 1301

How to download file via command line in php?

I want to paste one script in all my client machine which call php file which is on my server.

Let say my server path is www.google.com/support/lokesh.php

So that I want to put one file to all my client machine at location where it call php file(for example if it call from /home/lalu/myscript.sh) then my php code will put one file(additional.sh) to /home/lalu/additional.sh

below is my code to download file

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;

I want to paste one file location at client machine from where it call server file.

Upvotes: 2

Views: 6410

Answers (3)

NVRM
NVRM

Reputation: 13162

One attempt, with a progress bar.

#!/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);
}

enter image description here

The file is build directly into the current folder, not in the temp directory. This mean the file can be read while downloading. If the file type is made to handle that, like most media format do.

To use, pass an url as first argument in the command line.

./pget  https://site.download.mp4

You know you want to tweek it ;)

Upvotes: 6

Sahadev
Sahadev

Reputation: 1493

Try this code. In your download file :

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");

Upvotes: -1

jekaby
jekaby

Reputation: 403

In my opinion, the easiest way is:

$fileContent = file_get_contents('/home/lokesh/lalu.txt');

If I correctly understood.

Upvotes: 0

Related Questions