[英]How to access sub domain folder from main domain in PHP
我有一个网站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路径。
我为什么需要访问它-因为图像在子域中被用户上传。 :|
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页面来做到这一点
因此,我找到了解决方案。
使用opendir()
函数,我可以扫描子域目录中的图像。
$handle = opendir('../admin/images/');
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
echo "<img src='http://admin.mysite.com/images"."/"."$file' />";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.