我现在已用php编写了一段代码,主要是下面这条语句,它虽可以访问外网的文件,但这样很不安全,肯定是不行,不知谁有什么好的办法吗? 在线等!!!!
$remotefile = @fopen ("http://100.80.1.1/mysqlbackup/20090904.sql", "rb"); 
if (!$remotefile) { 
        echo " <p>Unable to open remote file.\n"; 
        exit; 

$line=""; 
while (!feof ($remotefile)) { 
        $line .=@fgets ($remotefile, 1024); } fclose($remotefile); $tempfile="/var/www/html/mysqlbackup/20090908.sql"; 
$localfile=@fopen($tempfile,"wb"); 
@fwrite($localfile,$line); 
fclose($localfile); 

解决方案 »

  1.   

    用php来读取要下载的文件,权限不够的就调到错误页面,有权限的才返回文件流
      

  2.   

    因为我在外网中备份了数据库,数据库文件肯定是不能让外人下载呀楼上的,那如何设置它的权限呢???我现在把程序改了一下,怎么不对呢?down.php
    <?php
    /*
     * Created on 2009-9-8
     *
     * To change the template for this generated file go to
     * Window - Preferences - PHPeclipse - PHP - Code Templates
     */
    $ipallow = array('192.168.166.1');
    if(!in_array($_SERVER['REMOTE_ADDR'],$ipallow)) 
    die();$filename = "test.txt";header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=".basename($filename));
    readfile($filename);?>
    upload.php
    <?php$remotefile = fopen ("http://192.168.166.2/mysqlbackup/down.php", "rb");
    if (!$remotefile) {
    echo "<p>Unable to open remote file.\n";
    exit;
    }
    while (!feof ($remotefile)) {
    $line .= fgets ($remotefile, 1024);}fclose($remotefile);$tempfile="postcodetest.sql";
    //echo $tempfile;
    $localfile = fopen($tempfile,"wb");
    fwrite($localfile,$line);
    fclose($localfile);?>
      

  3.   

    已解决服务器server.php
    <?php
    if (isset ($_POST)) {
    $login_name = $_POST[username];
    $login_password = $_POST[password];
    $login_ip = $_POST[ip];
    //如果为了安全,用户名、密码及IP可以从数据库中存储
    if ($login_name=="liu" and $login_password=="123" and $login_ip=="192.16.16.22"){
    echo "right";
    $filename = "/bak1/test.txt";
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=".basename($filename));
    $line=readfile($filename);
    }else{
    die("wrong");
    }  
    }
    exit;
    ?>
    客户端client.php
    <?php
    $url='http://14.8.19.3/mysqlbackup/server.php';
    $fields = "username=liu&password=123&ip=19.16.16.25"; 
    $fp=fopen("test.txt",wb);
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields ); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $str=curl_exec($ch);
    fwrite($fp,$str);
    fclose($fp);
    curl_close($ch);  
    ?>