treehouse : what would you like to learn today?
Web Design Web Development iOS Development

why unlink not working ?

  • i'm trying to delete file from link but its giving me error i just dont get it.

    <?php<br />
    function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
    }
    /* delete not working */
    function unlink if($_GET['action'] && $_GET['action'] == 'delete') {
    unlink($_GET['.$file']);
    header("$path."/".$file");
    exit();
    }

    //define the path as relative
    $path = "../uploads";

    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");

    echo " ";

    while ($file = readdir($dir_handle))
    {

    if($file!="$file.avi" && $file!="$file.srt")
    if($file!="." && $file!="..")
    if($file != "play.php")
    if($file != "index.html")

    echo $file . ': ' . formatBytes(filesize($path."/".$file), 2) . ' </a> -- <a href=\"$path."/"?action=delete&filename\=.$file\">delete file</a> <br/>';
    }


    //closing the directory
    closedir($dir_handle);

    ?>
  • well this much is not enough to get any conclusion.

    if you can shade some light on what is the error and format your code and post it i guess i can figure out.

    from what you have said issue could be...

    issue could be path issue.
    or
    issue could be because of folder rights if you are on windows server
  • You have a syntax error, for one.

    /* delete not working */ 
    // You missed the brackets for the function
    function unlink {
    if($_GET['action'] && $_GET['action'] == 'delete') {
    unlink($_GET['.$file']);
    header("$path."/".$file");
    exit();
    }
    }
  • I have to say that code is incredibly weird (to me at least).

    - Your unlink function doesn't look like a function. And if it was a function you should be calling it. Else it would be useless to create a function.
    - In your while loop you have ifs in ifs in ifs. You could just combine them all with && instead.
    - Also in your while loop you have
    if($file != "$file.avi" && $file!= "$file.srt")

    This will always return true.
    - In your echo, you're closing an anchor link without starting one. Doesn't make sense to me.


    Anyway, I think this is what your code should look like:

    <?php
    function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
    }
    function getExt($filename) {
    return substr($filename, strrpos($filename, '.') + 1);
    }

    $thisFile = $_SERVER['PHP_SELF'];

    //define the path as relative
    $path = "../uploads";

    /* delete not working */
    if(array_key_exists('action', $_GET) && $_GET['action'] == 'delete' && array_key_exists('filename', $_GET)) {
    $file = $_GET['filename'];
    unlink($path.'/'.$file);

    /* This doesn't make sense to me at all. Why would you TRY to redirect to a file you just deleted? (Notice I'm saying 'TRY' because you aren't redirecting but I guess that's what you were trying. */
    //header("$path."/".$file");
    //exit();

    // Redirecting to this file to clear out the params in the url.
    die(header("Location: $thisFile"));
    }

    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");

    echo " ";

    $hiddenFiles = array('.', '..', 'play.php', 'index.html');
    $allowedExts = array('avi', 'srt');

    while ($file = readdir($dir_handle))
    {
    $ext = getExt($file);

    if (in_array($file, $hiddenFiles) || !in_array($ext, $allowedExts))
    continue;

    echo $file . ': ' . formatBytes(filesize($path."/".$file), 2) . " -- <a href='$thisFile?action=delete&filename=$file'>delete file</a><br/>";
    }


    //closing the directory
    closedir($dir_handle);