Download multiple images at once from a php site

Asked

Viewed 611 times

0

Next, there’s a website where I need to download several images, I can access the image as follows: url.com/get_img.php?user=VARIÁVEL

where variable is replaced by an integer and the same returns me an image in PNG.

I need some help to know how to create code to automate work (a light to know where to start, tips and etc)

Thanks for your attention, thank you.

Obs: I am a good C programmer, but I understand little of other languages, and I know I will have to learn the basics, because I don’t know if I can do in C.

2 answers

2

You can do using file_get_contents and file_put_contents or Curl.

The file_get_contents can read a local or external file. It is especially suitable for simpler cases. Although you have the freedom to pass some headlines, it’s not nearly as complete as the cURL, but it’s a good option.

You can use it as follows.

<?php

/* ID's da imagem */
$ids = [1,2,3,4,5,6,7,8,9,10];

foreach($ids as $id) {
    /* Acessa, baixa e armazena o arquivo na variável $content */
    $content = file_get_contents("https://url.com/get_img.php?user={$id}");

    /* Escreve o conteúdo da variável $content em 1.png, 2.png, 3.png ... */
    file_put_contents("{$id}.png", $content);
}

With the cURL you can work better with Cookies, Headers, Follow Location, Authentication etc. It is a complete tool for requests. I will use the basic as an example.

<?php

/* ID's da imagem */
$ids = [1,2,3,4,5,6,7,8,9,10];

foreach($ids as $id) {

    /* Cria um novo arquivo e caso o arquivo exista, substitui */
    $file = fopen("{$id}.png", "w+");

    /* Cria uma nova instância do cURL */
    $curl = curl_init();

    curl_setopt_array($curl, [
        /* Define a URL */
        CURLOPT_URL            => "https://url.com/get_img.php?user={$id}",

        /* Informa que você quer receber o retorno */
        CURLOPT_RETURNTRANSFER => true,

        /* Define o "resource" do arquivo (Onde o arquivo deve ser salvo) */
        CURLOPT_FILE           => $file,

        /* Header Referer (Opcional) */
        CURLOPT_REFERER        => "https://url.com",

        /* Desabilita a verificação do SSL */
        CURLOPT_SSL_VERIFYPEER => false
    ]);

    /* Envia a requisição e fecha as conexões em seguida */
    curl_exec($curl);
    curl_close($curl);
    fclose($file);
}

Obs.: You can do a lot of things with C, including this. Remember that the PHP is developed with C.

0

As I understood the site is not yours, so you are trying to get all images with a single download method, otherwise you would access the FTP directly and download them.

Let’s try to work it out JAVASCRIPT by browser, then you will create an HTML by any text editor that will be opened directly by your browser without having to run from a server:

function loadImgs(){
  /*--- Dados do formulário ---*/
  var dataform = document.forms[0] || document.forms['dados'];
  var t = dataform.imgtotal.value;
  var url = dataform.url.value;
  var ext = dataform.ext.value;
  dataform.buscar.disabled = true;
  /*--- Gerar IMGS ---*/
  var div = document.getElementById('dl_imgs') || document.dl_imgs;
  div.innerHTML = "";
  for(i=0;i<t;i++){
    div.innerHTML += '<img src="'+url+""+i+''+ext+'" />';
  }
}
img{
  margin:10px;
  border-style: double;
  background-color:#f0f0f0;
  width: 50px;
  height: 50px;
}

#dl_imgs{
  margin:10px;
  background-color: #0fffff;
}
<form name="dados">
URL (Com HTTP e parametros): <input value="" name="url" type="text" /><br />
Aumente até o número estimativo: <input value="100" name="imgtotal" type="number" /><br />
Preencha a extensão com ponto ou deixe vazio: <input value=".png" name="ext" type="text" /><br />
<input value="Buscar" name="buscar" type="button" onclick="loadImgs()" />
</form>
<div id="dl_imgs"></div>

After loading all images use the command Ctrl+S to save a copy of the page, it will save all uploaded images in a folder named after the file you set: site_files

Browser other questions tagged

You are not signed in. Login or sign up in order to post.