使用PHP fopen和/或cURL下载外部文件 [英] Download external file with PHP fopen and/or cURL

查看:198
本文介绍了使用PHP fopen和/或cURL下载外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用下载脚本来处理外部文件,该文件将下载但已损坏/不起作用。我认为是因为我无法使用filesize()函数获取外部文件的文件大小。

I can't get my download script to work with external files, the file will download but is corrupted/not working. I think it's because I can't get the filesize of the external file with filesize() function.

这是我的脚本:

function getMimeType($filename){
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $ext = strtolower($ext);

    $mime_types=array(
        "pdf" => "application/pdf",
        "txt" => "text/plain",
        "html" => "text/html",
        "htm" => "text/html",
        "exe" => "application/octet-stream",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "xls" => "application/vnd.ms-excel",
        "ppt" => "application/vnd.ms-powerpoint",
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg"=> "image/jpg",
        "jpg" =>  "image/jpg",
        "php" => "text/plain",
        "csv" => "text/csv",
        "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    );

    if(isset($mime_types[$ext])){
        return $mime_types[$ext];
    } else {
        return 'application/octet-stream';
    }
}

$path = "http://www.example.com/file.zip";

/* Does not work on external files
// check file is readable or not exists
if (!is_readable($path))
    die('File is not readable or does not exists!');
*/

$file_headers = @get_headers($path);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    echo "Files does not exist.";
} else {

$filename = pathinfo($path, PATHINFO_BASENAME);

// get mime type of file by extension
$mime_type = getMimeType($filename);

// set headers
header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Length: " . filesize($path));
header("Content-Type: $mime_type");
header("Content-Description: File Transfer");

// read file as chunk
if ( $fp = fopen($path, 'rb') ) {
    ob_end_clean();

    while( !feof($fp) and (connection_status()==0) ) {
        print(fread($fp, 8192));
        flush();
    }

    @fclose($fp);
    exit;
}

}

我相信可以做到cURL-但我的知识不足。
我想知道的东西:

I believe it can be done with cURL - but my knowledge is lacking. What I would like to know:


  • 如何检查文件是否存在以及如何获取文件

  • How do I check if the file exist and how do I get the filesize with cURL?

仅使用cURL并忘记fopen会更好吗?

Would it be better just to use cURL and forget about fopen?

标头设置正确吗?

任何建议都值得赞赏!

推荐答案

您也可以尝试此过程,我假设您的源URL为 $ sourceUrl 且目标为/保存文件的路径是 $ destinationPath

You can try this process as well, I am assuming that your source url is $sourceUrl and destination/ path to save file is $destinationPath

$destFilename = 'my_file_name.ext';
$destinationPath = 'your/destination/path/'.$destFilename;

if(ini_get('allow_url_fopen')) {                                
    if( ! @file_put_contents($destinationPath, file_get_contents($sourceUrl))){
        $http_status = $http_response_header[0];
        sprintf('%s encountered while attempting to download %s',$http_status, $sourceUrl );
        break;
    }
} elseif(function_exists('curl_init')) {
    $ch = curl_init($sourceUrl);
    $fp = fopen($destinationPath, "wb");

    $options = array(
        CURLOPT_FILE => $fp,
        CURLOPT_HEADER => 0,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_TIMEOUT => 120); // in seconds

    curl_setopt_array($ch, $options);
    curl_exec($ch);
    $http_status = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
    curl_close($ch);
    fclose($fp);

    //delete the file if the download was unsuccessful
    if($http_status != 200) {
        unlink($destinationPath);
        sprintf('HTTP status %s encountered while attempting to download %s', $http_status, $sourceUrl );

    }
} else {    
    sprintf('Looks like %s is off and %s is not enabled. No images were imported.', '<code>allow_url_fopen</code>', '<code>cURL</code>'  );
    break;
}

您可以使用 curl_getinfo($ ch,CURLINFO_CONTENT_TYPE) ; 以防卷曲,以获取文件信息并根据您的要求使用它。

You can use curl_getinfo($ch, CURLINFO_CONTENT_TYPE); in case of curl to get the file info and use it as per your requirement.

这篇关于使用PHP fopen和/或cURL下载外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