繁体   English   中英

使用PHP脚本从URL下载文件

[英]Downloading File from a URL using PHP script

嗨,我想从一个URL中依次下载约250个文件。 我几乎完成了! 唯一的问题是我的URL的结构是: http ://lee.kias.re.kr/~newton/sann/out/201409/ /SEQUENCE1.prsa

其中id是序列中的文件,但文件名“ SEQUENCE1.psra”的格式为“ SEQUENCE?.psra”。 有什么办法可以在代码中指定这种文件格式? 文件夹中还有其他文件,但只有1个带有“ .psra”扩展名的文件。

Code:
<?php
// Source URL pattern
//$sourceURLOriginal = "http://www.somewebsite.com/document{x}.pdf";
 $sourceURLOriginal = " http://lee.kias.re.kr/~newton/sann/out/201409/{x}/**SEQUENCE?.prsa**";
// Destination folder
 $destinationFolder = "C:\\Users\\hp\\Downloads\\SOP\\ppi\\RSAdata";
// Destination file name pattern
 $destinationFileNameOriginal = "doc{x}.txt";
 // Start number
   $start = 7043;
// End number
$end = 7045;
 $n=1;
// From start to end
for ($i=$start; $i<=$end; $i++) {
// Replace source URL parameter with number

    $sourceURL = str_replace("{x}", $i, $sourceURLOriginal);

    // Destination file name
    $destinationFile = $destinationFolder . "\\" . 
    str_replace("{x}", $i, $destinationFileNameOriginal);
// Read from URL, write to file
    file_put_contents($destinationFile, 
    file_get_contents($sourceURL)
    );
// Output progress
echo "File #$i complete\n";
}
?>

如果我直接指定URL,它将起作用!

错误:警告:file_get_contents( http://lee.kias.re.kr/~newton/sann/out/201409/7043/SEQUENCE?.prsa ):无法打开流:C:\\ xampp \\ htdocs \\中的无效参数第37行的SOP \\ download.php文件#7043已完成

它使文件,但它们是空的!

如果有一种方法可以下载整个文件夹(依次以id命名)也可以工作! 但是,我们如何将整个文件夹下载到一个文件夹中?

服务器上的file_get_contents()函数可能无法正常工作。 试试这个代码:

    function url_get_contents ($Url) {
        if (!function_exists('curl_init')){ 
            die('CURL is not installed!');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $Url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

干得好。
我没有测试整个file_get_contentsfile_put_contents部分,但是如果您说它添加了文件(尽管为空白),那么我认为它在这里仍然可以使用...

其他一切正常。 我留了一个var_dump() ,以便您可以看到返回的样子。

我做了我在评论中建议的内容。 打开文件夹,解析文件列表,获取所需的文件名。
另外,我不知道您是否阅读了我的原始评论,但是$sourceURLOriginal开头有一个额外的空格,这可能会给您带来问题。

<?php

$start=7043;
$end=7045;

$sourceURLOriginal="http://lee.kias.re.kr/~newton/sann/out/201409/";
$destinationFolder='C:\Users\hp\Downloads\SOP\ppi\RSAdata';

for ($i=$start; $i<=$end; $i++) {
    $contents=file_get_contents($sourceURLOriginal.$i);
    preg_match_All("|href=[\"'](.*?)[\"']|",$contents,$hrefs);
    $file_list=array();
    if (empty($hrefs[1])) continue;
    unset($hrefs[1][0],$hrefs[1][1],$hrefs[1][2],$hrefs[1][3],$hrefs[1][4]);
    $file_list=array_values($hrefs[1]);
    var_dump($file_list);

    foreach ($file_list as $index=>$file) {
        if (strpos($file,'prsa')!==false) {
            $needed_file=$index;
            break;
        }
    }

    file_put_contents($destinationFolder.'\doc'.$i.'.txt',
        file_get_contents($sourceURLOriginal.$i.'/'.$file_list[$needed_file])
    );

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM