Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Kopiowanie zdjęć zzewnętrzego serwera
sweter
post
Post #1





Grupa: Zarejestrowani
Postów: 623
Pomógł: 11
Dołączył: 1.01.2009
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
Spirit86
post
Post #2





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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);
Go to the top of the page
+Quote Post
sweter
post
Post #3





Grupa: Zarejestrowani
Postów: 623
Pomógł: 11
Dołączył: 1.01.2009
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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)
Go to the top of the page
+Quote Post
korey
post
Post #4





Grupa: Zarejestrowani
Postów: 122
Pomógł: 2
Dołączył: 14.08.2009
Skąd: Łódź

Ostrzeżenie: (0%)
-----


mam dosłownie taki sam problem i nie wiem juz jak sobie z nim poradzić
Go to the top of the page
+Quote Post
-Oidar-
post
Post #5





Goście







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.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 00:04