SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: Using unlink

  1. #1
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using unlink

    I would like to be able to delete a selected file from a selected directory. Here's what I have so far:
    • a file that opens an image directory and displays each image and filename with a 'delete' link next to each.
    • each 'delete' link passes the directory and filename like so
    PHP Code:
    http://www.site.com/music/unlink.php?dir=genrepics&id=baroque.jpg 
    • the file 'unlink.php contains the following code:
    PHP Code:
    $dir "$_GET[dir]
     
        
    $handle opendir$dir ) ; 
        while( 
    $file readdir$handle ) )    { 
            if( 
    $file == "$_GET[id]
                
    unlink($file);
            
      }     
        
    closedir$handle ) ; 
    The trouble is when I pass values like the above example to unlink.php it returns the following:
    Warning: unlink(baroque.jpg) [function.unlink]: No such file or directory
    Anyone can help?

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Vancouver, USA
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like it's not getting the directory info. Is the script in the same directory as the image? If so, then are permissions right, if not, the problem is that the directory isn't being picked up.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yip noticed this myself - you've to go right back and include the full directory from where the script doing the file function exists, to the directory where your script is it's self.

    Something at the time I didn't know about; difficult lesson to learn.

  4. #4
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script is not in same directory, it's in the root and the images are in their own directories. The permissions are o.k. Here's what confuses me - another file used to browse the directory uses the following code to open the same directory and display the images and filenames:
    PHP Code:
    <?PHP 
    $dir 
    "$_GET[dir]
        
    $handle opendir$dir ) ; 
        while( 
    $file readdir$handle ) )    { 
            if( 
    $file == '.' || $file == '..' ) { 
                continue ; 
                if( 
    is_dir$dirname $file ) ) 
                
    list_dir$dirname $file '\\' ) ; 
            } 
            else { 
                echo( 
    $file ":<br />" "<img src=""\"$_GET[dir]\\" $file "\"" " />" "&nbsp;<a href=""\"unlink.php?dir=$_GET[dir]&id=$file\">[delete]</a><br />""<br /><br />" ); 
            } 
        }     
        
    closedir$handle ) ;
    This file is also in the root directory. Now the 'unlink.php' file uses very similar code:
    PHP Code:
    $dir "$_GET[dir]
     
        
    $handle opendir$dir ) ; 
        while( 
    $file readdir$handle ) )    { 
     
             if( 
    $file == "$_GET[id]
                
    unlink($file);
            }
      
     
    closedir$handle ) ; 
     
     if (
    unlink($file)) {
        echo(
    "$_GET[id]\ deleted successfully.");
     } 
      else {
          echo(
    "<br />Error deleting $_GET[id] from database.");
     } 
    But it reports 'Warning: unlink(baroque.jpg) [function.unlink]: No such file or directory '. I can see from this that it gets the filename correctly.
    Still stumped.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Vancouver, USA
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's getting the file name, but not the directory. Try unlink($dir/$file);

  6. #6
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mhalbrook
    It's getting the file name, but not the directory. Try unlink($dir/$file);
    Returns
    Warning: Division by zero

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Vancouver, USA
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FOrgot need " " around it.

  8. #8
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I put the quotes around it like this
    PHP Code:
    unlink("$dir/$file); 
    but it returns this
    Warning: unlink() [function.unlink]: No such file or directory
    I think the trouble is my misunderstanding of the following part of the 'while' portion of the code I posted that I'm using to browse the image directories. Specifically, this part
    PHP Code:
     if( $file == '.' || $file == '..' ) { 
    continue ; 
    if( 
    is_dir$dirname $file ) ) 
    list_dir$dirname $file '\\' ) ; 
    I said before that my 'unlink.php' page is apparently getting the filename because it is displayed with the warning, but I think it's only getting the filename from the querystring.
    Last edited by toma; May 25, 2003 at 04:03.

  9. #9
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Manual
    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
    The PHP manual recommends using
    PHP Code:
    while (false !== ($file readdir($handle))) { 
    instead.

    However, since what it looks like you're doing is checking for the existance of the file, you might want to use something like this:
    PHP Code:
    // If dir and id aren't set, don't try processing them.
    if ( (isset($_GET['dir'])) && (isset($_GET['id'])) ) {
        
    $dir basename($_GET['dir']);
        
    $path dirname($_SERVER['SCRIPT_FILENAME']) . '/' $dir;
        
    $file $path '/' basename($_GET['id']);

        if ( (
    is_dir($path)) && ($dir != ".") && ($dir != "..") ) {
        
    // Don't bother doing the file checks unless the directory checks out OK
            
    if ( (is_file($file)) && (is_writable($file)) ) {
                if (
    unlink($file) === FALSE) {
                    echo 
    "<p>The file system wouldn't allow us to delete the file.  This error should never occur and bears investigation.</p>";
                }
            } else {
                
    // This just helps debugging.  Change it to whatever you want.
                
    echo "<p>The script doesn't have write permissions for that file.</p>";
            }
        } else {
            
    // This just helps debugging.  Change it to whatever you want.
            
    echo "<p>File: {$file}</p>";
            echo 
    "<p>Security error: Access Denied.</p>";
        }
    } else {
        
    // Oops, you should never see this unless someone tried to load the url without arguments.
        
    echo "<p>Security error: Please specify a directory and file name.</p>";

    In case you're wondering, in most web servers, $_SERVER['DOCUMENT_ROOT'] is going to be your websites main directory. unlink requires the file to be in the current directory *or* to have the full directory supplied, including the path to the http root.

    basename removes any additional directory parts someone might try to add on to the file name.

    is_file is the complement to is_dir. It makes sure that the file is a regular file, not a directory.

    is_writable checks the file permissions and makes sure the file can be deleted.

    Edit:
    OK, now the script uses the directory that it is in as the base instead of document_root.
    Last edited by Powerlord; May 25, 2003 at 05:39.

  10. #10
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Powerlord
    The PHP manual recommends using
    PHP Code:
    while (false !== ($file readdir($handle))) { 
    instead.

    However, since what it looks like you're doing is checking for the existance of the file, you might want to use something like:
    PHP Code:
    $dir $_SERVER['DOCUMENT_ROOT'] . '/' $_GET['dir'];
    $file $dir '/' basename($_GET['id']);

    if ( (
    is_dir($dir)) && ($dir != "." ) && ($dir != ".." ) ) {
    // Don't do the following checks unless the directory checks out OK
    if ( (is_writable($file)) && (is_file($file)) ) {
    unlink($file);
    }

    In case you're wondering, in most web servers, $_SERVER['DOCUMENT_ROOT'] is going to be your websites main directory. unlink requires the file to be in the current directory *or* to have the full directory supplied.
    Thanks alot. This looks promising, but when I ran it the file was not deleted. There are no errors or warnings produced but if I look in the directory all files are unchanged (I refreshed to be sure). I used ech to check to $dir variable and it is set to
    home/sser/public_html/sschol/genrepics
    which needs to be
    home/sser/public_html/sschol/dms/asys/genrepics
    I'd rather not hard code those directories if possible.

    Edit: I saw your new post and tried it. It produced
    Security error: Access Denied.

  11. #11
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toma
    Thanks alot. This looks promising, but when I ran it the file was not deleted. There are no errors or warnings produced but if I look in the directory all files are unchanged (I refreshed to be sure). I used ech to check to $dir variable and it is set to
    which needs to be
    I'd rather not hard code those directories if possible.
    My above code is still a work in progress. In fact, I've edited it during the time you were posting the above message.

    Actually, is the script in the
    /home/sser/public_html/sschol/dms/asys directory? If so, it might be possible to take the directory that the script is in and use that as a base... in fact, I've already changed the script above to use it.

  12. #12
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Powerlord
    My above code is still a work in progress. In fact, I've edited it during the time you were posting the above message.

    Actually, is the script in the
    /home/sser/public_html/sschol/dms/asys directory? If so, it might be possible to take the directory that the script is in and use that as a base... in fact, I've already changed the script above to use it.
    Yes the unlink.php file is in this directory. The files it is attempting to process are in their own directory in this directory. Hope that is clear.

    Edit: I notice that even if the directory is correct when I echo it the file passed in the query is not deleted.

  13. #13
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toma
    Yes the unlink.php file is in this directory. The files it is attempting to process are in their own directory in this directory. Hope that is clear.

    Edit: I notice that even if the directory is correct when I echo it the file passed in the query is not deleted.
    OK, I think I finally got everything set up to use the correct directories.

    I do find it odd that it still doesn't delete the files if you supply the correct directories. Does it still echo back an error from unlink?

    Update: I changed the error messages so that they are different for each "class" of error. If it returns the "Security error: Please specify a directory and file name" every time you attempt to run the script, it's possible that I'm trying to use the new PHP arrays and you're running a version of PHP that doesn't support them.

  14. #14
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Powerlord
    OK, I think I finally got everything set up to use the correct directories.

    I do find it odd that it still doesn't delete the files if you supply the correct directories. Does it still echo back an error from unlink?
    It displays this
    Security error: Access Denied.

  15. #15
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, here's a version that *should* work with all versions of PHP4

    PHP Code:
    // If dir and id aren't set, don't try processing them.
    if ( (isset($HTTP_GET_VARS['dir'])) && (isset($HTTP_GET_VARS['id'])) ) {
        
    $dir basename($HTTP_GET_VARS['dir']);
        
    $path getcwd() . '/' $dir;
        
    $file $path '/' basename($HTTP_GET_VARS['id']);

        if ( (
    is_dir($path)) && ($dir != ".") && ($dir != "..") ) {
        
    // Don't bother doing the file checks unless the directory checks out OK
            
    if ( (is_file($file)) && (is_writable($file)) ) {
                if (
    unlink($file) === FALSE) {
                    echo 
    "<p>The file system wouldn't allow us to delete the file.  This error should never occur and bears investigation.</p>";
                }
            } else {
                
    // This just helps debugging.  Change it to whatever you want.
                
    echo "<p>The script doesn't have write permissions for that file.</p>";
            }
        } else {
            
    // This just helps debugging.  Change it to whatever you want.
            
    echo "<p>Security error: Access Denied.</p>";
            echo 
    "<p>File: {$file}</p>";
        }
    } else {
        
    // Oops, you should never see this unless someone tried to load the url without arguments.
        
    echo "<p>Security error: Please specify a directory and file name.</p>";

    If this still doesn't work, I can go back and add more debugging information.

  16. #16
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Powerlord
    OK, here's a version that *should* work with all versions of PHP4
    PHP Code:
    // If dir and id aren't set, don't try processing them.
    if ( (isset($HTTP_GET_VARS['dir'])) && (isset($HTTP_GET_VARS['id'])) ) {
    $dir basename($HTTP_GET_VARS['dir']);
    $path getcwd() . '/' $dir;
    $file $path '/' basename($HTTP_GET_VARS['id']);

    if ( (
    is_dir($path)) && ($dir != "." ) && ($dir != ".." ) ) {
    // Don't bother doing the file checks unless the directory checks out OK
    if ( (is_file($file)) && (is_writable($file)) ) {
    if (
    unlink($file) === FALSE) {
    echo 
    "<p>The file system wouldn't allow us to delete the file. This error should never occur and bears investigation.</p>";
    }
    } else {
    // This just helps debugging. Change it to whatever you want.
    echo "<p>The script doesn't have write permissions for that file.</p>";
    }
    } else {
    // This just helps debugging. Change it to whatever you want.
    echo "<p>Security error: Access Denied.</p>";
    echo 
    "<p>File: {$file}</p>";
    }
    } else {
    // Oops, you should never see this unless someone tried to load the url without arguments.
    echo "<p>Security error: Please specify a directory and file name.</p>";

    If this still doesn't work, I can go back and add more debugging information.
    This, in fact, works! It goes without saying I'd never come up with that. Thank you.


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
  •