Deleting Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Deleting Files

Post by moiseszaragoza »

Ok. I'm having problems passing information from one page to another.


I know the answer is to use a session.

This is what I have.

I’m getting all the files I have in a directory. Using the readdir() and loading it in a loop.
But I want to be able to delete a file from the directory using php. How would I do this?

Code: Select all

<?php
					$dir = "mydir/";

				// Open a known directory, and proceed to read its contents
					if (is_dir($dir)) &#123;
				   if ($dh = opendir($dir)) &#123;
				       while (($file = readdir($dh)) !== false) &#123;  				   	      
				           echo "<a href='Deletefile.php?file=$file'>Delete File </a> &nbsp;&nbsp;&nbsp;&nbsp;<a href='$dir$file' target='_self'>filename: $file : </a>" . "<br>";
				       &#125;
			       closedir($dh);
				   &#125;
				&#125;
on the Deletefile.php page i have

Code: Select all

<?php 
$file = $_GET&#1111;'file'] 

	function DeletemyFile($file)
&#123;
	unlink($file)
   echo "the file : $file has been delete ";
&#125;
 ?>
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Why don't you combine these two and make it a bit more secure while you are at it:

Code: Select all

<?php 
session_start();
if(!empty($_GET&#1111;'file']) && !empty($_SESSION&#1111;'fiilit']))&#123;

//check if $file is in $fiilit and then and ONLY then

$message=deleteFile($_GET&#1111;'file']);
&#125;else $message="";

function readDir($dir)&#123;
  $fiilit=new Array();
  //Open a known directory, and proceed to read its contents 
       if (is_dir($dir)) &#123; 
       if ($dh = opendir($dir)) &#123; 
          while (($file = readdir($dh)) !== false) &#123;                          
             $fiilit&#1111;]=basename($file); //stores only the name, not directory info
          &#125; 
          closedir($dh);
      &#125;
$_SESSION&#1111;'fiilit']=$fiilit; //note this !!!!
return $fiilit;
&#125;
function deleteFile($file)&#123;
    unlink($file);
    return "$file deleted";
&#125;
//printing your filelist from...
$filelist=readDir("whatever_directory");
print $message;
print "them in the loop of some sort as you did before";
When using GET it is imperative to check if the information passed is correct. Propably using basename() isn't the smartest thing. Fill in the missing parts and it should work.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

store the rows/data that you will display to the visitor in the session.
map each row to a number, usually 1 to n....

on the delete page, check the rownumber, and lookup the related data in the session...

this way you can handle any kind of data you are displaying... and operations based on the data that was shown... also note that it is not a problem to have a primary key that consists out of more than one row in your database...


so yes, sessions are the way to go (imho)
Post Reply