SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Checkboxes and arrays!!!

    Hi all,
    i've some problem with a script that should read the content of a folder and print the content in a table, and near each files name , i display a checkboxes that if checked and pressed the button delete it will delete from the server the file.
    Now this is the file taht read and print the result:

    PHP Code:
    <?php
    function listFiles($dir,$type) { 
        if (
    strlen($type) == 0$type "all"
        
    $x 0
        if(
    is_dir($dir)) { 
            
    $thisdir dir($dir); 
            while(
    $entry=$thisdir->read()) { 
                if((
    $entry!='.')&&($entry!='..')) { 
                    if (
    $type == "all") { 
                        
    $result[$x] = $entry
                        
    $x++; 
                        
    next
                    } 
                    
    $isFile is_file("$dir$entry"); 
                    
    $isDir is_dir("$dir$entry"); 
                    if ((
    $type == "files") && ($isFile)) { 
                        
    $result[$x] = $entry
                        
    $x++; 
                        
    next
                    } 
                    if ((
    $type == "dir") && ($isDir)) { 
                        
    $result[$x] = $entry
                        
    $x++; 
                        
    next
                    } 
                    
    $temp explode("."$entry); 
                    if ((
    $type == "noext") && (strlen($temp[count($temp) - 1]) == 0)) { 
                        
    $result[$x] = $entry
                        
    $x++; 
                        
    next
                    } 
                    if ((
    $isFile) && (strtolower($type) == strtolower($temp[count($temp) - 1]))) { 
                        
    $result[$x] = $entry
                        
    $x++; 
                        
    next
                    } 
                } 
            } 
        } 
        return 
    $result

    /* directory e estensione files */
    $files listFiles("./","jpg"); 
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Download</title>
    <style type="text/css">
    td { 
        font- family:Verdana,Geneva,Arial,Helvetica,sa
    ns-serif;
        font-size:11px
    }
    </style>
    </head>
    <body bgcolor="#ffffff" link="#ff8000" vlink="#00659C" alink="#00659C">
    <form action="cancella.php" method=\"POST\">
    <table cellpadding='1' cellspacing='0' border='0' bgcolor='black' width='552' align='center'>
     <tr>
      <td><br>  
    <?php
    if (isset($files)) { 
        echo 
    "<font color=white>Numero di file(s): <b>".count($files)."</b></font><br><br>";  
        
    $cols 2;
        
    $width round(100 $cols);
        
    $ret "<table border=0 cellspacing=2 width=\"100%\" bgcolor=white><tr><td valign=top width=\"$width%\">";
        
    $numtot count($files);
        
    $numto 1.0 $cols;
        
    $step 1.0 $numtot;
        
    $numfile 0.0;
        foreach (
    $files as $value) { 
            
    $numfile += $step;
            
    $ret .= "<input type=\"checkbox\" name=\"del[]\" id=\"$value\">  $value  <a href=\"http://mysite.com/upload/$value\" target=\"_new\">Guarda</a><br>";// <== modifica
            
    if (($numfile+0.0000001)>=$numto) {
                    
    $ret.= "</td><td valign=\"top\" width=\"$width%\">";
                    
    $numfile -= $numto;
            }
        }
        
    $ret ereg_replace("</td><td valign=\"top\" width=\"$width%\">$","",$ret);
        
    $ret .= "</td></tr></table>$files";
        echo 
    $ret;
    }
    ?>
      </td>
     </tr>
    </table><br>
    <center><INPUT type="SUBMIT"  value="Cancella"></center>
    </form>
    </body>
    </html>
    And this is the file cancella.php:

    PHP Code:
    foreach($_POST['del'] as $path => $key
       if(
    $keyunlink($path); 
    But when i go to press the button "Cancella", it returns to me:

    Warning: Invalid argument supplied for foreach()
    I've read that this is beacuse the array $POST is NULl, bot after all, how can i solve this problem?!?!

    byez all

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might be confused because of the escaped quotes here: method=\"POST\"

    Try removing the \'s.
    John

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK , now it works!!!!

    But when the script call the function unlink(); it print out:
    Warning: unlink() failed (No such file or directory)

    Because i don't konw how to re-call the variable $value that is the name of the file.How can i get this variable from the array $_POST ???

    thank you and bye

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $_POST['del'] is not an associative array. You would go about deleting the files this way:
    PHP Code:
    foreach( $_POST['del'] as $fileToDelete)
      
    unlink$fileToDelete ); 
    John

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've modified the file to this:

    PHP Code:
    foreach($_POST['del'] as $file
       
    unlink($file); 
        
    var_dump($file); 
    And it return to me:

    Warning: unlink() failed (No such file or directory) in /home/mysite/www/upload/cancella.php on line 4
    string(2) "on"

    It seems that there isn't the variable $value in the array $_POST

    How to solve it?

    thanks again

  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do this instead, for now:
    PHP Code:
    foreach( $_POST['del'] as $file )
      echo 
    $file.'<br />'
    John

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've checked 4 box and it returns:

    on
    on
    on
    on


    And now??
    thanks in advice

  8. #8
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    You need to set the values in your checkboxes, like this:

    <input type='checkbox' name='del[]' value='$value' id='$value' />$value
    John

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP John


    You need to set the values in your checkboxes, like this:

    <input type='checkbox' name='del[]' value='$value' id='$value' />$value
    I'm stupid men ihihihi

    Thank you very much John, they are my first posts in this community and y're now my guru

    Thank you !!!!!

  10. #10
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are most welcome.
    John


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •