Jump to content

Recommended Posts

OK, I'm doing an image hoster, as many of you know, and I've been trying for a number of days to get a delete function working. The problem is, I can't get the delete.php page to actually do anything. So, I changed my method of deleting from a link next to each picture to a checkbox system. Here's the code that shows all the directory's files with the checkboxes next to them:

 

<?
$handle = @opendir($f_user);

if(!empty($handle))
{
while(false !== ($file = readdir($handle)))
{
if(is_file($f_user . "/" . $file))
echo '<a href="'. $f_user .'/'. $file .'">'. $file .'</a><input type=checkbox name="'.$file.'" value="delete"><br><br>';
}
}

closedir($handle);
?>

 

What I want to know is, when the user clicks 'delete selected' (not shown in above code), how do I get said files to delete? I can't say 'unlink(filename);' because filename is $file, and due to the loop, $file applies to everything. Soo, how do I define the checkboxes to unlink? I've been told cookies are one method, but I can't figure out how to do them, and also that I could use GET, although I'm equally as stuck on that, because I'm not submitting any real information, therefore I can't do anything with it, so I'd have to 'GET' the file from the URL. If you want to see a working example:

 

http://www.reflexprojects.net/rs/upload/secure_page.php - Username: test, Password: test

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/
Share on other sites

Sure you can use get. Its actually rather easy.

 

This is what I would do. I'm a bit confused what you want to do, but I'll give it a shot.

 

url: website.com/delete.php?id=4

 

<?php

 

$id = $_GET['id'];

 

$directory = "files/images/"; // directory to all images

$ext = "jpg"; //extension of image

$file = "$id.$ext"; // uses the id of the image and adds a dot and the extension

 

unlink ($directory . $file);

 

?>

 

The problem would happen when $ext is different for every file. You would then have to create a database driven thing, so once a user uploads a file you use php to get the extension of that file and put it in a column. Then use select mysql function and mysql_fetch_assoc to get the extension...

 

And then it should work!!

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-339937
Share on other sites

Instead of an id pass the filename in the url. It may be a security concern so a better way would be to pass it encrypted. Ex:

 

$urlVar = md5('filename.jpg');
echo "<a href=\"page.php?delete={$urlVar}\" />

 

$fileURL = $_GET['delete'];
//loop through the file
    if($fileURL = md5($file)){ //where $file is the filename got from the folder
        unlink($file);
    }
//loop end

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-339957
Share on other sites

Instead of an id pass the filename in the url. It may be a security concern so a better way would be to pass it encrypted. Ex:

 

$urlVar = md5('filename.jpg');
echo "<a href=\"page.php?delete={$urlVar}\" />

 

$fileURL = $_GET['delete'];
//loop through the file
    if($fileURL = md5($file)){ //where $file is the filename got from the folder
        unlink($file);
    }
//loop end

 

How code it be a security risk if only an adminstrator has the right to access the delete.php otherwise it throughs a 403 Forbidden? I mean seriously, don't do any extra work when not necessary.

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-339959
Share on other sites

How code it be a security risk if only an adminstrator has the right to access the delete.php otherwise it throughs a 403 Forbidden? I mean seriously, don't do any extra work when not necessary.

 

I didnt know that and i still cant read it somewhere. If it is really for the admin back end then yeah u really dont need such fancy stuff, but otherwise consider it.

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-339961
Share on other sites

ok, I've got a url like

http://www.reflexprojects.net/rs/upload/delete.php?delete=file.png

but I can't GET it on the next page. I think the confusion is that the images are stored in a folder (which is named whatever the user's username is).

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-340009
Share on other sites

You need to do preliminary planning. Because:

 

1) Not smart to create a folder for each user

2) Can't use the GET in an easy way if each image is named something different (instead name by ids, like I said before)

3) Need to use Mysql to store image title and extension details

 

 

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-340018
Share on other sites

1) why not? Look at CuteNews

2) "

3) "

 

Well then don't listen to me. I'm just trying to help.

 

Second of all, I did a similar project to what you are doing LAST NIGHT. Instead of images it was documents.

 

I used mysql and had each document auto increment.

 

You can scratch off number 1 since I didn't do different users. But it was still very similar. The reason you use mysql is because when you want to delete a file already on the server, you can't easily get the extension of the name by simply knowing the id of the file. So you bring it from the mysql and update the extension after you upload a new image. You also use a database for title, author details, and all that jazz.

 

FINALLY, you MUST name images by auto increment to show all image links on one page:

 

// Select whole table
$result = mysql_query("SELECT * FROM docs");

// Echo preliminary html
echo "<ul>";

// Initiate Loop
while ( $row = mysql_fetch_assoc( $result )) {

$id_page = $row['id'];
$title = $row['title'];

// Echo the results
echo "<li><a href=\"doc.php?id=$id_page\">$title</a></li>";

// Echo ending html
echo "</ul>";

}

 

I can give you a link to what I created last night. Its already on the internet and it would be a good start for your project. Reply back and I'll pm you.

 

 

Link to comment
https://forums.phpfreaks.com/topic/67673-solved-cookieget-help/#findComment-340030
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.