【问题标题】:Get file contents of a domain in another domain in the same server获取同一服务器中另一个域中的域的文件内容
【发布时间】:2013-07-02 02:36:07
【问题描述】:

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

在 www.domain1.com 中,有一个名为“图片”的文件夹。用户可以通过他们的 ID 创建一个文件夹来上传他们的图片到该文件夹​​。 (www.domain1.com/Pictures/User_iD) 同时使用上传的图片创建缩略图,并保存到动态创建的路径中。(www.domain1.com/Pictures/User_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 上。 如果我使用相同的代码来显示同一域中的图像,则图像看起来很好。

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

提前致谢

【问题讨论】:

  • 用户是否已阅读两个域的权限?通常 2 个域无权访问彼此的文件。或者将文件放在双方都可以访问的子目录中
  • 我会检查读取权限。我们还有什么可以做的吗?

标签: php image multiple-domains


【解决方案1】:

Glob 需要文件访问。但因为它在另一个域上。它没有获得文件访问权限(也不应该)。即使他们在同一台服务器上,他们也不应该因为很多原因而无法访问彼此的文件。

您可以在 domain1.com 上编写一个小 API,返回某个用户的图像列表。 然后,您使用 for istance 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

在 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(互联网上有很多教程)

2) 您应该检查 $user_id 是否正确,甚至在请求中添加密钥以阻止 hack0rs。例如:file_get_contents('http://www.domain1.com/api/images.pgp?user_id=1&amp;secret=mySecret') 然后在 domain1.com 上做一个简单的检查,看看密码是否正确。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
相关资源
最近更新 更多