Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Policzenie rozmiaru pliku
Destrudo
post
Post #1





Grupa: Zarejestrowani
Postów: 74
Pomógł: 0
Dołączył: 10.10.2011

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


Hej,

Mam taki problem, mianowicie: chciałbym listować pliki css z danej strony i liczyć ich rozmiar. Listowanie udało się zrobić poniższym kodem:
  1. <?php
  2.  
  3. $url="wp.pl";
  4. $file = file_get_contents('http://'.$url);
  5. $doc = new DOMDocument();
  6. $doc->loadHTML($file);
  7. $domcss = $doc->getElementsByTagName('link');
  8. $sum=0;
  9. foreach($domcss as $links) {
  10. if( strtolower($links->getAttribute('rel')) == "stylesheet" ) {
  11. $tre = "". $links->getAttribute('href') ."<br />";
  12. echo $tre;
  13. }
  14. }
  15.  
  16. ?>

jednak za nic nie mogę zrobić poprawnego wyliczenia rozmiaru w kb. Myslałem o liczeniu znaków (to byłyby bajty), ale też nie chce poprawnie mi to zadziałać. Macie może jakiś pomysł?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
markuz
post
Post #2





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

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


http://stackoverflow.com/questions/2781684...f-a-remote-file
Go to the top of the page
+Quote Post
Destrudo
post
Post #3





Grupa: Zarejestrowani
Postów: 74
Pomógł: 0
Dołączył: 10.10.2011

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


Próbowałem już z tym sposobem, jednak nie liczy mi poprawnie i nie wiem właśnie dlaczego. Kod po aktualizacji wygląda tak:
  1. <?php
  2.  
  3. $url="wp.pl";
  4. $file = file_get_contents('http://'.$url);
  5. $doc = new DOMDocument();
  6. $doc->loadHTML($file);
  7. $domcss = $doc->getElementsByTagName('link');
  8. $sum=0;
  9. foreach($domcss as $links) {
  10. if( strtolower($links->getAttribute('rel')) == "stylesheet" ) {
  11. $tre = "". $links->getAttribute('href') ."<br />";
  12. echo $tre;
  13. }
  14. }
  15. function remote_file_size($url){
  16. # Get all header information
  17. $data = get_headers($url, true);
  18. # Look up validity
  19. if (isset($data['Content-Length']))
  20. # Return file size
  21. return (int) $data['Content-Length'];
  22. }echo remote_file_size($tre);
  23. ?>


Pewnie zrobiłem jakiś głupi błąd, ale gdy wpisze się zamiast zmiennej $tre zwykły link, liczy poprawnie, a gdy jest zmienna, podaje dziwną wartość - https://destrudo.pl/xx/scr.php

Po wpisaniu zamiast zmiennej bezwzględnej ścieżki do tego css'a, który się tam wyświetla, pokazuje poprawną wartość - 323326.

Nie mam teraz pomysłu jak to zrobić, żeby działało ze zmienną (zmienna może generować kilka cssow, ale w tym wypadku generuje jednego, na którym i tak nie działa)
Go to the top of the page
+Quote Post
chomikiki
post
Post #4





Grupa: Zarejestrowani
Postów: 14
Pomógł: 1
Dołączył: 16.09.2016
Skąd: Wrocław

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


  1. function remote_file_size($url){
  2. # Get all header information
  3. $data = get_headers($url, true);
  4. # Look up validity
  5. if (isset($data['Content-Length'])){
  6. # Return file size
  7. return (int) $data['Content-Length'];
  8. }
  9. }
  10.  
  11. $url='wp.pl';
  12. $file = file_get_contents('http://'.$url);
  13. $doc = new DOMDocument();
  14. $doc->loadHTML($file);
  15. $domcss = $doc->getElementsByTagName('link');
  16. $sum=0;
  17.  
  18. foreach($domcss as $links) {
  19. if($links->getAttribute('rel') == "stylesheet") {
  20. echo $links->getAttribute('rel').' ';
  21. $tre=$links->getAttribute('href');
  22. echo $tre.'<br>';
  23. $sum+=remote_file_size($tre);
  24. }
  25. }
  26. echo 'suma dla '.$url.' -> '.$sum;
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 23.08.2025 - 03:57