Débutant demmande aide sur un script php

xunil2003 Messages postés 761 Date d'inscription mercredi 17 novembre 2004 Statut Membre Dernière intervention 24 mars 2024 - Modifié le 15 juil. 2018 à 20:21
jordane45 Messages postés 38136 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 16 avril 2024 - 16 juil. 2018 à 10:03
Bonjour,

J'ai récupéré un code php pour mon site personnelle pour télécharger de gros fichier. Mon site internet est chez l'hébergeur Free avec protection htaccess.
Code : https://cyberzoide.developpez.com/php4/file/

C'est pour mon avocat qui est sur Paris, pour qu'il puisse piocher les pièces dont il à besoin. il y a uniquement des fichiers : PDF, DOCX, XLS, JPG, PNG, MP4, EML.

1) Je n'arrive pas à comprendre comment modifier l'affichage des noms de fichiers en lien hypertexte pour que l'utilisateur clique sur le fichier afin de le télécharger.

2) Ensuite j'ai deux messages d'erreur :
Warning: is_dir() [function.is-dir]: Unable to access ../../.. in /mnt/129/sda/3/2/monsite/niveau-0/2018-02-pieces-TGI-Draguinan/fichiers.php on line 33

la ligne 33 correspond à :
if(is_dir($base."/".$entry) && !in_array($entry, array(".",".."))) {


Warning: opendir(../../niveau-0/2018-02-pieces-TGI-Draguinan/fichiers_3.php) [function.opendir]: failed to open dir: No such file or directory in /mnt/129/sda/3/2/ld2018/niveau-0/2018-02-pieces-TGI-Draguinan/fichiers_3.php on line 65

la ligne 65 correspond à :
if ($dir = opendir($cur)) {


Voici le code complet :
<html>
<head>
<style type="text/css">


* {font-size: 10pt;}</style></head><body> <?php /* racine */$BASE = "../.."; /* infos à extraire */function addScheme($entry,$base,$type) {  $tab['name'] = $entry;  $tab['type'] = filetype($base."/".$entry);  $tab['date'] = filemtime($base."/".$entry);  $tab['size'] = filesize($base."/".$entry);  $tab['perms'] = fileperms($base."/".$entry);  $tab['access'] = fileatime($base."/".$entry);  $t = explode(".", $entry);  $tab['ext'] = $t[count($t)-1];  return $tab;} /* liste des dossiers */function list_dir($base, $cur, $level=0) {  global $PHP_SELF, $BASE, $order, $asc;  if ($dir = opendir($base)) {    $tab = array();    while($entry = readdir($dir)) {      if(is_dir($base."/".$entry) && !in_array($entry, array(".",".."))) {        $tab[] = addScheme($entry, $base, 'dir');      }    }    /* tri */    usort($tab,"cmp_name");    foreach($tab as $elem) {      $entry = $elem['name'];      /* chemin relatif à la racine */      $file = $base."/".$entry;     /* marge gauche */      for($i=1; $i<=(4*$level); $i++) {        echo " ";      }      /* l'entrée est-elle le dossier courant */      if($file == $cur) {        echo "<img src=\"dir-open.gif\" /> $entry<br />\n";      } else {        echo "<img src=\"dir-close.gif\" /> <a href=\"$PHP_SELF?dir=". rawurlencode($file) ."&order=$order&asc=$asc\">$entry</a><br />\n";      }      /* l'entrée est-elle dans la branche dont le dossier courant est la feuille */      if(ereg($file."/",$cur."/")) {        list_dir($file, $cur, $level+1);      }    }    closedir($dir);  }} /* liste des fichiers */function list_file($cur) {  global $PHP_SELF, $order, $asc, $order0;  if ($dir = opendir($cur)) {    /* tableaux */    $tab_dir = array();    $tab_file = array();    /* extraction */    while($file = readdir($dir)) {      if(is_dir($cur."".$file)) {        if(!in_array($file, array(".",".."))) {          #$tab_dir[] = addScheme($file, $cur, 'dir');         }      } else {          $tab_file[] = addScheme($file, $cur, 'file');      }    }    /* tri */    usort($tab_dir,"cmp_".$order);    usort($tab_file,"cmp_".$order);    /* affichage */    echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";    echo "<tr style=\"font-size:8pt;font-family:arial;\">    <th>".(($order=='name')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur).    "&order=name&asc=$asc&order0=$order\">Nom</a></th><td> </td>    <th>".(($order=='size')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=size&asc=$asc&order0=$order\">Taille</a></th><td> </td>    <th>".(($order=='date')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=date&asc=$asc&order0=$order\">Dernière modification</a></th><td> </td>    <th>".(($order=='type')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=type&asc=$asc&order0=$order\">Type</a></th><td> </td>    <th>".(($order=='ext')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=ext&asc=$asc&order0=$order\">Extention</a></th><td> </td>    <th>".(($order=='perms')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=perms&asc=$asc&order0=$order\">Permissions</a></th><td> </td>    <th>".(($order=='access')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=access&asc=$asc&order0=$order\">Dernier accès</a></th></tr>";    foreach($tab_dir as $elem) {      echo "<tr><td><img src=\"dir-close.gif\" /> ".$elem['name']."</td><td> </td>      <td> </td><td> </td>      <td>".date("d/m/Y H:i:s", $elem['date'])."</td><td> </td>      <td>".assocType($elem['type'])."</td><td> </td>      <td> </td><td> </td>      <td>".$elem['perms']."</td><td> </td>      <td>".date("d/m/Y", $elem['access'])."</td></tr>\n";    }    foreach($tab_file as $elem) {      echo "<tr><td><img src=\"file-none.gif\" /> ".$elem['name']."</td><td> </td>      <td align=\"right\">".formatSize($elem['size'])."</td><td> </td>      <td>".date("d/m/Y H:i:s", $elem['date'])."</td><td> </td>      <td>".assocType($elem['type'])."</td><td> </td>      <td>".assocExt($elem['ext'])."</td><td> </td>      <td>".$elem['perms']."</td><td> </td>      <td>".date("d/m/Y", $elem['access'])."</td></tr>\n";    }    echo "</table>";    closedir($dir);  }} /* formatage de la taille */function formatSize($s) {  /* unités */  $u = array('octets','Ko','Mo','Go','To');  /* compteur de passages dans la boucle */  $i = 0;  /* nombre à afficher */  $m = 0;  /* division par 1024 */  while($s >= 1) {    $m = $s;    $s /= 1024;    $i++;  }  if(!$i) $i=1;  $d = explode(".",$m);  /* s'il y a des décimales */  if($d[0] != $m) {    $m = number_format($m, 2, ",", " ");  }  return $m." ".$u[$i-1];} /* formatage du type */function assocType($type) {  /* tableau de conversion */  $t = array(    'fifo' => "file",    'char' => "fichier spécial en mode caractère",    'dir' => "dossier",    'block' => "fichier spécial en mode bloc",    'link' => "lien symbolique",    'file' => "fichier",    'unknown' => "inconnu"  );  return $t[$type];} /* description de l'extention */function assocExt($ext) {  $e = array(    '' => "inconnu",    'doc' => "Microsoft Word",    'docx' => "Microsoft Word",    'xls' => "Microsoft Excel",    'ppt' => "Microsoft Power Point",    'pdf' => "Adobe Acrobat",    'PDF' => "Adobe Acrobat",    'zip' => "Archive WinZip",    'txt' => "Document texte",    'gif' => "Image GIF",    'jpg' => "Image JPEG",    'png' => "Image PNG",    'php' => "Script PHP",    'php3' => "Script PHP",    'htm' => "Page web",    'html' => "Page web",    'css' => "Feuille de style",    'js' => "JavaScript",    'mp4' => "Vidéo",    'eml' => "E-mail",  );  if(in_array($ext, array_keys($e))) {    return $e[$ext];  } else {    return $e[''];  }} function cmp_name($a,$b) {    global $asc;    if ($a['name'] == $b['name']) return 0;    if($asc == 'a') {        return ($a['name'] < $b['name']) ? -1 : 1;    } else {        return ($a['name'] > $b['name']) ? -1 : 1;        }}function cmp_size($a,$b) {    global $asc;    if ($a['size'] == $b['size']) return cmp_name($a,$b);    if($asc == 'a') {        return ($a['size'] < $b['size']) ? -1 : 1;    } else {        return ($a['size'] > $b['size']) ? -1 : 1;    }}function cmp_date($a,$b) {    global $asc;    if ($a['date'] == $b['date']) return cmp_name($a,$b);    if($asc == 'a') {        return ($a['date'] < $b['date']) ? -1 : 1;    } else {        return ($a['date'] > $b['date']) ? -1 : 1;    }}function cmp_access($a,$b) {    global $asc;    if ($a['access'] == $b['access']) return cmp_name($a,$b);    if($asc == 'a') {        return ($a['access'] < $b['access']) ? -1 : 1;    } else {        return ($a['access'] > $b['access']) ? -1 : 1;    }}function cmp_perms($a,$b) {    global $asc;    if ($a['perms'] == $b['perms']) return cmp_name($a,$b);    if($asc == 'a') {        return ($a['perms'] < $b['perms']) ? -1 : 1;    } else {        return ($a['perms'] > $b['perms']) ? -1 : 1;    }}function cmp_type($a,$b) {    global $asc;    if ($a['type'] == $b['type']) return cmp_name($a,$b);    if($asc == 'a') {        return ($a['type'] < $b['type']) ? -1 : 1;    } else {        return ($a['type'] > $b['type']) ? -1 : 1;    }}function cmp_ext($a,$b) {    global $asc;    if ($a['ext'] == $b['ext']) return cmp_name($a,$b);    if($asc == 'a') {        return ($a['ext'] < $b['ext']) ? -1 : 1;    } else {        return ($a['ext'] > $b['ext']) ? -1 : 1;    }}?> <table border="1" cellspacing="0" cellpadding="10" bordercolor="gray"><tr valign="top"><td> <!-- liste des répertoireset des sous-répertoires --><?php if(!in_array($order, array('name','date','size','perms','ext','access','type'))) {  $order = 'name';}if(($order == $order0) && ($asc != 'b')) {  $asc = 'b';} else {  $asc = 'a';}/* lien sur la racine */if(!$dir) {  echo "<img src=\"dir-open.gif\" /> /<br />\n";} else {  echo "<img src=\"dir-close.gif\" /> <a href=\"$PHP_SELF\">/</a><br />\n";}list_dir($BASE, rawurldecode($dir), 1);   ?> </td><td> <!-- liste des fichiers --><?php/* répertoire initial à lister */if(!$dir) {  $dir = $BASE;} list_file(rawurldecode($dir)); ?> </td></tr></table> </body></html>


Pouvez vous m'aider ?
Merci.

3 réponses

jordane45 Messages postés 38136 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 16 avril 2024 344
15 juil. 2018 à 20:22
Bonjour,

Merci de bien vouloir reposter ton code en faisant des retours à la ligne et en utilisant correctement les balises de code ( y ajouter le LANGAGE afin d'avoir la coloration syntaxique).

J'ai édité ton message... mais je ne vais pas m'amuser à refaire la mise en forme.
Merci.

NB : Pour les balises de code : http://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
0
xunil2003 Messages postés 761 Date d'inscription mercredi 17 novembre 2004 Statut Membre Dernière intervention 24 mars 2024
15 juil. 2018 à 23:59
Bonjour,

Voici le code complet

<html>
<head>
<style type="text/css">
* {font-size: 10pt;}
</style>
</head>
<body>

<?php

/* racine */
$BASE = "../..";

/* infos à extraire */
function addScheme($entry,$base,$type) {
  $tab['name'] = $entry;
  $tab['type'] = filetype($base."/".$entry);
  $tab['date'] = filemtime($base."/".$entry);
  $tab['size'] = filesize($base."/".$entry);
  $tab['perms'] = fileperms($base."/".$entry);
  $tab['access'] = fileatime($base."/".$entry);
  $t = explode(".", $entry);
  $tab['ext'] = $t[count($t)-1];
  return $tab;
}

/* liste des dossiers */
function list_dir($base, $cur, $level=0) {
  global $PHP_SELF, $BASE, $order, $asc;
  if ($dir = opendir($base)) {
    $tab = array();
    while($entry = readdir($dir)) {
      if(is_dir($base."/".$entry) && !in_array($entry, array(".",".."))) {
        $tab[] = addScheme($entry, $base, 'dir');
      }
    }
    /* tri */
    usort($tab,"cmp_name");
    foreach($tab as $elem) {
      $entry = $elem['name'];
      /* chemin relatif à la racine */
      $file = $base."/".$entry;
     /* marge gauche */
      for($i=1; $i<=(4*$level); $i++) {
        echo " ";
      }
      /* l'entrée est-elle le dossier courant */
      if($file == $cur) {
        echo "<img src=\"dir-open.gif\" /> $entry<br />\n";
      } else {
        echo "<img src=\"dir-close.gif\" /> <a href=\"$PHP_SELF?dir=". rawurlencode($file) ."&order=$order&asc=$asc\">$entry</a><br />\n";
      }
      /* l'entrée est-elle dans la branche dont le dossier courant est la feuille */
      if(ereg($file."/",$cur."/")) {
        list_dir($file, $cur, $level+1);
      }
    }
    closedir($dir);
  }
}

/* liste des fichiers */
function list_file($cur) {
  global $PHP_SELF, $order, $asc, $order0;
  if ($dir = opendir($cur)) {
    /* tableaux */
    $tab_dir = array();
    $tab_file = array();
    /* extraction */
    while($file = readdir($dir)) {
      if(is_dir($cur."".$file)) {
        if(!in_array($file, array(".",".."))) {
          $tab_dir[] = addScheme($file, $cur, 'dir');
          
        }
      } else {
          $tab_file[] = addScheme($file, $cur, 'file');
      }
    }
    /* tri */
    usort($tab_dir,"cmp_".$order);
    usort($tab_file,"cmp_".$order);
    /* affichage */
    echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";
    echo "<tr style=\"font-size:8pt;font-family:arial;\">
    <th>".(($order=='name')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur).
    "&order=name&asc=$asc&order0=$order\">Nom</a></th><td> </td>
    <th>".(($order=='size')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=size&asc=$asc&order0=$order\">Taille</a></th><td> </td>
    <th>".(($order=='date')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=date&asc=$asc&order0=$order\">Dernière modification</a></th><td> </td>
    <th>".(($order=='type')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=type&asc=$asc&order0=$order\">Type</a></th><td> </td>
    <th>".(($order=='ext')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=ext&asc=$asc&order0=$order\">Extention</a></th><td> </td>
    <th>".(($order=='perms')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=perms&asc=$asc&order0=$order\">Permissions</a></th><td> </td>
    <th>".(($order=='access')?(($asc=='a')?'/\\ ':'\\/ '):'')."<a href=\"$PHP_SELF?dir=".rawurlencode($cur). "&order=access&asc=$asc&order0=$order\">Dernier accès</a></th></tr>";
    foreach($tab_dir as $elem) {
      echo "<tr><td><img src=\"dir-close.gif\" /> ".$elem['name']."</td><td> </td>
      <td> </td><td> </td>
      <td>".date("d/m/Y H:i:s", $elem['date'])."</td><td> </td>
      <td>".assocType($elem['type'])."</td><td> </td>
      <td> </td><td> </td>
      <td>".$elem['perms']."</td><td> </td>
      <td>".date("d/m/Y", $elem['access'])."</td></tr>\n";
    }
    foreach($tab_file as $elem) {
      echo "<tr><td><img src=\"file-none.gif\" /> ".$elem['name']."</td><td> </td>
      <td align=\"right\">".formatSize($elem['size'])."</td><td> </td>
      <td>".date("d/m/Y H:i:s", $elem['date'])."</td><td> </td>
      <td>".assocType($elem['type'])."</td><td> </td>
      <td>".assocExt($elem['ext'])."</td><td> </td>
      <td>".$elem['perms']."</td><td> </td>
      <td>".date("d/m/Y", $elem['access'])."</td></tr>\n";
    }
    echo "</table>";
    closedir($dir);
  }
}

/* formatage de la taille */
function formatSize($s) {
  /* unités */
  $u = array('octets','Ko','Mo','Go','To');
  /* compteur de passages dans la boucle */
  $i = 0;
  /* nombre à afficher */
  $m = 0;
  /* division par 1024 */
  while($s >= 1) {
    $m = $s;
    $s /= 1024;
    $i++;
  }
  if(!$i) $i=1;
  $d = explode(".",$m);
  /* s'il y a des décimales */
  if($d[0] != $m) {
    $m = number_format($m, 2, ",", " ");
  }
  return $m." ".$u[$i-1];
}

/* formatage du type */
function assocType($type) {
  /* tableau de conversion */
  $t = array(
    'fifo' => "file",
    'char' => "fichier spécial en mode caractère",
    'dir' => "dossier",
    'block' => "fichier spécial en mode bloc",
    'link' => "lien symbolique",
    'file' => "fichier",
    'unknown' => "inconnu"
  );
  return $t[$type];
}

/* description de l'extention */
function assocExt($ext) {
  $e = array(
    '' => "inconnu",
    'doc' => "Microsoft Word",
    'docx' => "Microsoft Word",
    'xls' => "Microsoft Excel",
    'ppt' => "Microsoft Power Point",
    'pdf' => "Adobe Acrobat",
    'PDF' => "Adobe Acrobat",
    'zip' => "Archive WinZip",
    'txt' => "Document texte",
    'gif' => "Image GIF",
    'jpg' => "Image JPEG",
    'png' => "Image PNG",
    'php' => "Script PHP",
    'php3' => "Script PHP",
    'htm' => "Page web",
    'html' => "Page web",
    'css' => "Feuille de style",
    'js' => "JavaScript",
    'mp4' => "Vidéo",
    'eml' => "E-mail",
  );
  if(in_array($ext, array_keys($e))) {
    return $e[$ext];
  } else {
    return $e[''];
  }
}

function cmp_name($a,$b) {
    global $asc;
    if ($a['name'] == $b['name']) return 0;
    if($asc == 'a') {
        return ($a['name'] < $b['name']) ? -1 : 1;
    } else {
        return ($a['name'] > $b['name']) ? -1 : 1;    
    }
}
function cmp_size($a,$b) {
    global $asc;
    if ($a['size'] == $b['size']) return cmp_name($a,$b);
    if($asc == 'a') {
        return ($a['size'] < $b['size']) ? -1 : 1;
    } else {
        return ($a['size'] > $b['size']) ? -1 : 1;
    }
}
function cmp_date($a,$b) {
    global $asc;
    if ($a['date'] == $b['date']) return cmp_name($a,$b);
    if($asc == 'a') {
        return ($a['date'] < $b['date']) ? -1 : 1;
    } else {
        return ($a['date'] > $b['date']) ? -1 : 1;
    }
}
function cmp_access($a,$b) {
    global $asc;
    if ($a['access'] == $b['access']) return cmp_name($a,$b);
    if($asc == 'a') {
        return ($a['access'] < $b['access']) ? -1 : 1;
    } else {
        return ($a['access'] > $b['access']) ? -1 : 1;
    }
}
function cmp_perms($a,$b) {
    global $asc;
    if ($a['perms'] == $b['perms']) return cmp_name($a,$b);
    if($asc == 'a') {
        return ($a['perms'] < $b['perms']) ? -1 : 1;
    } else {
        return ($a['perms'] > $b['perms']) ? -1 : 1;
    }
}
function cmp_type($a,$b) {
    global $asc;
    if ($a['type'] == $b['type']) return cmp_name($a,$b);
    if($asc == 'a') {
        return ($a['type'] < $b['type']) ? -1 : 1;
    } else {
        return ($a['type'] > $b['type']) ? -1 : 1;
    }
}
function cmp_ext($a,$b) {
    global $asc;
    if ($a['ext'] == $b['ext']) return cmp_name($a,$b);
    if($asc == 'a') {
        return ($a['ext'] < $b['ext']) ? -1 : 1;
    } else {
        return ($a['ext'] > $b['ext']) ? -1 : 1;
    }
}
?>

<table border="1" cellspacing="0" cellpadding="10" bordercolor="gray">
<tr valign="top"><td>

<!-- liste des répertoires
et des sous-répertoires -->
<?php 
if(!in_array($order, array('name','date','size','perms','ext','access','type'))) {
  $order = 'name';
}
if(($order == $order0) && ($asc != 'b')) {
  $asc = 'b';
} else {
  $asc = 'a';
}
/* lien sur la racine */
if(!$dir) {
  echo "<img src=\"dir-open.gif\" /> /<br />\n";
} else {
  echo "<img src=\"dir-close.gif\" /> <a href=\"$PHP_SELF\">/</a><br />\n";
}
list_dir($BASE, rawurldecode($dir), 1); 
?>

</td><td>

<!-- liste des fichiers -->
<?php
/* répertoire initial à lister */
if(!$dir) {
  $dir = $BASE;
} 
list_file(rawurldecode($dir)); 
?>

</td></tr>
</table>

</body>
</html> 
0
jordane45 Messages postés 38136 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 16 avril 2024 344
Modifié le 16 juil. 2018 à 10:03
Bonjour,

Il faut commencer par vérifier que ce n'est pas . ou ..
if($base != "." && $base != ".." ) {





0
Rejoignez-nous