hide a folder path when user downloads a file

前端 未结 1 1285
小蘑菇
小蘑菇 2020-12-20 05:18

I am using wordpress for my website and there are some wallpapers in my folder which i provide for download . But i dont want users to know the exact file location of folder

相关标签:
1条回答
  • 2020-12-20 05:28

    Example With PDF doc

    $nameOld = "/public_html/wp-content/example.folder/oldnme.pdf";
    $nameNew = "newName.pdf" ;
    header("Content-Transfer-Encoding: binary");
    header('Content-type: application/pdf');
    header("Content-disposition: attachment; filename=$nameNew"); //
    readfile($nameOld);
    



    Edit Prove of Concept for your image system using download.php?img=flower without the extension and flower show be the image name

    $directory = "/public_html/wp-content/example.folder/";
    $types = array("jpg","gif","png");
    $ext = null;
    
    if (! isset($_GET['img'])) {
        die("Invalid URL");
    }
    
    $nameOld = filter_var($_GET['img'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW);
    $nameNew = uniqid(basename($nameOld));
    
    // File the file
    foreach ( $types as $type ) {
        if (is_file($nameOld . "." . $type)) {
            $ext = $type;
            break;
        }
    }
    
    if ($ext == null) {
        die("Sorry Image Not Found");
    }
    $nameOld .= "." . $ext;
    $type = image_type_to_mime_type(exif_imagetype($nameOld));
    
    header("Content-Transfer-Encoding: binary");
    header('Content-type: ' . $type);
    header("Content-disposition: attachment; filename=$nameNew"); //
    readfile($nameOld);
    
    0 讨论(0)
提交回复
热议问题