dream04110 2015-04-21 03:17
浏览 44

从域导入图像到子域?

How do you link images from a domain to a subdomain in PHP?

I have a slider on my main site. I want to use the same images on my mobile site which is subdomained at m.mysite.com

I have tried to following code(which I use for my current slider):

<?php
$files = glob('images/gallery/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
  echo "<div class='slide'><a href=''><img src='" . $file . "' alt='description'></a></div>";
}
?>

Nothing happens. So I modify my code to this:

<?php
$files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
  echo "<div class='slide'><a href=''><img src='" . $file . "' alt='description'></a></div>";
}
?>

This time it kinda works. It outputs this:

<div class='slide'><a href=''><img src='../images/gallery/1012081_1501438913448804_6782879489506876198_n.jpg' alt='description'></a></div>
<div class='slide'><a href=''><img src='../images/gallery/10177464_1495074060751956_373648277891171115_n.jpg' alt='description'></a></div>

All of the images are "broken".

When I click the img src in the source code I get this: http://m.example.com/images/gallery/1012081_1501438913448804_6782879489506876198_n.jpg

It seems as though the code is looking in the right directory, makes a div for each image, but then adds the m. to the beginning.

How can I fix this to where it does not look in the mobile directory for these images?

To make it simple the image link should be http://example.com/images/gallery/1012081_1501438913448804_6782879489506876198_n.jpg

Thanks guys.

  • 写回答

1条回答 默认 最新

  • dongque3797 2015-04-21 04:28
    关注

    I see two possible solutions here:

    1. If you want to deliver it from the mobile subdomain, the webserver needs it inside the webroot of the mobile website. You could symlink the image dir to the subdomain, so it has the same path for both domains. Maybe you'll have to tell your web server to follow symlinks, too.

    2. Just serve the images from your main domain on the mobile website

    A possible solution might look like:

    <?php
    $files = glob('../images/gallery/*.{jpg,png,gif}', GLOB_BRACE);
    $domain = "http://www.example.com/"; //your domain
    foreach($files as $file) {
      $path = ltrim($file, './'); //strip ../ from the beginning of the path
    
      echo "<div class='slide'><a href=''><img src='" . $domain . $path . "' alt='description'></a></div>";
    }
    

    A side note: don't use ?> at the end of your php files, you don't need it. Cause if you accidently have a line break (or whatever) after that it will be printed out to the browser imedeatly and cause problems if you ever want to send a header...

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题