【问题标题】:How to access sub domain folder from main domain in PHP如何在 PHP 中从主域访问子域文件夹
【发布时间】:2014-08-30 14:06:15
【问题描述】:

我有一个站点 - mysite.com 和一个子域 admin.mysite.com 。我想从主站点访问子域文件夹。子域文件夹为admin,与主域处于同一目录级别。

目录组织为:

root/admin - sub domain folder
root/httpd - main domain folder

我要访问的内容:

root/admin/images/

发件人:

root/httpd/index.php

PHP 脚本必须扫描上述目录中的图像,然后在主域文档中的<img> 标记中扫描echo 它们的dir/http 路径。

为什么我需要访问它 - 因为图像是由子域中的用户上传的。 :|

【问题讨论】:

  • 为什么不直接从主域连接到 admin.example.com?如果这不是您想要的,.. 将移动一个目录级别,因此如果您想访问 admin 文件夹,请使用 ../admin 等。从您给出的示例中,图像将位于 ../admin /images/imagename.png
  • httpd 不仅仅是一个文件夹。这是一个域文件夹。你不能 dp '../admin' 进入 admin 文件夹。浏览器将始终将其解释为 mysite.com/admin/images。
  • 其次,图像文件夹中的图像数量会发生变化,因此我需要使用 php 脚本动态加载它们,而不是将每个图像引用为 admin.mysite.com/image/i.jpg ...等等。希望你能理解。

标签: php apache


【解决方案1】:

所以我找到了解决方案。

使用opendir() 函数我能够扫描子域目录中的图像。

$handle = opendir('../admin/images/');

   while($file = readdir($handle)){

        if($file !== '.' && $file !== '..'){

            echo "<img src='http://admin.mysite.com/images"."/"."$file' />";

        }
     }

【讨论】:

    【解决方案2】:
    define("IMG_DIRECTORY", "you subdomain upload folder");
    
    $filename = $_GET['f'];
    $filepath = IMG_DIRECTORY . $filename;
    
    //if file does not exists
    if( strlen($filename) == 0 || !is_file($filepath) ){
        header('HTTP/1.0 404 Not Found');
        die("HTTP/1.0 404 Not Found");
    }
    
    $path_parts = pathinfo( $filename );
    $file_ext   = $path_parts['extension'];
    
    header("Content-type:image/" . strtolower($file_ext));
    header("Content-Disposition: inline; filename=".$filename);
    
    //For IE Save as filename bug
    if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")){
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Cache-Control: private",false); 
        header("Pragma: public");
        header("Expires: 0"); 
        header('Content-Transfer-Encoding: Binary');
    }
    
    @readfile(str_replace("@","",$filepath));
    

    你可以建立一个 api 页面来做到这一点

    http://example.com/imageapi.php?f=sample.png

    【讨论】:

    • 感谢您的建议。但问题是我也需要扫描目录以获取图像文件名。所以我想核心问题是如何使用 PHP 访问/扫描这个文件夹:/.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多