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!
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?
<?php
session_start();
if(!empty($_GETї'file']) && !empty($_SESSIONї'fiilit'])){
//check if $file is in $fiilit and then and ONLY then
$message=deleteFile($_GETї'file']);
}else $message="";
function readDir($dir){
$fiilit=new Array();
//Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$fiilitї]=basename($file); //stores only the name, not directory info
}
closedir($dh);
}
$_SESSIONї'fiilit']=$fiilit; //note this !!!!
return $fiilit;
}
function deleteFile($file){
unlink($file);
return "$file deleted";
}
//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.
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...