Download multiple images at once from a site in php

0

Next, there is a site 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 it returns an image in PNG .

I need help finding out how I can create a code to automate the job (a light to know where to start, tips and more)

Thanks in advance, thank you.

I'm a good C programmer, but I understand little of other languages, and I know I'll have to learn the basics, because I do not know if it will work in C.

    
asked by anonymous 18.02.2018 / 02:29

2 answers

2

You can do this using file_get_contents and file_put_contents or # .

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 headers, it is not as complete as cURL , but it is 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 cURL you can work better with Cookies , Headers , Follow Location , Authentication etc . It is a complete tool for requisitions. I'll use the basics 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);
}

Note: You can do many things with C , including this. Remember that the   PHP is developed with C .

    
18.02.2018 / 03:04
0

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

Let's try to solve for JAVASCRIPT by the browser, then you will create 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 the images use the command Ctrl + S to save a copy of the page, it will save all images loaded in a folder with the name of the file that you define: site_files

    
18.02.2018 / 22:53