user3742640
user3742640

Reputation: 23

get images files with different parts in names

hi guys i have problem to get images with different parts in names, let me to explain more. in directory i have many pictures with this format in name : 1-[1-9].jpg or 2-[1-9].jpg or ... for example names can be 1-5.jpg or 1-14.jpg or 2-3.jpg so i dont know what is true way to get my files!!

this is an example what i want:

function get_data($url) 
{
    $ch = curl_init();
    $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
    return $data;
}

$diff = [1-9];

$html = get_data('http://mysite.info/screenshots/1-' . $diff. '.jpeg' );

Upvotes: 1

Views: 49

Answers (2)

arkascha
arkascha

Reputation: 42915

You cannot use wildcards or similar approaches in http requests. Such a request has to query exactly one specific object, except if you have a processing logic in the server side.

In your example you use urls requesting a specific image file. Using that approach you request exactly one single and specific image. You'd have to make a single request for each possible name pattern to retrieve all available image files matching your desired pattern.

Another approach would be to not request a specific image in your http request, but a processing logic, typically a script on the server side. Such script can accept things like a wildcard pattern (or similar) and return matches, even multiple images at once. But this obviously means that you have control over that server system, can implement the logic on that system. So it has to be your system you query the images from.

Upvotes: 0

Tuan Anh Hoang-Vu
Tuan Anh Hoang-Vu

Reputation: 1995

If it is one of your server, you should figure out what is the pattern of your image file names, for example, x-y.jpeg, where x is from 1 to 100, and y is from 1 to 9. Then process with this code:

foreach (range(1, 100) as $x) {
    foreach (range(1, 9) as $y) {
        $html = get_data('http://mysite.info/screenshots/'.$x.'-'.$y.'.jpeg' );
    }
}

Upvotes: 1

Related Questions