2013-07-04 36 views
0

我在同一台服务器中有两个域。 www.domain1.com & www.domain2.com。获取同一服务器中另一个域中域的文件内容

在www.domain1.com中,有一个名为'Pictures'的文件夹。对于该文件夹,用户可以通过用他们的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显示。 如果我使用相同的代码来显示在相同的域中的图像,图像显示正常。

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

在此先感谢

+0

用户是否已阅读两个域的权限?通常2个域不能访问其他文件。或者把文件放在可由 –

+0

访问的子目录中,我将检查读取权限。有什么我们可以做的吗? –

回答

1

Glob需要文件访问。但因为它在另一个域上。它没有得到文件访问权(它不应该)。即使它们位于同一台服务器上,由于很多原因,它们也不应该能够访问其他文件。

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

上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&secret=mySecret')然后在domain1.com上进行simpel检查以查看密钥是否正确。

+0

你能帮我吗?因为我是PHP新手,并且不太了解实例curl –

+0

我用一个小例子对我的答案做了一个小的编辑。更多信息:http://www.binarymoon.co.uk/2010/05/alternative-curl/ – Pinoniq

相关问题