Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wyświetlanie plików z folderu
Forum PHP.pl > Forum > Przedszkole
Gość_MICHALL_*
Czy jest jakiś skrtyp w php który wyświetlał by wszystkie pliki txt znajdujące się w podamym folderze??
i czy da się za jego pomocą stworzyć tabele w której wyświetlone były by te pliki wraz z zmiennymi w nich zamieszczonymi?
Gość
Mam taki skrypt
  1. <?php 
  2. if ($handle = opendir('newsy/')) {
  3.  while (false !== ($file = readdir($handle))) { 
  4.  if ($file != "." && $file != "..") { 
  5. include("newsy/$file");
  6.  } 
  7.  }
  8.  closedir($handle); 
  9. }
  10. ?>

jak zrobić aby pliki układały się nazwami (nazwy to numery od 0-99)
ukłądały się malejąco questionmark.gif
Athlan
readdir" title="Zobacz w manualu php" target="_manual czyta pliki jak lecą, nie da się smile.gif

ja bym radził wsadzić wszystkie nazwy do tablicy array_push" title="Zobacz w manualu php" target="_manual i posortować wg nawy smile.gif

pozdrawiam smile.gif
Gość
Nie umiem posłuiwać się array_push sad.gif
NetJaro
Ludzie, po co na siłe smile.gif?

Wystarczy przeczytać komentarze do funkcji readdir" title="Zobacz w manualu php" target="_manual.

  1. <?php
  2. function listImages($dirname, $sortby, $sortdir) {
  3.  
  4. $ext = array("jpg", "png", "jpeg", "gif");
  5. $files = array();
  6.  if($handle = opendir($dirname)) {
  7.  while(false !== ($file = readdir($handle))){
  8.  for($i=0;$i<sizeof($ext);$i++){
  9.  if(stristr($file, ".".$ext[$i])){ //NOT case sensitive: OK with JpeG, JPG, ecc.
  10.  $imgdata = getimagesize($dirname . "/" . $file);
  11.  $filesize = filesize($dirname . "/" . $file);
  12.  //Some formats may contain no image or may contain multiple images.
  13.  //In these cases, getimagesize() might not be able to properly determine the image size.
  14.  //getimagesize() will return zero for width and height in these cases.
  15.  if($imgdata[0] && $imgdata[1] && $filesize){
  16.  $files[] = array(
  17.  "name" => $file,
  18.  "width" => $imgdata[0],
  19.  "height" => $imgdata[1],
  20.  "size" => $filesize
  21.  );
  22.  } 
  23.  }
  24.  }
  25.  }
  26.  closedir($handle);
  27.  }
  28.  
  29.  //obtain an array of columns
  30.  foreach ($files as $key => $row) {
  31.  $name[$key] = $row['name'];
  32.  $width[$key] = $row['width'];
  33.  $height[$key] = $row['height'];
  34.  $size[$key] = $row['size'];
  35.  }
  36.  
  37. return array_multisort($$sortby, $sortdir, $files) ? $files : false;
  38. }
  39. //end listImages
  40.  
  41. //start test
  42. $sortby = "width"; // sort-by column; accepted values: name OR width OR height OR size
  43. $path = "path"; // path to folder
  44. $sortdir = SORT_ASC; // sorting order flags; accepted values: SORT_ASC or SORT_DESC
  45.  
  46. $files = listImages($path, $sortby, $sortdir);
  47. foreach ($files as $file){
  48.  echo 'name = <strong>' . $file['name'] . '</strong> width = <strong>' . $file['width'] . '</strong> height = <strong>' . $file['height'] . '</strong> size = <strong>' . $file['size'] . '</strong><br />';
  49. }
  50. //end test
  51. ?>

smile.gif
Gość
Nie ma jakiegoś innego sposobu questionmark.gif
chce włąśnie uniknąc tablicy.

Mój problem polega na tym, że miałem zamiar zrobić system newsów
w którym istnieje obcja usun.
Ale nie umiem wycinać danego fragmentu pliku (robiłem to za pomocą include) i dlatego pomyślałem że jeżeli wczytam poprzez ten kod
  1. <?php 
  2. if ($handle = opendir('newsy/')) {
  3.  while (false !== ($file = readdir($handle))) { 
  4.  if ($file != "." && $file != "..") { 
  5. include("newsy/$file");
  6.  } 
  7.  }
  8.  closedir($handle); 
  9. }
  10. ?>

