YAFEIML
V2EX  ›  PHP

php 子目录这两种写法区别在哪?

  •  
  •   YAFEIML · May 9, 2015 · 3363 views
    This topic created in 4027 days ago, the information mentioned may be changed or developed.
    不懂就要问
    $filename_full = 'cache'.$filename;
    $filename_full = './cache1'.$filename;
    $filename_full = './cache1/'.$filename;
    Supplement 1  ·  May 9, 2015
    那么问题来了,我是在找黑吗??怎么防止呢?判断下$_GET['url'];是不是好点

    <?php
    $target = $_GET['url'];
    $filename = basename($target);
    $tupian = './cache/'.$filename;
    $kzm = explode(".",$filename);
    $tupian_md5 = $tupian.'.txt';

    if (file_exists($tupian_md5) ) {

    if (count(file($tupian_md5)) == '1')
    {
    $md5file = md5_file($tupian);
    file_put_contents($tupian_md5,PHP_EOL.$md5file,FILE_APPEND);
    $content = file_get_contents($tupian);
    }

    else if (count(file($tupian_md5)) == '2' && substr_count(file_get_contents($tupian_md5),mb_substr(file_get_contents($tupian_md5),0,32)) == 2)

    {
    $content = file_get_contents($tupian);
    }

    else if (count(file($tupian_md5)) == '2' && substr_count(file_get_contents($tupian_md5),mb_substr(file_get_contents($tupian_md5),0,32)) != 2)

    {
    @unlink($tupian);
    @unlink($tupian_md5);
    $content = file_get_contents($target);
    }

    else
    {
    $content = file_get_contents($tupian);
    }
    }
    else {
    $md5file_t = md5_file($target);
    file_put_contents($tupian_md5,$md5file_t);
    //如果该文件并不存在,则读取其内容并保存在本地以供将来调用
    $content = file_get_contents($target);

    $fp = fopen($tupian, 'w+');
    fwrite($fp, $content);
    fclose($fp);
    }

    header('Content-Type: image/'.end($kzm).'');
    echo $content;
    ?>
    13 replies    2015-05-09 21:31:54 +08:00
    Livid
        1
    Livid  
    MOD
    PRO
       May 9, 2015
    1 和 2 是一样的,就是单纯的字符串连接。

    3 的话,是访问 cache 子目录下的文件。
    Septembers
        2
    Septembers  
       May 9, 2015
    这种写法存在安全问题
    see http://zone.wooyun.org/content/2196
    Septembers
        3
    Septembers  
       May 9, 2015
    这种写法存在潜在安全问题
    whatisnew
        4
    whatisnew  
       May 9, 2015
    没看懂。。。
    Tink
        5
    Tink  
    PRO
       May 9, 2015 via iPhone
    一二都是当前目录下的cacheXXXXXX,第三个是cache目录下的XXXXXXX
    bdbai
        6
    bdbai  
       May 9, 2015 via iPhone
    如果$filename有..然后你又做删除操作的话要小心了
    elvba
        7
    elvba  
       May 9, 2015 via Android
    楼主再看看这两个,能看出是什么意思么?
    $filename_full = '/cache1/'.$filename;
    $filename_full = '../cache1/'.$filename;
    YAFEIML
        8
    YAFEIML  
    OP
       May 9, 2015
    @elvba
    $filename_full = '/cache1/'.$filename; //根目录吧
    $filename_full = '../cache1/'.$filename;//上级目录吧
    randyzhao
        9
    randyzhao  
       May 9, 2015
    @Livid 哈哈 没准 $filename = "/test.php"; 呢
    lilydjwg
        10
    lilydjwg  
       May 9, 2015
    PHP 没有连接文件路径的函数么?
    YAFEIML
        11
    YAFEIML  
    OP
       May 9, 2015
    @Livid
    @Septembers
    @Tink
    @bdbai
    @elvba
    @randyzhao
    @lilydjwg
    那么新的问题来了。。
    loveyu
        12
    loveyu  
       May 9, 2015
    一般我做下realpath 然后再检测下
    eastphoton
        13
    eastphoton  
       May 9, 2015
    /filename 根目录
    ./filename 当前目录
    ../filename 表示上一级目录
    filename 当前目录
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1043 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 22:39 · PVG 06:39 · LAX 15:39 · JFK 18:39
    ♥ Do have faith in what you're doing.