例如现在要通过a.php文件下载http://www.baidu.com/img/baidu_jgylogo3.gif 这个图片,或者zip文件也行。
然后点击a.php文件时,能弹出另存为窗口,重命名这个文件。。代码得如何写啊??整了好久都不行~~~

解决方案 »

  1.   

    <?php
    $path = 'http://www.baidu.com/img/baidu_jgylogo3.gif';
    $file_name="xx.gif"; //这里重命名
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename='.$file_name); 
    ob_end_clean();
    readfile($path);
    ?>header('Content-Disposition: attachment; filename=xx.gif'); 
    这个头可以激活对话框一个文件介绍php做下载
      

  2.   

    <?php
    $path = 'http://www.baidu.com/img/baidu_jgylogxo3.gif';
    if(!file_exists($path)){
    echo "该文件不存在!!!";
    exit();
    }
    $file_name="xx.gif"; //这里重命名
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename='.$file_name); 
    ob_end_clean();
    readfile($path);
    ?>
      

  3.   

    远程文件判断比较难,一般判断head返回状态,200就表示正常,404就是不存在
    但遇到转向甚至多重转向的情况就相当麻烦了返回状态可以用socket或者curl_getinfo($ch, CURLINFO_HTTP_CODE),或者相关获取连线响应头的方法
    最后,其实这个也没办法判断的,如果对方有做针对非合法网址自动处理时,并不一定会返回404
      

  4.   

    file_exists 不能用于远程文件。
    试试用curl。function urlExist($url)
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $data = curl_exec($ch);
    curl_close($ch);
    if (!$data) {
      return "Domain could not be found";
    }
    else {
      preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
      $code = end($matches[1]);
      if ($code == 200) {
    return "Page Found";
      }
      elseif ($code == 404) {
    return "Page Not Found";
      }
    }
    }
    echo urlExist('http://www.baidu.com/img/baidu_jgylogxo3.gif');
      

  5.   


    那请问判断head返回状态如何写?