dongqing904999 2011-06-14 16:10
浏览 38
已采纳

使用PHP和cURL下载文件

I used this function for downloading SWF files(flash games) sucessfully. When I use this script for one particular site it downloads all games(I told the script to download 4 games from a list) with exact size of 299bytes? I tried downloading these games with Google Chrome and the download is sucessfull. Is there something missing in the CURL functions I use or the download algorithm is not good enough? Any help will be greatly appreciated.

function saveFlash($fullPaths,$folder,$gamenames,$i){
        $curl = curl_init($fullPaths[$i]);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //Create a new file in the given folder
        $fp = fopen($folder."/".$gamenames[$i].".swf", 'w');
            if ($fp == FALSE){ 
                echo "File not opened<br>";} 
        //Ask cURL to write the contents to a file
        curl_setopt($curl, CURLOPT_FILE, $fp);
        //Execute the cURL session
        curl_exec ($curl);
        //Close cURL session and file
        curl_close ($curl);
        fclose($fp);
    }

Text editor gives the following

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.freeonlinegames.com/">here</a>.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at freeonlinegames.com Port 80</address>
</body></html>
  • 写回答

2条回答 默认 最新

  • duanjie1339 2011-06-14 16:22
    关注

    You'll want to set CURLOPT_FOLLOWLOCATION to allow it to follow the redirects.

    You may also want to set a CURLOPT_MAXREDIRS so it doesn't redirect out of control.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法