Options for reading a remote directory

发布于 2022-09-06 09:31:02 字数 1674 浏览 14 评论 0

I have a script that displays images based on certain conditions. When none of the conditions are met, I want to randomly display one of the standard (backup) images. Those other images are on a remote server. I have read that you can't read a directory on a remote server, which makes sense.

Is my best bet to place a file into the remote server's image directory that outputs all of the image file names so I can parse it with the other server? Is there an easier way?

I prefer not to use FTP (http://php.net/manual/en/book.ftp.php).

What are my options for basically just getting the names of the images in that folder?

Thanks, Ryan

UPDATE:

@mario's answer is lightweight and works like a charm. It is exactly the solution I thought I wanted, but after thinking about it some more, and reading that even @mario would do it differently, I decided to go with @bensiu's answer, because to me, control and security are more important than convenience. With @mario's method, it's very hard to know if the data you're getting is any good (lack of control) and you're exposing your directory / some server information (security). @bensiu's suggestion involves a second file (inconvenience), but provides the control and security I'm ultimately deciding to go with!

Thank you both!

-Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2022-09-13 09:31:02

I would prefer an exact and dedicated handler script like @bensiu pointed out.

But an alternative would be to read out a directory listing. A simple Apache generated mod_index listing would be sufficient for:

$html = file_get_contents("http://example.com/images/");
preg_match_all('/<a href="([-\w\d.]+\.(jpeg|png|gif))"/', $html, $uu);
$files = $uu[1];
水水月牙 2022-09-13 09:31:02

I hope you at least have access to remote server...

You can place there script "A" that will do the job locally, return list of images in preffered format ( raw text, JSON, XML... ), and this script will be remotly called by curl form your server....

It also wise to make sure that when you call script "A" you at least passing some secret key to prevent unathorised access (not perfect solution but could be enought)

梦里的微风 2022-09-13 09:31:02

if you have PHP5 and the HTTP stream wrapper enabled on your server, it's very easy and simple to copy it to a local file:

copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');

ome hosts disable copy() function then you can make your own -

<?php 
    function copyemz($file1,$file2){ 
          $contentx =@file_get_contents($file1); 
                   $openedfile = fopen($file2, "w"); 
                   fwrite($openedfile, $contentx); 
                   fclose($openedfile); 
                    if ($contentx === FALSE) { 
                    $status=false; 
                    }else $status=true; 

                    return $status; 
    } 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文