Hi everyone,
So I could use some newbie advice on using the PHP Unlink() function.
The background info:
I have two different php scripts.
- Script "1" allows me to upload files (using file upload) to a given directory that I specify in the configuration.
- Script "2" allows me to view (view only) all files that have been uploaded via script 1.
I am currently in the process of modifying Script "2" so that I can delete any file that the user may designate. I am not setting this up to do multiple delte. Just a single file at a time.
I have added the word "delete" to each file detail line. My goal is to set this up so tha when the user "clicks" on the word "delete" on a given file line that file is delted.
The problem:
1. I know that I need to use the PHP Unlink() function to accomplish this task. I know that that the model for this function is basically:
PHP Code:
unlink ($fileToDelete)
2. I don't know how to associate this function to the word "delete" and make the function unique to that specified file.
Here is a snippet of my code to help explain what I have done:
Created a delete file function:
PHP Code:
function deletFile (unlink($file));
Added a link called "DELETE" to the File "2" (phpDirList) Output
PHP Code:
// Try to get a directory handle and continue if it returns TRUE.
if ($dir = @opendir("./")) {
$output = ""; // Empty the HTML-output variable.
while (($file = readdir($dir)) !== FALSE) { // Read the directory step by step and process the data.
if (test_ext ($file)) { // Check if the entry should be displayed or not and continue if TRUE.
$stats = @stat ( "./".$file); // Get the file stats.
// And append a table row to the output variable.
$output .= '
<tr class="line">
<td class="col2">DELETE</td>
<td class="col1">
<a title="'.$file.'" href="'.$file.'">'.$url.''.$file.'</a>
</td>
<td class="col2">'.number_format ($stats[7], 0, ",", ".").'</td>
<td class="col3">'.str_replace (" ", " ยท ", date ("m.d.Y H:i", $stats[9])).'</td>
</tr>'."\n";
}
}
closedir($dir); // Close the directory handle.
} else {
$output = "<tr><td colspan=\"3\"><h1>Cannot open Directory!</h1></td></tr>"; // Display error message if there was no directory handle returned in the leading condition.
}
So what's next?
I almost wish I could do something like
HTML Code:
<a onclick="<?unlink($file)?>">DELETE</a>
Am I going down the right path?
Any help, direction, or comments would be appreciated!