emediastudios Posted October 28, 2007 Share Posted October 28, 2007 I wanted to create a file that lists all the files in a directory, show a thumbnail of each and give an option to delete. Is this hard to do? I have this code i found on the web <?php // open the current directory $dhandle = opendir('.'); // define an array to hold the files $files = array(); if ($dhandle) { // loop through all of the files while (false !== ($fname = readdir($dhandle))) { // if the file is not this file, and does not start with a '.' or '..', // then store it for later display if (($fname != '.') && ($fname != '..') && ($fname != basename($_SERVER['PHP_SELF']))) { // store the filename $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname; } } // close the directory closedir($dhandle); } echo "<select name=\"file\">\n"; // Now loop through the files, echoing out a new select option for each one foreach( $files as $fname ) { echo "<option>{$fname}</option>\n"; } echo "</select>\n"; ?> It creates a nice list, but to get the files to display on click and an option to delete is over my head, any ideas or does someone have a code or link that could help mm. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/ Share on other sites More sharing options...
emediastudios Posted October 28, 2007 Author Share Posted October 28, 2007 I found a simpler code that does the same thing. Can i use this code with some extra coding to show the selected image and then option to delete on submit? It seems that there is a demand for this code. <?php echo "<select name=\"file\">\n"; foreach (new DirectoryIterator('.') as $file) { // if the file is not this file, and does not start with a '.' or '..', // then store it for later display if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) { echo "<option>"; // if the element is a directory add to the file name "(Dir)" echo ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename(); echo "</option>\n"; } } echo "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-379750 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 I have looked everywhere on the net, and have had no luck, any one have a code that can do this? Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380165 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 I used the code off this site and replaced the one that im using above. <?php $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<br />$file<a href=delete.php?$file=$file> 'Delete'</a>"; } ?> This shows all the files in my folder. Then i changed the delete.php file with this code only <?php {unlink($File);} ?> and i get this error Warning: unlink() [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\delete.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380171 Share on other sites More sharing options...
LemonInflux Posted October 29, 2007 Share Posted October 29, 2007 I had one. This code creates a couple of text fields as well: <?php //print_r($_POST); if($_POST["action"] == "Upload File") { 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 = $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."; } } ?> <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 File" name="action"> </form> <br> <?php if(is_array($error)) { while(list($key, $val) = each($error)) { echo $val; echo "<br>\n"; } } ?> Folders<table width="600" border="0" cellspacing="5" cellpadding="3"> <?php $files = array(); $dirs = array(); $dir=opendir($f_user); while(($file = readdir($dir)) !== false) { if($file !== '.' && $file !== '..') { if(!is_dir($f_user.'\\'.$file)){ $files[] = $file; }else{ $dirs[] = $file; } } } closedir($dir); natcasesort($files); natcasesort($dirs); foreach($dirs as $v){ echo '<tr><td width="400" valign="middle"><img src="../upload_files/uploader_icons/folder_icon.png" width="16" height="14"> <a href="folderview.php?fol='.$v.'"><font color=purple>'.$v.'</a></font></td><td width="200"><a href="deletefol.php?fol='. $v .'">Delete</a></td></tr>'; } echo "</table>\n"; echo 'Files<br><table width="600">'; for($i=0; $i<count($files); $i++) { if($files[$i] != "index.php") echo '<tr><td width="400" valign="middle"><img src="../upload_files/uploader_icons/file_icon.png" width="14" height="16"> <a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a></td><td width="200"><a href=\delete.php?del='. $files[$i] .'>Delete</a></td></tr>'; } echo "</table>\n"; ?> <form action="subfol.php" method="post" name="subfolder" id="subfolder"> <p>Add a subfolder</p> <p>Folder name: <input name="subfol" type="text" id="subfol"> </p> <p> <input type="submit" name="Submit" value="Create Folder!"> </p> </form> Might need a lot of modification. Was originally created for an image hosting system (people had their own directories, and could add subfolders). Went through it quick and took out all I could see, but knowing me there'll be loads I missed. Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380172 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 Thanks for trying man, looks like a lot of work. The code here below lists all my files. <?php $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<br />$file<a href=delete.php?file=$file> 'Delete'</a>"; } ?> I will make some changes later in its appearance but the code does its thing. the code i need to sort out is the delete.php i'm a newbie to php but am getting a little drift of things. i tried this code but to no avail delete.php <?php {unlink($File);} ?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380176 Share on other sites More sharing options...
GingerRobot Posted October 29, 2007 Share Posted October 29, 2007 Well, firstly, the parameter you pass is called file - with a small f. The file you try to unlink is called $File with a capital F. Also, did you retrieve that variable from the $_GET array? Try: <?php unlink($_GET['file']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380178 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 Well, firstly, the parameter you pass is called file - with a small f. The file you try to unlink is called $File with a capital F. Also, did you retrieve that variable from the $_GET array? Try: <?php unlink($_GET['file']); ?> I did a s you recommended and get the following error Warning: unlink(Object) [function.unlink]: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\images\delete.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380180 Share on other sites More sharing options...
jonoc33 Posted October 29, 2007 Share Posted October 29, 2007 I have PMed you with the exact code I used from my post earlier. It will most likely work for you as all you have to do is change some settings. Jono Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380187 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 I changed the code you pm me to this <?PHP $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "-<a href=$folder$file><label title=file>$file</a></label>"; echo "-<a href=filedelete.php?file=$file>Delete</a><br />"; echo '<img src="$file" alt="image" width="150" />'; } ?> i tried to get a thumnail to display but it doesnt Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380197 Share on other sites More sharing options...
adam291086 Posted October 29, 2007 Share Posted October 29, 2007 As i am a newbie i think i may of found a little problem. When you are echoing the file location within the html ''img scr'' you need to include the full path details and not just $file, which is the name of the file. You need to set up a variable that contains both $folder and $file Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380198 Share on other sites More sharing options...
rameshfaj Posted October 29, 2007 Share Posted October 29, 2007 If the error is permission denied,then u can check the access mode using chmod(077),if the error is file not found then the object/file may not be present. Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380199 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 As i am a newbie i think i may of found a little problem. When you are echoing the file location within the html ''img scr'' you need to include the full path details and not just $file, which is the name of the file. You need to set up a variable that contains both $folder and $file Gave that a go before, it didnt work either. thanks for trying champ, and yes that makes sense Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380204 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 This part of the code should open the image also <?php echo "<a href=$folder$file><label title=file>$file</a><br /></label>"; ?> but it doesnt ??? Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380205 Share on other sites More sharing options...
adam291086 Posted October 29, 2007 Share Posted October 29, 2007 are there any error messages or are you just getting a blank screen? Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380206 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 no error messages, just an empty image box. i get the file name echoed but when i click on it nothing. ??? Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380211 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 seems so strange as the file names echo correctly, why doesnt the link or thumnail work. Code again with a couple of changes <?PHP $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<a href=$folder$file><label title=$file>$file</a><br /></label>"; echo '<img src="$folder$file" alt="image" width="150" /><br />'; echo "<a href=filedelete.php?file=$file>Delete</a><br /><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380215 Share on other sites More sharing options...
adam291086 Posted October 29, 2007 Share Posted October 29, 2007 hey, try clicking on the file name and let the page load. Then look at the html source code and see if the img scr is corrent there. If it is then the picture should load. Your code is working but there is a problem with the img scr part. Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380217 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 loaded the page and read the souce code. its here <?php <a href=../images/Conventoncntr.jpg><label title=Conventoncntr.jpg>Conventoncntr.jpg</a><br /></label><img src="$folder$file" alt="image" width="150" /><br /><a href=filedelete.php?file=Conventoncntr.jpg>Delete</a><br /> ?> something not right there. this is the code generated from <?php #echo the files foreach ($files as $file) { echo "<a href=$folder$file><label title=$file>$file</a><br /></label>"; echo '<img src="$folder$file" alt="image" width="150" /><br />'; echo "<a href=filedelete.php?file=$file>Delete</a><br /><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380219 Share on other sites More sharing options...
adam291086 Posted October 29, 2007 Share Posted October 29, 2007 this may not work, but i have had the same problem before. Try removing the ../ from the $folder and just have /images/ Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380220 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 i did this and the link works now <?php echo "<a href='$folder$file'>$file</a><br />"; ?> now for the thumnail it still doesnt display Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380223 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 need some repair to this code <?php echo "<img src='$folder$file' alt="image" width="150" /><br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380224 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 i have this code <?php echo "<img src='$folder$file' alt="image" width="150" /><br />";?> and i get this error Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\filelist.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380226 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 ok i now have the images displaying ;D but it just echos this code alt='image' width='150' /> beside the image ??? Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380227 Share on other sites More sharing options...
emediastudios Posted October 29, 2007 Author Share Posted October 29, 2007 sorry here is the code <?php echo "<img src='$folder$file'> alt='image' width='150' /><br />";?> Quote Link to comment https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/#findComment-380228 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.