'Undefined offset PHP' in a loop "for"

-2

I'm trying to organize some folders in order in an application that I'm doing for FTP. Here is the code I'm using:

    if ($file->isDir()) {
                    echo '<table>';
                    $archive = [$file];
                    for($i = 0; $i <= $archive; $i++){
                        echo '<tr></tr>';
                        echo '<td><a href="index.php?path='.$dir. DIRECTORY_SEPARATOR .$archive[$i].'"><img src="icones/archive.ico" width="30px" height="30px"/></a></td>' . $file->getBaseName();
                    }       
                    echo '</table>';
                } else {
                    switch($file->getExtension()) {
  Restante do código...

Error appearing on screen:

Undefined offset: 1

This error goes down in an infinite loop.

    
asked by anonymous 25.04.2014 / 14:11

2 answers

2

Your current code has a number of issues with using variables, arrays, objects, and HTML. The suggestion to solve each of the subjects is extensive and lacks the rest of the code that you use until you reach the excerpt that appears in your question.

Some of the problems visible in your question:

  • $file is an object, but you are passing it as the key of the array $archive ;

  • You are outputting for() and only then do you output $archive . The <tr></tr> must be within <td></td> .

  • The TD array is working as if it has numeric keys which is what the TR contains. (see point 1);

  • You are concatenating $archive after closing $i . It would have to be concatenated before the $file->getBaseName() is closed;

  • If there are no results, you will have a table without content;

  • List Files and Directories

    As far as I understand, you want to list files and directories of a particular path to display to the user. Below is a working example that illustrates this:

    <?php
    
    $outputHtml = '';
    
    $pathToList = dirname(__FILE__);            // caminho a listar
    $dir = new DirectoryIterator($pathToList);  // Iterar o caminho
    
    // por cada entrada encontrada
    foreach ($dir as $fileinfo) {
    
      // se não for "." ou ".."
      if (!$fileinfo->isDot()) {
    
        /* Verificar se é directoria ou ficheiro
         * para definir o icon a utilizar
         */
        if ($fileinfo->isDir()) {
          $icon = 'https://cdn2.iconfinder.com/data/icons/snipicons/5000/folder-close-32.png';
        }
        else {
          $icon = 'https://cdn2.iconfinder.com/data/icons/snipicons/500/file-32.png';
        }
    
        /* actualizar a variável de output com o HTML
         * referente a este ficheiro/directoria
         */    
        $outputHtml.= '
        <div>
          <a href="index.php?path='.$fileinfo->getPathname().'">
            <img src="'.$icon.'" width="32px" height="32px"/> '.$fileinfo->getFilename().'
          </a>
        </div>';
      }
    }
    
    /* Apresentar a listagem ou se não temos
     * resultados apresentar uma mensagem.
     */
    if (!empty($outputHtml)) {
      echo $outputHtml;
    }
    else {
      echo '<p>Não foram encontrados ficheiros ou directorias para o caminho indicado!</p>';
    }
    
    ?>
    

    The code above will give you a listing similar to the screenshot below:

    In short, what is being done is:

  • Define the path to list in the TD variable:

    $pathToList = "/caminho/para/destino/*";
    
  • Using the method TD (English) we have built an iteration of the directory:

    $dir = new DirectoryIterator($pathToList);
    
  • For each localized entry, we perform the desired actions. In the example above we are listing localized content by assigning an icon to when it is a file and another icon for when it is a directory.

    foreach ($dir as $fileinfo) {
      ...
    }
    
  • We present the result to the user if we have found something, if not, we present a message.

  • 19.06.2014 / 18:43
    1

    You have a array in your for - this will not to function. Try this:

    for($i = 0; $i <= $file; $i++) {
        // o seu código aqui
    }
    

    If it still is not, let's think: you have a condition $file->isDir() , which means that $file has no more than one value - is to be an object - and you are >, which does not make sense .

    In more technical terms, you are converting a singular object to a vector, and for for this is useless and will not work.

        
    25.04.2014 / 14:21