newsy, to uda mi się usunąć plik z danym newsem poprzez co nie będzie on już wyświetlany smile.gif
Athlan
  1. <?php
  2. $files[] = array(
  3.  "name" => $file,
  4.  "width" => $imgdata[0],
  5.  "height" => $imgdata[1],
  6.  "size" => $filesize
  7. );
  8. ?>


czyli jednak wkładasz do tablicy...

  1. <?php
  2. //start test
  3. $sortby = "width"; // sort-by column; accepted values: name OR width OR height OR size
  4. $path = "path"; // path to folder
  5. $sortdir = SORT_ASC; // sorting order flags; accepted values: SORT_ASC or SORT_DESC
  6. ?>


i sortujesz...

tak jak mówiłem smile.gif

chyba najlepszy sposób poza tym że zamiast do tablicy, wkładasz dane do mysql i je potem wywalasz... tongue.gif

pozdraiwam smile.gif
Gość
Co mam edytować w tym kodzie aby mi działal??
  1. <?php
  2. //listImages(string path, string sort-by name | width | height | size, int sorting order flags SORT_ASC | SORT_DESC)
  3. function listImages($dirname, $sortby, $sortdir) {
  4.  
  5. $ext = array("txt", "txt", "txt", "txt");
  6. $files = array();
  7.  if($handle = opendir($dirname)) {
  8.  while(false !== ($file = readdir($handle))){
  9.  for($i=0;$i<sizeof($ext);$i++){
  10.  if(stristr($file, ".".$ext[$i])){ //NOT case sensitive: OK with JpeG, JPG, ecc.
  11.  $imgdata = getimagesize($dirname . "/" . $file);
  12.  $filesize = filesize($dirname . "/" . $file);
  13.  //Some formats may contain no image or may contain multiple images.
  14.  //In these cases, getimagesize() might not be able to properly determine the image size.
  15.  //getimagesize() will return zero for width and height in these cases.
  16.  if($imgdata[0] && $imgdata[1] && $filesize){
  17.  $files[] = array(
  18.  "name" => $file,
  19.  "width" => $imgdata[0],
  20.  "height" => $imgdata[1],
  21.  "size" => $filesize
  22.  );
  23.  } 
  24.  }
  25.  }
  26.  }
  27.  closedir($handle);
  28.  }
  29.  
  30.  //obtain an array of columns
  31.  foreach ($files as $key => $row) {
  32.  $name[$key] = $row['name'];
  33.  $width[$key] = $row['width'];
  34.  $height[$key] = $row['height'];
  35.  $size[$key] = $row['size'];
  36.  }
  37.  
  38. return array_multisort($$sortby, $sortdir, $files) ? $files : false;
  39. }
  40. //end listImages
  41.  
  42. //start test
  43. $sortby = "width"; // sort-by column; accepted values: name OR width OR height OR size
  44. $path = "path"; // path to folder
  45. $sortdir = SORT_ASC; // sorting order flags; accepted values: SORT_ASC or SORT_DESC
  46.  
  47. $files = listImages($path, $sortby, $sortdir);
  48. foreach ($files as $file){
  49.  echo 'name = <strong>' . $file['name'] . '</strong> width = <strong>' . $file['width'] . '</strong> height = <strong>' . $file['height'] . '</strong> size = <strong>' . $file['size'] . '</strong><br />';
  50. }
  51. //end test
  52. ?>

wyświetla:
Warning: opendir(path) [function.opendir]: failed to open dir: Invalid argument in D:\Program Files\WebServ\httpd\portal\newsy.php on line 7

Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in D:\Program Files\WebServ\httpd\portal\newsy.php on line 38

Warning: Invalid argument supplied for foreach() in D:\Program Files\WebServ\httpd\portal\newsy.php on line 48
Athlan
podałeś złą ścieżkę do katalogu smile.gif

  1. <?php
  2. $path = "path"; // path to folder
  3. ?>


pozdrawiam smile.gif
Gość
i dalej nie działa sad.gif
na jakiej zasadzie on działa?
dem
tak tylko nawiazujac do jednej krotkiej wypowiedzi

Cytat(Gość @ 2.08.2006, 12:12 ) *
Mój problem polega na tym, że miałem zamiar zrobić system newsów
w którym istnieje obcja usun.
Ale nie umiem wycinać danego fragmentu pliku (...)


rozumiem, ze newsy bylyby w jednym pliku, gdzie kazdy wers(linia) to kolejny news z roznymi danymi, tak? jesli o to chodzi to moge ci wytluamczyc jak ma takie cos dzialac, bo sam na tym oparlem moje newsy...

wiem jaki balagan jest potem, gdy chce sie oblsugiwac n plikow w danym folderze, etc
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.