Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Kopiowanie zdjęć zzewnętrzego serwera
Forum PHP.pl > Forum > Przedszkole
sweter
Witam,
chcę skopiować zdjęcie z Fotka.pl więc wykonuję takie polecenie:
  1. copy('http://c10.asteroid.pl/a.eu.fotka.pl.s3.amazonaws.com/057/738/57738379_640.jpg', '/images/plik.jpg');

Niestety, nie wiem czemu ale za każdym razem wyświetla mi:
Cytat
Warning: copy(/images/plik.jpg): failed to open stream: No such file or directory

Wszystkie ścieżki są oczywiście poprawne.
Może to jakieś zabezpieczenie ze strony serwera, aby nikt nie pobierał zdjęć?

Pozdrawiam
Spirit86
If you have PHP5 and the HTTP stream wrapper enabled on your server, it's incredibly simple to copy it to a local file:
copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');

Remote files. This needs allow_url_fopen to be enabled in php.ini, but it's the easiest method.

Alternatively you could use cURL if your PHP installation supports it. There's even an example.

And if you really want to do it manually use the HTTP module.

Don't even try to use sockets directly.

  1. $url = "http://other-site/image.png";
  2. $dir = "/my/local/dir/";
  3.  
  4. $rfile = fopen($url, "r");
  5. $lfile = fopen($dir . basename($url), "w");
  6.  
  7. while(!feof($url)) fwrite($lfile, fread($rfile, 1), 1);
  8.  
  9. fclose($rfile);
  10. fclose($lfile);



  1. $url = "http://other-site/image.png";
  2. $dir = "/my/local/dir/";
  3. $lfile = fopen($dir . basename($url), "w");
  4.  
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_URL, $url);
  7. curl_setopt($ch, CURLOPT_HEADER, 0);
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
  10. curl_setopt($ch, CURLOPT_FILE, $lfile);
  11.  
  12. fclose($lfile);
  13. curl_close($ch);
sweter
Dziękuję, ale gdy pobieram fotę za pomocą Twojego kodu (tego drugiego) to, owszem zapisuje mi plik na HDD, ale przy otwarciu wyświetla mi błąd:
Cytat
Błąd podczas interpretowania pliku obrazu JPEG (Improper call to JPEG library in state 200)
korey
mam dosłownie taki sam problem i nie wiem juz jak sobie z nim poradzić
Oidar
Jak by ktoś jeszcze kiedyś miał z tym problem to taki mały kod wyciągnąłem od siebie:
  1. function curl($adres) {
  2. $curl = curl_init($adres);
  3. curl_setopt($curl, CURLOPT_FAILONERROR, 1);
  4. curl_setopt($curl, CURLOPT_URL, $adres);
  5. curl_setopt($curl, CURLOPT_HEADER, 0);
  6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  7. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  8. curl_setopt($curl, CURLOPT_TIMEOUT, 40);
  9. return(curl_exec($curl));
  10. curl_closes($curl);
  11. }
  12.  
  13.  
  14. $adres = 'http://zdjecie.jpg';
  15. $img = curl($adres);
  16. file_put_contents('katalog/zdjecie1.jpg', $img);


Funkcja curl pobiera bity obrazka/pliku, zaś funkcja file_put_contents wrzuca go w plik o podanym adresie.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.