skot Posted September 8, 2008 Share Posted September 8, 2008 Hi. I'm trying to use the following to delete a file on the server with the press of a button. The file name comes from an array of files located in the directory. The problem is I've tried everything and I can't get the script to actually delete the file.. Uploaded.php contains a list of files. When 'X' is pressed next to a file, the following script passes the filename to delFile.php, which is supposed to remove it completely. Am I going about this the wrong way? Uploaded.php:- <?php function sortByDate($b, $a) { if ($a['dateSort'] == $b['dateSort']) { return 0; } return ($a['dateSort'] < $b['dateSort'])? -1 : 1; } //Valid extensions $valid_extensions = array ('xls', 'zip', 'pdf', 'csv', 'txt', 'doc', 'jpeg', 'png', 'jpg', 'gif', 'rar', 'xlsx', 'docx', 'bak', '001', 'rtf', 'mak', 'ini', 'sgc', 'sly', 'xml', 'vbs', 'docx', 'xlsx', 'bmp', 'htm'); $path = '.'; $dirArray = array(); // set timezone to GMT for all time fields putenv("TZ=GB/GMT"); // open dir $myDirectory = opendir($path); // fetch entries while($fileName = readdir($myDirectory)) { //Only process files if (!is_dir($path.'/'.$fileName)) { //Only process files with valid extensions $ext = strtolower(array_pop(explode(".", ($fileName)))); if (in_array($ext, $valid_extensions)) { $fileIdx = count($dirArray); $size = filesize($fileName); $totalSize += $size; $dirArray[$fileIdx]['name'] = $fileName; $dirArray[$fileIdx]['ext'] = $ext; $dirArray[$fileIdx]['dateSort'] = date('Ymdhi', filemtime($fileName)); $dirArray[$fileIdx]['date'] = date('d/m/Y @ H:i', filemtime($fileName)); $dirArray[$fileIdx]['size'] = round((filesize($fileName) / 1024 / 1024), 2); } } } $sizecount = round(($totalSize/ 1024 / 1024), 2); $sizecountgb = round(($totalSize/ 1024 / 1024 / 1024), 2); // close dir closedir($myDirectory); //Sort by key usort($dirArray, 'sortByDate'); echo "<p><font face=verdana size=4 color=#016E6B><B>"; echo "To download right click on a file and 'save as...'<br>\n"; echo "If the file is an image, you can left click on it to view it in the browser.</B></font></p><br>\n"; echo "<p class=\"box2\"<font face=verdana size=3 color=#045856>Current File Count: <b>" . count($dirArray) . "</b><br>\n"; echo "Current Total Size: <b>" . $sizecount . " MB</b> <i>(". $sizecountgb . " GB)</i></font></p><br>\n"; // print header row echo "<center>\n"; echo "<table width=\"98%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#016E6B\">\n"; echo "<tr align=\"left\" background=\"http://centurysoftware.co.uk/img/Crow_bg2.gif\">\n"; echo " <th align=\"left\">File Name</th>\n"; echo " <th align=\"center\">Type</th>\n"; echo " <th align=\"center\">Size</th>\n"; echo " <th align=\"center\">Date/Time Uploaded</th>\n"; echo " <th align=\"center\">Delete?</th>\n"; echo "</tr>\n"; // print results foreach ($dirArray as $file) { echo "<tr background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\">\n"; echo " <td align=\"left\" background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\">"; echo "<font face=\"verdana\" size=\"2\" color=\"#D9F4FD\"><b> "; echo "<a href=\"{$file['name']}\"><img src=\"http://centurysoftware.co.uk/img/Cstar.gif\" border=\"0\">"; echo $file['name'] . "</a></b></font></td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\">{$file['ext']}</td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\">{$file['size']} " . MB ."</td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\">{$file['date']}</td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\"><form name=del method=post action=delFile.php><input name=delfile type=hidden value={$file['name']}><input type=submit name=X id=X value=X /></form></td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "</center><br clear=\"all\">\n"; ?> [b]delFile.php:-[/b] <?php unlink("$delfile"); echo("<center><br><br><b>File Removed</b><br><br>"); echo("<br><br><br><a href=uploaded.php>Go back?<a></center>"); ?> Link to comment https://forums.phpfreaks.com/topic/123349-php-delete-file/ Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 First of all, $delFile means nothing to delFile.php because you never actually set $delFile = $_POST['delfine']. Secondly, you really do need quotations around HTML attributes. Really. Try validating your page. Link to comment https://forums.phpfreaks.com/topic/123349-php-delete-file/#findComment-637070 Share on other sites More sharing options...
skot Posted September 9, 2008 Author Share Posted September 9, 2008 Thank you for your feedback. New code based on your input, and something I thought was worth trying, which still doesn't work:- Uploaded.php: <?php function sortByDate($b, $a) { if ($a['dateSort'] == $b['dateSort']) { return 0; } return ($a['dateSort'] < $b['dateSort'])? -1 : 1; } //Valid extensions $valid_extensions = array ('xls', 'zip', 'pdf', 'csv', 'txt', 'doc', 'jpeg', 'png', 'jpg', 'gif', 'rar', 'xlsx', 'docx', 'bak', '001', 'rtf', 'mak', 'ini', 'sgc', 'sly', 'xml', 'vbs', 'bmp', 'htm'); $path = '.'; $dirArray = array(); // set timezone to GMT for all time fields putenv("TZ=GB/GMT"); // open dir $myDirectory = opendir($path); // fetch entries while($fileName = readdir($myDirectory)) { //Only process files if (!is_dir($path.'/'.$fileName)) { //Only process files with valid extensions $ext = strtolower(array_pop(explode(".", ($fileName)))); if (in_array($ext, $valid_extensions)) { $fileIdx = count($dirArray); $size = filesize($fileName); $totalSize += $size; $dirArray[$fileIdx]['name'] = $fileName; $dirArray[$fileIdx]['ext'] = $ext; $dirArray[$fileIdx]['dateSort'] = date('Ymdhi', filemtime($fileName)); $dirArray[$fileIdx]['date'] = date('d/m/Y @ H:i', filemtime($fileName)); $dirArray[$fileIdx]['size'] = round((filesize($fileName) / 1024 / 1024), 2); } } } $sizecount = round(($totalSize/ 1024 / 1024), 2); $sizecountgb = round(($totalSize/ 1024 / 1024 / 1024), 2); // close dir closedir($myDirectory); //Sort by key usort($dirArray, 'sortByDate'); echo "<p><font face=verdana size=4 color=#016E6B><B>"; echo "To download right click on a file and 'save as...'<br>\n"; echo "If the file is an image, you can left click on it to view it in the browser.</B></font></p><br>\n"; echo "<p class=\"box2\"<font face=verdana size=3 color=#045856>Current File Count: <b>" . count($dirArray) . "</b><br>\n"; echo "Current Total Size: <b>" . $sizecount . " MB</b> <i>(". $sizecountgb . " GB)</i></font></p><br>\n"; // print header row echo "<center>\n"; echo "<table width=\"98%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#016E6B\">\n"; echo "<tr align=\"left\" background=\"http://centurysoftware.co.uk/img/Crow_bg2.gif\">\n"; echo " <th align=\"left\">File Name</th>\n"; echo " <th align=\"center\">Type</th>\n"; echo " <th align=\"center\">Size</th>\n"; echo " <th align=\"center\">Date/Time Uploaded</th>\n"; echo " <th align=\"center\">Delete?</th>\n"; echo "</tr>\n"; // print results foreach ($dirArray as $file) { echo "<tr background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\">\n"; echo " <td align=\"left\" background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\">"; echo "<font face=\"verdana\" size=\"2\" color=\"#D9F4FD\"><b> "; echo "<a href=\"{$file['name']}\"><img src=\"http://centurysoftware.co.uk/img/Cstar.gif\" border=\"0\">"; echo $file['name'] . "</a></b></font></td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\">{$file['ext']}</td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\">{$file['size']} " . MB ."</td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\">{$file['date']}</td>\n"; echo " <td background=\"http://centurysoftware.co.uk/img/Crow_bg.gif\" align=\"center\"><form name=\"del\" method=\"post\" action=\"delFile.php?delFile={$file['name']}\"><input name=\"delfile\" type=\"hidden\" value=\"{$file['name']}\"><input type=\"submit\" name=\"X\" id=\"X\" value=\"X\" /></form></td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "</center><br clear=\"all\">\n"; ?> delFile.php <?php $delFile = $_POST['delFile']; unlink("$delFile"); //echo("<center><br><br><b>File: ". $delFile ." has been deleted.</b><br><br>"); echo("<br><br><br>Done. <a href=uploaded.php>Go back?<a></center>"); ?> Any suggestions would be appreciated Link to comment https://forums.phpfreaks.com/topic/123349-php-delete-file/#findComment-637186 Share on other sites More sharing options...
JasonLewis Posted September 9, 2008 Share Posted September 9, 2008 Another thing, you are using $_POST['delFile'] but in your form the field is called "delfile", with no capital F. Also, some validation should be in order. And make sure that $delFile has the extension as well, and the correct directory. Link to comment https://forums.phpfreaks.com/topic/123349-php-delete-file/#findComment-637311 Share on other sites More sharing options...
skot Posted September 9, 2008 Author Share Posted September 9, 2008 Thank you - that's all it was - the capitalization of delFile has resolved the problem Link to comment https://forums.phpfreaks.com/topic/123349-php-delete-file/#findComment-637537 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.