[Php-it] Directory corrente sulla root

Alessandro Marinuzzi alecos_net at libero.it
Sat Oct 6 13:16:28 CEST 2007


Cristiano Verondini ha scritto:
> On 06/ott/07, at 08:14, Alessandro Marinuzzi wrote:
>   
>>> web/www.sitoweb.it/home/script.php
>>>
>>> la root in questo caso è home ma appena metto lo script lì non mi da
>>> nulla, pagina bianca! mentre se lo metto in:
>>>
>>> web/www.sitoweb.it/home/dir/script.php
>>>
>>> lo script restituisce dir
>>>
>>> Come aggirare? e come ottenere solo il nome della root?
>>>       
> 	Forse non ho capito quello che intendi, ma nel primo caso mi  
> verrebbe da dire che il path è home/ mentre nel secondo home/dir/
>   
la home corrisponde a / quindi non mi restituisce home ma bensì un bel
nulla...
>> ho trovato una soluzione che non mi piace ma funziona:
>>
>> <?php
>>   $dir = basename(dirname($_SERVER['PHP_SELF']),"/");
>>   if ($dir == "") {
>>     $path = "root";
>>   } else {
>>     $path = basename(dirname($_SERVER['PHP_SELF']),"/");
>>   }
>>   echo $path;
>> ?>
>>
>> ce ne sono di migliori? Ciao e grazie :-)
>>     
>
> 	Io lavorerei su __FILE__
>   
provata anche questa strada... vengo al dunque: ho uno script che genera
un archivio tar tramite una classe e vorrei aggiungere il nome della
directory all'interno dell'archivio in base alla directory dove risiede
lo script.

<?php  
  ini_set('memory_limit', '-1');
  ini_set('register_globals', 0);
  ini_set('max_execution_time', 0);
  //=============================================================
  $rel_path = basename(dirname($_SERVER['PHP_SELF']),"/");
  if ($rel_path == "") {
    $cwd_path = $_SERVER['HTTP_HOST'];
  } else {
    $cwd_path = basename(dirname($_SERVER['PHP_SELF']),"/");
  }
  //=============================================================
  $password = "changeme";
  //=============================================================
  $tar_file = "$cwd_path.tar";
  //=============================================================
  if (!isset($_POST['password'])) {
    echo 'FORM HTML';
  } else {
    if ($_POST['password'] == $password) {
      Class Tar_Archive {
        var $tar_file;
        var $fp;
        function Tar_Archive($tar_file) {
          $this->tar_file = $tar_file;
          $this->fp = fopen($this->tar_file, "wb");
          $tree = $this->build_tree();
          $this->process_tree($tree);
          fputs($this->fp, pack("a512", ""));
          fclose($this->fp);
          header("Pragma: public");
          header("Expires: 0");
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
          header("Cache-Control: private",false);
          header("Content-Type: application/octet-stream");
          header("Content-Disposition: attachment; filename=".basename($tar_file).";");
          header("Content-Transfer-Encoding: binary");
          header("Content-Length: ".filesize($tar_file));
          readfile($tar_file);
          unlink($tar_file);
        }
        function build_tree($dir = '.') {
          $handle = opendir($dir);
          while(false !== ($readdir = readdir($handle))) {
            if ($readdir != '.' && $readdir != '..') {
              $path = $dir.'/'.$readdir;
              if (!is_link($path)) {
                if (is_file($path)) {
                  $output[] = substr($path, 2, strlen($path));
                } elseif (is_dir($path)) {
                  $output[] = substr($path, 2, strlen($path)).'/';
                  $output = array_merge($output, $this->build_tree($path));
                }
              }
            }
          }
          closedir($handle);
          return $output;
        }
        function process_tree($tree) {
          foreach($tree as $pathfile) {
            if (substr($pathfile, -1, 1) == '/') {
              fputs($this->fp, $this->build_header($pathfile));
            } elseif ($pathfile != $this->tar_file) {
              $filesize = filesize($pathfile);
              $block_len = 512*ceil($filesize/512)-$filesize;
              fputs($this->fp, $this->build_header($pathfile));
              fputs($this->fp, file_get_contents($pathfile));
              fputs($this->fp, pack("a".$block_len, ""));
            }
          }
          return true;
        }
        function build_header($pathfile) {
          if (strlen($pathfile) > 99) die('Error');
            $info = stat($pathfile);
            if (is_dir($pathfile)) $info[7] = 0;
              $header = pack("a100a8a8a8a12A12a8a1a100a255", $pathfile, sprintf("%6s ", decoct($info[2])), sprintf("%6s ", decoct($info[4])), sprintf("%6s ", decoct($info[5])), sprintf("%11s ",decoct($info[7])), sprintf("%11s", decoct($info[9])), sprintf("%8s", " "), (is_dir($pathfile) ? "5" : "0"), "", "");
              clearstatcache();
              $checksum = 0;
              for ($i = 0; $i < 512; $i++) {
                $checksum += ord(substr($header,$i,1));
              }
              $checksum_data = pack("a8", sprintf("%6s ", decoct($checksum)));
              for ($i = 0, $j = 148; $i < 7; $i++, $j++) {
                $header[$j] = $checksum_data[$i];
              }  
              return $header;
        }
      }
      $tar = & new Tar_Archive("$tar_file");
    } else {
      echo 'FORM HTML';
    }
  }
?>

Con queste modifiche ottengo un archivio in base alla dir dove si trova
ma all'interno dell'archivio manca la dir radice in pratica l'archivio
non viene scompattato in una diretory ma bensì vengono scompattati tutti
i file e dir nello 'spazio libero' mentre io invece vorrei una
situazione di questo tipo:

archivio.tar => dir dinamica => tutti i file e dir

invece ho:

archivio.tar => tutti i file e dir

spero di essermi spiegato... avevo provato a fare una cosa sporca come
questa:

$tree = $this->build_tree(getcwd());

ma funziona solo sotto windows e con molte perplessità....

-- 
Alessandro Marinuzzi
--------------------
http://www.alecos.it
--------------------

-------------- parte successiva --------------
Un allegato HTML ? stato rimosso...
URL: http://lists.ziobudda.net/pipermail/php-it/attachments/20071006/708534e7/attachment.htm 


More information about the Php-it mailing list