Marcos01 Posted August 30, 2008 Share Posted August 30, 2008 Hi all, I use 2 dropdown boxes to select the image I want to delete. First select directory and then select the image. The path to the image: ../userfiles/images/projects/ How do I use the path in the code? This is the code so far: //delete image: $folder = $_POST["folder"]; $image = $_POST["images"]; //delete image if ($_POST['doaction']=='submit'.'delete') { unlink($folder.$image); } Link to comment https://forums.phpfreaks.com/topic/122021-delete-image/ Share on other sites More sharing options...
genericnumber1 Posted August 30, 2008 Share Posted August 30, 2008 That's not really the best method... what if someone passes ../../../ for the dir and index.php for the image? There goes your homepage. I would strip all of the .. and / from both inputs. After you've done that.. unlink("../userfiles/images/projects/$folder/$image"); should suffice. Link to comment https://forums.phpfreaks.com/topic/122021-delete-image/#findComment-629858 Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 You should also make sure that the file exists first to make sure they didn't pass their own string in by editing the POST data. Link to comment https://forums.phpfreaks.com/topic/122021-delete-image/#findComment-629860 Share on other sites More sharing options...
redarrow Posted August 30, 2008 Share Posted August 30, 2008 Example for delete a file.......... This will work so be very carefull remeber, once a file is deleted it gon for good!. <?php chdir('../pics/'); //cheek the directory there......... $do = unlink($fileToDel); // delete the file from the cheeked directory...... if($do=="1"){ // if do ==1 then echo file was deleted......... echo "The file was deleted successfully."; } else { echo "There was an error trying to delete the file."; }// if file not there then echo message ?> Link to comment https://forums.phpfreaks.com/topic/122021-delete-image/#findComment-629865 Share on other sites More sharing options...
Marcos01 Posted August 30, 2008 Author Share Posted August 30, 2008 Thanks for the replies. Unfortunately it didn't work. I tried this code first code: $folder = $_POST["folders"]; $image = $_POST["images"]; if ($_POST['doaction']=='submit'.'delete') { unlink("../userfiles/images/projecten/$folder/$image"); } Next I will try your code redarrow Link to comment https://forums.phpfreaks.com/topic/122021-delete-image/#findComment-629869 Share on other sites More sharing options...
Marcos01 Posted August 30, 2008 Author Share Posted August 30, 2008 Tried this code too but didn't work. I am not getting any errors. $folder = $_POST["folders"]; $image = $_POST["images"]; $path = "../userfiles/images/projecten/"; if ($_POST['doaction']=='bevestigen'.'wissen') { chdir('../userfiles/images/projecten/'.$folder.'/'); $do = unlink($image); if($do=="1"){ echo "The file was deleted successfully."; } else { echo "There was an error trying to delete the file."; } } Link to comment https://forums.phpfreaks.com/topic/122021-delete-image/#findComment-629889 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.