Jump to content

Recommended Posts

I am making a basic cms for a gallery script, I have a list of thumbnails (generated from an ajax script) with a delete link next to each thumb like so...

 

          <ul class="thumb-list">

            <li><img src="images/thumbs/000000001.jpg"><a href="delete.php?file=000000001.jpg">delete</a></li>

            <li><img src="images/thumbs/000000002.jpg"><a href="delete.php?file=000000002.jpg">delete</a></li>

            etc...

          </ul>

 

I have found this script which I think is what I need to use??

<?php

$filename = "myfile.txt";

unlink($filename);

?>

 

ideally what I want to do is have the user click the delete link, a prompt displays - "are you sure?" the thumb and the large image are deleted and the list of thumbnails is reloaded.

 

the thumbs and the large images have the same file name but are in different folders..

 

/images - large images

 

/images/thumbs - thumbnails

 

any pointers appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/239503-noob-unlink-basics/
Share on other sites

If you want some sort of prompt you will probably need Javascript. In fact what you described you seem to want to accomplish without having to refresh the page. If so you are going to need to use AJAX for this. If you are unfamiliar with AJAX/Javascript or how it can alter the HTML DOM you have some reading to do. I personally suggest using the Jquery library if you are up to learning about it. Makes developing these kinds of things much easier.

 

But to address the first part of your question. With your links, youare sending a get variable. This variable is called file, and can be reached via the $_GET super global. Combining this with the second snippet you posted, you can do something like

//this is delete.php
$filename = $_GET['file'];
unlink($filename);

 

Note: this doesn't validate or sanitize your $_GET['file'] variable. Now we have to address the fact that you have images stored in an image directory, and you have two images to delete (one in /images and one in images/thumbnail) we address this with the following

$filename = $_GET['file'];
$img = "images/$filename";
$thumb = "images/thumbnails/$filename";

unlink(img);
unlink($thumb);

 

Again, I do not sanitize or validate the $_GET data. You must remember to do this, especially if you are deleting files.

 

 

How, hooking this simple script up with AJAX is something beyond the scope of a simple topic post if you don't know anything about ajax/javascript. I suggest doing a few google searches on AJAX/Javascript of Jquery if you feel up to it

 

 

Hope this helps

 

Link to comment
https://forums.phpfreaks.com/topic/239503-noob-unlink-basics/#findComment-1230307
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.