0

I have been trying to convert to images the results from a list produced by file() an by file_get_contents and I have got this far but cant seem to be getting any where: Here's what I got:

<?php
$data = file('http://cadenanoticias.com.mx/galerias/notas/22925/');

foreach((array)$data as $whoisline){
echo '<img src="http://cadenanoticias.com.mx/galerias/notas/22925/'.$whoisline.'">';
}
?>

So far I can see the ending double quotes and the closing bracket and that's it.

Any Idea on how to achieve this?

0m3r
  • 12,286
  • 15
  • 35
  • 71
Samuel Ramzan
  • 1,770
  • 1
  • 15
  • 24
  • 1
    $data is already going to be an array. there's no point in casting an array to an array. done any basic debugging, like checking what `var_dump($data)` shows? – Marc B Jul 23 '15 at 21:11
  • is that your site? just use `glob()` –  Jul 23 '15 at 21:11
  • Yes its mine, I use it as an images server; – Samuel Ramzan Jul 23 '15 at 21:13
  • no need to scrape your own site via HTTP, just access the directory directly and use glob() –  Jul 23 '15 at 21:16
  • No, Im doing this from a remote server. – Samuel Ramzan Jul 23 '15 at 21:17
  • ah ok then, no glob option - carry on ;-) –  Jul 23 '15 at 21:17
  • @Dagon Why now a spider? That scared me :) 'Ol flower was much nicer. – Rizier123 Jul 23 '15 at 21:20
  • you could add a simple index file (API) and serve the files to your script in friendlier manor. but not a big deal –  Jul 23 '15 at 21:20
  • @Rizier123 i photographed it in the office, i thought it matched my friendly personality –  Jul 23 '15 at 21:21
  • If i Use: $data = file_get_contents ('http://cadenanoticias.com.mx/galerias/notas/22925/'); I can see the list of files... All I want is to grab them in put them in the src of an IMG... This is getting to tough. – Samuel Ramzan Jul 23 '15 at 21:31
  • the problem is you get html in the way, you need to parse that to get to the directory listing, –  Jul 23 '15 at 21:40

4 Answers4

1

With a little help of PHP Simple HTML DOM Parser you could just do the following.

<?php
require "simple_html_dom.php";

// Create DOM from URL or file
$html = file_get_html("http://cadenanoticias.com.mx/galerias/notas/22925/");

// Find all images
foreach($html->find("a") as $element) {
    if (substr(strrchr($element->href,'.'),1) == "jpg") {
        echo '<img src="http://cadenanoticias.com.mx/galerias/notas/22925/'. $element->href .'">';
    }
}
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
  • Thank's that worked like magic... Love You man!! Thank's to all of you for your comments, I will analyse every reply to learn more from you guys... – Samuel Ramzan Jul 23 '15 at 21:53
0

The data returned in your example using file_get_contents is HTML, so in order to extract the images you'll have to parse the HTML.

I would recommend using curl instead to fetch the HTML as file_get_contents doesn't always allow fetching of remote URLs.

Another option is to use preg_match to extract the image URLs and then you can loop through them as desired.

If the directory is local, then you could use glob and then the data returned will be an array that you can loop through.

Community
  • 1
  • 1
Jamie Bicknell
  • 2,306
  • 17
  • 35
0

ok my idea for what it is worth:

on the http://cadenanoticias.com.mx/galerias/notas/22925/ site add an index.php:

<?php
$files=array();
foreach (glob("*.jpg") as $filename) {
$files[]=$filename;
}
echo json_encode($files);

on the script on the other server

<?php
    $data = file_get_contents('http://cadenanoticias.com.mx/galerias/notas/22925/');

    foreach(json_decode($data) as $myImage){
    echo '<img src="http://cadenanoticias.com.mx/galerias/notas/22925/'.$myImage.'">';
    }
    ?>
  • I would have to do the same for the other 14 thousand folders, I don't thing it would work, thanks anyway :) – Samuel Ramzan Jul 23 '15 at 21:47
  • you have a defult listing via appache –  Jul 23 '15 at 21:50
  • to make the pure scrapeing approach a LOT esier: http://httpd.apache.org/docs/trunk/mod/mod_autoindex.html#indexoptions –  Jul 23 '15 at 21:52
-1

You need to look into glob.

foreach (glob("/galerias/notas/22925/*.{png,jpg,tiff}") as $filename) {

echo '<img src="http://cadenanoticias.com.mx/galerias/notas/22925/'.$filename.'">';

}
Austin Collins
  • 439
  • 3
  • 13
  • 3
    um No, it wont.. " **Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem** –  Jul 23 '15 at 21:16