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

问题描述:

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

I have two domains in same server. www.domain1.com & www.domain2.com.

在www.domain1.com上,有一个名为图片的文件夹。对于该文件夹,用户可以通过ID创建文件夹来上传他们的图片。 (www.domain1.com/Pictures/User_iD)
同时使用上传的图像创建缩略图,并将其保存到动态创建的路径中。(www.domain1.com/Pictures/User_iD/thumbs)

in www.domain1.com, there is a folder called 'Pictures'. To that folder user can upload their pictures by creating a folder by their ID. (www.domain1.com/Pictures/User_iD) A thumbnail is created using uploaded image at the same time and being saved to this path which is created dynamically.(www.domain1.com/Pictures/User_iD/thumbs)

这是在我们的系统中使用PHP脚本发生的。

This is happening using PHP script in our system.

所以我的问题是,我需要显示那些用户在www.domain2.com上传了图片。
i使用以下代码来执行此操作,但它无效。

So my problem is, i need to display those User uploaded images in www.domain2.com. i have used following code to do that, but it is not working.

$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上。
如果我使用相同的代码显示同一个域中的图像,图像看起来很好。

but when i try to do that, images wont display on the www.domain2.com/user.php. if i use the same code to display images which are in the same domain, images are appearing fine.

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

Hope i explain the situation correctly. Please help.

提前致谢

Glob需要文件访问权限。但是因为它在另一个领域。它不会获得文件访问权限(它不应该)。即使它们位于同一台服务器上,它们也不应该因为很多原因而访问其他文件。

Glob needs file-access. But since it is on another domain. It doesn't get file-access (and it shouldn't). Even if they are on the same server, they shouldn't be able to access each others files for so many reasons.

你能做的就是写一个小的API domain1.com,返回特定用户的图像列表。
然后您在domain1.com上使用forntance curl

What you can do is write a small API on domain1.com that returns a list of images for a certain user. You then access that information using for isntance curl

访问该信息,其中存储了图片:

on domain1.com where the pictures are stored:

<?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

在domain2.com上:

on domain2.com:

<?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'

备注:

1)我在这里使用了file_get_contents而不是curl,因为它更容易使用。但并非所有主机都允许将file_get_contents用于其他域。如果他们不允许它使用curl(互联网上有很多教程)

1) I used file_get_contents instead of curl here since it is easier to use. But not all hosts allow a file_get_contents to a different domain. If they don't allow it use curl (there are a lot of tutorials on the internet)

2)你应该检查$ user_id是否正确甚至添加一个请求保密hack0rs的密钥。例如: file_get_contents('http://www.domain1.com/api/images.pgp?user_id=1&secret=mySecret')然后在domain1.com上做一个简单的检查或秘密是正确的。

2) You should check if the $user_id is correct and even add a secret key to the request to keep the hack0rs out. e.g.: file_get_contents('http://www.domain1.com/api/images.pgp?user_id=1&secret=mySecret') And then on domain1.com do a simpel check to see or the secret is correct.