Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:14:56+00:00 2026-05-23T07:14:56+00:00

I wrote a PHP script to delete files selected in a gridview. This is

  • 0

I wrote a PHP script to delete files selected in a gridview. This is the first time I’ve done this. The script works fine on my local development machine but I don’t know if this is the proper way to do it. I’d like to find out what possible problems can I run into when deleting files and how can I modify this to prevent problems.

I was looking at this page to get the basic idea: http://www.php.net/manual/en/function.unlink.php

<?php
// get required includes
require_once(ROOT_PATH.'user/controls/snippets/error_messages.php');
require_once(ROOT_PATH.'user/controls/accordion/get_user_name.php');

// ------------------------------------------------------------
// DELETE SELECTED FILES
// ------------------------------------------------------------
if(isset($_POST['delete_file']) && isset($_POST['checked2']))
{
    $checked = array_map('intval',$_POST['checked2']);
    $delete_list = implode(", ", $checked);

    // DB: get file names to delete
    $get_file_names = mysqli_query($conn, "SELECT FileName FROM downloads WHERE DownloadId IN ($delete_list) AND UserName = '$user_name'")
    or die($dataaccess_error);

    // delete files from server
    while($row = mysqli_fetch_array($get_file_names))
    {
        $dir = DOWNLOAD_DIRECTORY;
        $file_name = $row['FileName'];
        $file_to_delete = $dir.$file_name;
        unlink($file_to_delete);
    }

    // DB: delete selected file references from db
    $delete_selected = mysqli_query($conn, "DELETE FROM downloads WHERE DownloadId IN ($delete_list) AND UserName = '$user_name'")
    or die($dataaccess_error);

    if(mysqli_affected_rows($conn) > 0)
    {
        $effected_rows = mysqli_affected_rows($conn);
        echo "<div class='msgBox2b noBorder'>SUCCESS: ($effected_rows) FILE(S) have been DELETED..</div>";
    }
}
elseif(isset($_POST['delete_file']) && !isset($_POST['checked2']))
{
    echo $msg_error;
}
?>

Thank you!

Edit: Would it be better this way?

$fh = fopen($file_to_delete, 'w') or die($failed_to_open_file);
fclose($fh);
unlink($file_to_delete);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:14:57+00:00Added an answer on May 23, 2026 at 7:14 am

    Not all files can be unlinked because of permissions, so check the return value of that call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this php script to delete old files older than 24 hrs, but
I wrote this PHP script to retrieve my form results via email. Everything works
I wrote a PHP script which will run every Monday using cron. In this
SOLVED: I wrote and tested a PHP script on the local server. (Nothing fancy,
This script I wrote works perfect on IE! It works on Chrome and FireFox
I just wrote a PHP login script, and what I'm trying to accomplish is
I am trying to write a PHP script that can upload multiple files. for($i=0;$i<count($_FILES['uploadimg']['name']);$i++){
I wrote a PHP code like this $site=http://www.google.com; $content = file_get_content($site); echo $content; But
Just a quick one - I wrote a php script recently that dynamically creates
i wrote a php script to generate random values between 1 and 99999999999999999999 (that's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.