如何从命令行下载网站中的文件,用php编写


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

当我使用wget下载它是成功的,但它不会与exec_shell()工作或system然后我试着用卷发这是Joe在另一个问题中告诉我的下载网站的代码

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;
}

它正在下载一个页面,但我想改变它从命令行下载如果你有更好的建议请告诉我

例如:

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

当我回显它时它不工作

但是当我在命令行中输入这个时,它会下载到/root:

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

您可以从命令行界面向脚本传递任何参数。但是你必须在php中启用这样的功能,如果没有(只是尝试在命令行php -i)。

要在php脚本中使用参数,您应该使用$argv数组。例如:

php yourscript.php http://myurl.com

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

查看官方手册:php.net