I am trying to create a form like the one we ask questions in, but with image & video Upload facility.
I am making Admin Support pages for a CMS. When the admin uploads an image, the name of the uploaded file can be seen in the textarea and the Image will be become visible on the customer pages.

After the file's usefullness, the admin may decide to remove the file from his webpage and deletes the link from his text box.
When that happens, the file will not be shown on the admin/user pages, but it still exists in my server.
How do I delete such useless files?
If you can find a way for the useless files to be deleted as the Admin deletes the links, it will be very helpful.

For a better understanding, checkout stackoverflow.com question form.

Dani AI

Generated

As described, the core problem is not deleting files (that is possible) but reliably detecting when an uploaded file is truly unused. pointed to direct deletion; the missing piece is a safe, auditable workflow that avoids accidental loss and handles legacy content.

Treat uploads as first‑class attachments. On upload insert an uploads row (id, path, checksum, uploaded_at, ref_count, last_referenced, status). Render content with attachment IDs or a small token (for example [[file:123]] or an attachment handler URL) instead of embedding raw filenames. When a post is saved, extract referenced IDs, compute the diff against the previous save, and update reference counts. Example diff logic:

$old = extractAttachmentIds($oldHtml);
$new = extractAttachmentIds($newHtml);
$added = array_diff($new, $old);
$removed = array_diff($old, $new);

foreach ($added as $id) incrementRef($id);
foreach ($removed as $id) decrementRef($id);

For existing posts that contain filenames/URLs, parse the HTML to build a usage map, normalize paths, and map them to upload records. DOM parsing is safer than regex for this: extract src/href, strip query strings, and canonicalize before comparison.

$dom = new DOMDocument();
@$dom->loadHTML($html);
$refs = [];
foreach ($dom->getElementsByTagName('img') as $img) {
    $refs[] = normalizePath($img->getAttribute('src'));
}

Never remove files immediately when links are deleted. Mark candidates as orphaned, move them to a trash/ area, and run a scheduled garbage collector that purges files only after a retention period (e.g., 30 days). To find purge candidates:

SELECT id, path FROM uploads
WHERE ref_count = 0 AND uploaded_at < DATE_SUB(NOW(), INTERVAL 30 DAY);

Provide an admin review UI for orphans, keep logs, and keep backups during the migration. This approach prevents accidental data loss and scales safely across many posts.

Recommended Answers

All 2 Replies

Well if you have stored information about where these files are saved with your link, I guess you could retrieve this information when you delete the link, and delete the related files with it?

You can use the unlink() function for deleting files :).

Its not that simple. Have a look at the stackoverflow.com question form. YOu will get a better idea.
The solution is not that simple, or else I would have got it by now. :P

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.