Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End why unlink not working ?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #29992
    greenbucket
    Member

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

    
    
    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);

    ?>
    #81753
    john010117
    Member

    You have a syntax error, for one.

    /* delete not working */
    // You missed the brackets for the function
    function unlink {
    if($_GET && $_GET == 'delete') {
    unlink($_GET);
    header("$path."/".$file");
    exit();
    }
    }
    #81494
    MrSoundless
    Member

    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:

    
    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;

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

    /* delete not working */
    if(array_key_exists('action', $_GET) && $_GET == 'delete' && array_key_exists('filename', $_GET)) {
    $file = $_GET;
    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) . " -- delete file
    ";
    }


    //closing the directory
    closedir($dir_handle);
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.