V
V
Valeriy19972015-09-07 17:08:34
JavaScript
Valeriy1997, 2015-09-07 17:08:34

How to download images from other sites?

Is there a solution for downloading images from web resources by url, and then saving them. The bottom line is that the images are saved on the client.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mr-molodoy, 2015-09-07
@Valeriy1997

by "on the client" do you mean the browser?
It seems that the security policy of browsers today will not even allow you to make a cross-domain request and get images without a special header from the server that you are going to "rob". If I'm not mistaken.
You can follow. scheme:
Client <- Your site -> Site "victim".
Those:

$.post('/getImages.php', {
     url: 'http://site.com/images/image.jpg'
}, function (data) {
    // Сохраняем изображения
    // Правда признаюсь - это извращение ;)
}, 'JSON');

<?php
     $url = $_POST['url'];
     $filename = end('/', $url);
     $path = __DIR__ . '/images_cache/';

     file_put_contents($path . $filename, file_get_contents($url));

     json_encode([
          'image' => '/images_cache/' . $filename,
          'filename' => $filename
     ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question