Jump to content

Delete function on uploader


LemonInflux

Recommended Posts

I have built an image uploader, but with logins etc. etc. for people to have their own areas. However, it suddenly dawned on me, there is no way to actually delete files. I tried to think of a way, but I can't think of any way to do it. NOTE: This is flat, no SQL. Here's the core code for the uploader:

 

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = $f_user . "/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>

<center><form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<input type="file" name="image_file" size="20">
<br><br><input type="submit" value="Upload Image" name="action">
</form></center>
<div align="center"><br>
  <?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>
  <?
$handle = @opendir($f_user);

if(!empty($handle))
{
while(false !== ($file = readdir($handle)))
{
if(is_file($f_user . "/" . $file))
echo '<a href=http://www.reflexprojects.net/rs/upload/'. $f_user .'/'. $file .'>'. $file .'</a><br><br>';
}
}

closedir($handle);
?>

 

The best solution would be for the files to list in a 2-column table, with the name/link on the left (<a href=http://www.reflexprojects.net/rs/upload/'. $f_user .'/'. $file .'>'. $file .'</a>) and the 'delete' option on the right.

 

CODEKEY:

 

$f_user = username (stores files in a folder named with their username)

Link to comment
Share on other sites

I just saw two posts straight after each other anyway

 

To list your code you will need some thing like this

 

  <?php
$files = array();
$dir=opendir("./PLACEDIRHERE");
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..' && !is_dir($file))  
{  
	$files[] = $file;  
}  
}  
closedir($dir);
natcasesort($files);
echo "<ul>\n";
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo "<li><a href=\"$dir/".$files[$i]."\">".$files[$i]."</a><a href=\"delete.php?del=$files[$i]\"> - Delete</a></li>\n";
}
echo "</ul>\n";
?>

I have made the list for you now you need to make the delete.php script just a hint you will need to use a combination of the get command and the unlink command

Link to comment
Share on other sites

<?php

$file = $f_user"/"$_GET['del'];

if(unlink($file)) {
echo "The file was deleted successfully.";
}
else {
echo "There was an error trying to delete the file.";
}
?>

 

The code I'm using with yours. I typed the directory ID wrong. So what's wrong with this? $f_user is there because the folder is the user's username. EG. Testuser uploading test.gif would end up with the file in /testuser/test.gif. Anyways, problems?

Link to comment
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.