Thread: fopen a URL?

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    230
    Rep Power
    96

    fopen a URL?


    I need to get the contents of a directory from one domain of ours and use it on my local machine and/or another domain of ours.

    PHP Code:
    // Check $WooComm against the DCI directory (export as a CSV) to see what images are needed
    $DCICSV "dci_images.csv";
    $MissingImages = array();
    $images = array();
    if (
    file_exists($DCICSV) AND is_readable($DCICSV)) {
        
    $DCICSVList file($DCICSVFILE_IGNORE_NEW_LINES);
        for (
    $i 0$i $WooCommCount$i++) {
            
    $images[] = $WooComm[$i]['sku'].".jpg";
            
    $image $WooComm[$i]['sku'].".jpg";
            if (!
    in_array($image$DCICSVList)) {
                
    $MissingImages[] = $image;
            }
        }

    That's my original. I'm trying to avoid having to visit the page that displays all the images and copy/paste that list into a CSV file. I googled a bunch. scandir() doesn't work on an URL. The notes on the scandir page said you can use fopen with the correct fopenwrappers. From all the documentation I came up with this which doesn't work.
    PHP Code:
    // Check $WooComm against the DCI directory (export as a CSV) to see what images are needed
    ini_set("allow_url_fopen"1);
    $handle fopen("http://daystarproductsinternational.com/DCIImages/""r");
    $DirectoryListing scandir($handle);
    $MissingImages = array();
    $images = array();
    if (!empty(
    $DirectoryListing)) {
        for (
    $i 0$i $WooCommCount$i++) {
            
    $images[] = $WooComm[$i]['sku'].".jpg";
            
    $image $WooComm[$i]['sku'].".jpg";
            if (!
    in_array($image$DirectoryListing)) {
                
    $MissingImages[] = $image;
            }
        }

    As I'm typing this I'm now wondering if I need to set the allow_url_fopen on the PHP install of the directory I'm trying to fopen. That makes sense since you wouldn't just want any Tom, **** (good thing my name isn't **** or no one here would be able to address me by name), or Harry to be able to retrieve your directory listing.

    Am I correct that I need to set allow_url_fopen on the remote website?

    Is there an easier way?

    Thanks,

    Mike
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,080
    Rep Power
    4101
    You cannot read the directory listing of a remote URL. Simple as that.

    The remote URL has to provide you a directory listing by generating a page with a list of the files. You then have to parse the output of that webpage to gather the list of files. None of PHP's directory related functions will help you here. The DOM extension may be helpful to make parsing the HTML easier.

    So what you need to do is look at the HTML of that URL in your code and determine how you can parse it and extract the list of files from it.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,780
    Rep Power
    4536
    Once you have the HTML, using whatever method you want, this will parse it and return all of the "XXXX.jpg" listed on the page in $matches[1].

    Code:
    preg_match_all("/>(\w+\.jpg)</", $text, $matches);
    
    var_dump($matches);
    See it in action here: https://3v4l.org/8oAKV

    -John
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo