Php 获取同一服务器中另一个域中某个域的文件内容

Php 获取同一服务器中另一个域中某个域的文件内容,php,image,multiple-domains,Php,Image,Multiple Domains,我在同一台服务器上有两个域。 www.domain1.com和www.domain2.com 在www.domain1.com中,有一个名为“图片”的文件夹。用户可以通过自己的ID创建一个文件夹,将自己的图片上传到该文件夹。(www.domain1.com/pictures/user\u ID) 同时使用上载的图像创建缩略图,并保存到动态创建的路径中。(www.domain1.com/Pictures/User\u iD/thumbs) 这是在我们的系统中使用PHP脚本实现的 所以我的问题是,我

我在同一台服务器上有两个域。 www.domain1.com和www.domain2.com

在www.domain1.com中,有一个名为“图片”的文件夹。用户可以通过自己的ID创建一个文件夹,将自己的图片上传到该文件夹。(www.domain1.com/pictures/user\u ID) 同时使用上载的图像创建缩略图,并保存到动态创建的路径中。(www.domain1.com/Pictures/User\u iD/thumbs)

这是在我们的系统中使用PHP脚本实现的

所以我的问题是,我需要在www.domain2.com上显示用户上传的图片。 我使用了下面的代码来实现这一点,但它不起作用

$image_path="http://www.domain1.com/Pictures/"."$user_id";


$thumb_path="http://www.domain1.com/Pictures/"."$user_id/"."thumbs";

$images = glob($image_path.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
得到这样的图像

               foreach ($images as $image) {
      // Construct path to thumbnail
        $thumbnail = $thumb_path .'/'. basename($image);

     // Check if thumbnail exists
    if (!file_exists($thumbnail)) {
    continue; // skip this image
    }
但是当我尝试这样做时,图像不会显示在www.domain2.com/user.php上。 如果我使用相同的代码来显示相同域中的图像,则图像看起来很好

希望我能正确地解释情况。 请帮忙


提前感谢

Glob需要文件访问权限。但是因为它在另一个域上。它不能访问文件(而且不应该)。即使它们在同一台服务器上,由于许多原因,它们也不能访问彼此的文件

您可以在domain1.com上编写一个小API,返回特定用户的图像列表。 然后使用for isntance curl访问该信息

在存储图片的domain1.com上:

<?php
//get the user id from the request
$user_id = $_GET['user_id'];

$pathToImageFolder = 'path_to_pictures' . $user_id ;

$images = glob($pathToImageFolder.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
//return a JSON array of images
print json_encode($images,true); #the true forces it to be an array

用户对这两个域都有读取权限吗?通常情况下,两个域不能访问彼此的文件。或者将文件放在bothi可访问的子目录中,bothi将检查读取权限。我们还有什么可以做的吗?你能帮我吗?因为我是php新手,对实例库不太了解,所以我用一个小例子对我的答案做了一个小编辑。更多信息请点击此处:
<?php
//retrieve the pictures
$picturesJSON = file_get_contents('http://www.domain1.com/api/images.php?user_id=1');
//because our little API returns JSON data, we have to decode it first
$pictures = json_decode($picturesJSON);
// $pictures is now an array of pictures for the given 'user_id'