please someone give me a code to delete a file in a folder, ie one code to be used on a variety of file deletion

Dani AI

Generated

A short, practical follow-up to the thread: reported a parse error on the unlink line, posted a minimal example and suggested using an SPL iterator for bulk operations. The snippet below gives a safer, reusable single-function approach that avoids directory-traversal, checks permissions, and returns structured error info so you can diagnose failures without guessing.

function safe_delete($baseDir, $relativePath) {
    $base = rtrim($baseDir, '/\\') . DIRECTORY_SEPARATOR;
    $realBase = realpath($base);
    if ($realBase === false) return ['ok'=>false,'error'=>'base_not_found'];

    $candidate = $base . ltrim($relativePath, '/\\');
    $realDir = realpath(dirname($candidate));
    if ($realDir === false || strpos($realDir, $realBase) !== 0) {
        return ['ok'=>false,'error'=>'invalid_path'];
    }

    $full = $realDir . DIRECTORY_SEPARATOR . basename($candidate);
    if (!is_file($full)) return ['ok'=>false,'error'=>'not_a_file'];
    if (!is_writable($realDir)) return ['ok'=>false,'error'=>'dir_not_writable'];

    if (!@unlink($full)) {
        return ['ok'=>false,'error'=>'unlink_failed','info'=>error_get_last()];
    }
    return ['ok'=>true];
}

Quick troubleshooting checklist for the parse error and deletion failures:

  • Look at the exact PHP error message (enable temporary reporting with ini_set('display_errors',1); error_reporting(E_ALL);) or check the server PHP error log.
  • Parse errors are usually caused by a missing semicolon, unmatched braces/parentheses, or PHP tags not opened/closed — check the line before the reported error.
  • Confirm filesystem permissions: on Unix you need write permission on the containing directory (not just the file) to remove a file.
  • If unlink fails but no permission issue, use error_get_last() (as shown) to get the underlying message. See the PHP manual for unlink and realpath for details.

Safer practice: for user-driven deletes, validate input against a whitelist or move files to a "trash" folder with rename() before permanent removal so accidental deletes can be recovered.

Recommended Answers

All 5 Replies

if the file is on the server-

if (file_exists("folder/myfile.xlsx"))
			unlink("folder/myfile.xlsx");

if the file is on the server-

if (file_exists("folder/myfile.xlsx"))
			unlink("folder/myfile.xlsx");

Give Op Something to work on instead of straight code. That way they won't learn.
That being said, I would suggest you check SPL's directory iterator. There are couple of examples under comments

I have no problem helping others..I'm not a teacher.

brings a parse error on the unlink line

I have no problem helping others..I'm not a teacher.

doing for them is not helping them it is making them more lame and dependant on somebody

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.