开发者

how to download a file in a site from command line writing it in php

开发者 https://www.devze.com 2023-03-21 01:44 出处:网络
when I use wget for dowloading it is successful but it will not work with exec_shell() or system then I tried to use curl

when I use wget for dowloading it is successful but it will not work with exec_shell() or system then I tried to use curl here is the code for downloading a site that Joe in another question told me from this site

function curl_download($Url){

// is cURL installed yet?
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

// OK cool - then let's create a new cURL resource handle
$ch = curl_init();

// Now set some options (most are optional)

// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);

// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);

/开发者_运维知识库/ Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// Download the given URL, and return output
$output = curl_exec($ch);

// Close the cURL resource, and free system resources
curl_close($ch);

return $output;
}

it is working for downloading a page but I want to change it to download from command line if you have better suggestions please tell me

for example I used this:

<?php
  $output=exec_shell('wget ftp://ftp.gnu.org/pub/gnu/wget/wget-latest.tar.gz');
?>

it is not working when I echoed it

but when I enter this in command line it will download it on /root:

wget ftp://ftp.gnu.org/pub/gnu/wget/wget-latest.tar.gz


You can pass any parameters to your script from command line interface. But you have to enable such functionality in php if not (just try php -i in command line).

To use parameters in php script you should use $argv array. For example:

php yourscript.php http://myurl.com

$url = $argv[1]; //it will contain first parameter passed to your script
curl_download($url);

Check out official manual at php.net

0

精彩评论

暂无评论...
验证码 换一张
取 消