emediastudios Posted October 13, 2007 Share Posted October 13, 2007 Hi there I have this code if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = "../images/". $_POST['photo']; $myFile2 = "../images/". $_POST['photo2']; $myFile3 = "../images/". $_POST['photo3']; $myFile4 = "../images/". $_POST['photo4']; $myFile5 = "../images/". $_POST['photo5']; $myFile6 = "../images/". $_POST['photo6']; $myFile7 = "../images/". $_POST['photo7']; $myFile8 = "../images/". $_POST['photo8']; $myFile9 = "../images/". $_POST['photo9']; if (!empty($_POST['photo'])) { unlink($myFile); } else { echo ""; } if (!empty($_POST['photo2'])) { unlink($myFile2); } else { echo ""; } if (!empty($_POST['photo3'])) { unlink($myFile3); } else { echo ""; } if (!empty($_POST['photo4'])) { unlink($myFile4); } else { echo ""; } if (!empty($_POST['photo5'])) { unlink($myFile5); } else { echo ""; } if (!empty($_POST['photo6'])) { unlink($myFile6); } else { echo ""; } if (!empty($_POST['photo7'])) { unlink($myFile7); } else { echo ""; } if (!empty($_POST['photo8'])) { unlink($myFile8); } else { echo ""; } if (!empty($_POST['photo9'])) { unlink($myFile9); } else { echo ""; } } header(sprintf("Location: %s", $deleteGoTo)); } I was told to use these codes and i had it working, but i did something and cant get it to work again <?php for($i=0;$i<count($_POST['photo'];$i++) { if (!empty($_POST['photo'][$i]) unlink($myFile[$i]); } ?> and this one <?php $myFile = array(); for ($i=1;$i<count($_POST['photo']);$i++) $myFile[$i] = '../images/' . $_POST['photo'][$i]; ?> How can i replace my code with this much more simplified code? Thanks all Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/ Share on other sites More sharing options...
marcus Posted October 13, 2007 Share Posted October 13, 2007 $myFile = array(); for($i=1;$i<=9;$i++){ $myFile[$i] = "../images/" . $_POST['photo' . $i]; } Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368781 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 I use this code if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = "../images/". $_POST['photo']; $myFile2 = "../images/". $_POST['photo2']; $myFile3 = "../images/". $_POST['photo3']; $myFile4 = "../images/". $_POST['photo4']; $myFile5 = "../images/". $_POST['photo5']; $myFile6 = "../images/". $_POST['photo6']; $myFile7 = "../images/". $_POST['photo7']; $myFile8 = "../images/". $_POST['photo8']; $myFile9 = "../images/". $_POST['photo9']; for($i=0;$i<count($_POST['photo']);$i++) { if (!empty($_POST['photo'][$i])) unlink($myFile[$i]); } } header(sprintf("Location: %s", $deleteGoTo)); } and get this error Warning: unlink(.) [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\property_delete.php on line 117 Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\property_delete.php:117) in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\property_delete.php on line 122 Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368782 Share on other sites More sharing options...
marcus Posted October 13, 2007 Share Posted October 13, 2007 Make you have the correct permissions to delete a file, and make sure the file exists before trying to delete it. And for the header just do: header("Location: $deleteGoTo"); Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368783 Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 Wow, that's a bit of unneccessary code. Whoever gave you the new code to try doesn't understand how the post array works. The main problem occurs with the $_POST['photo'][$i]. I am hoping that it's always a maximum of 9 images always being submitted at once, if so, this code works. I use 'photo'.$i in order to add the '1' or '2' to the photo part of it. (Can you make that first post variable to be $_POST['photo1'] instead of $_POST['photo']? Cause that makes it easier for you and me both.) $myFile = array(); for($i=1;$i<=9;$i++) { $myFile[$i] = "../images/" . $_POST['photo' . $i]; if (!empty($_POST['photo'.$i]) unlink($myFile[$i]); } Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368784 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 Thanks for your help but still stuck Could you past that code in my code below and ill see if it works. Heres my code if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = "../images/". $_POST['photo']; $myFile2 = "../images/". $_POST['photo2']; $myFile3 = "../images/". $_POST['photo3']; $myFile4 = "../images/". $_POST['photo4']; $myFile5 = "../images/". $_POST['photo5']; $myFile6 = "../images/". $_POST['photo6']; $myFile7 = "../images/". $_POST['photo7']; $myFile8 = "../images/". $_POST['photo8']; $myFile9 = "../images/". $_POST['photo9']; if (!empty($_POST['photo'])) { unlink($myFile); } else { echo ""; } if (!empty($_POST['photo2'])) { unlink($myFile2); } else { echo ""; } if (!empty($_POST['photo3'])) { unlink($myFile3); } else { echo ""; } if (!empty($_POST['photo4'])) { unlink($myFile4); } else { echo ""; } if (!empty($_POST['photo5'])) { unlink($myFile5); } else { echo ""; } if (!empty($_POST['photo6'])) { unlink($myFile6); } else { echo ""; } if (!empty($_POST['photo7'])) { unlink($myFile7); } else { echo ""; } if (!empty($_POST['photo8'])) { unlink($myFile8); } else { echo ""; } if (!empty($_POST['photo9'])) { unlink($myFile9); } else { echo ""; } } header("Location: $deleteGoTo"); } Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368795 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 If i change it to post photo1 i have heaps of code i have to alter, my database along with others. Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368802 Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 Ok, I'll rewrite the loop to handle the special case for "photo" so it doesn't do "photo1" if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = array(); for($i=1;$i<=9;$i++) { if($i == 1){//handle the special case for $i=1 $myFile[$i] = "../images/" . $_POST['photo']; if (!empty($_POST['photo']) unlink($myFile[$i]); } else {//regularly loop through $myFile[$i] = "../images/" . $_POST['photo' . $i]; if (!empty($_POST['photo'.$i]) unlink($myFile[$i]); } } header("Location: $deleteGoTo"); That's the reworked into the code, short huh? I've got a question about the header at the bottom, what's its purpose? After everything is done, it changes the page? Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368811 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 yes it opens a property deleted.php file. thanks il give it a go Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368814 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 I went to the trouble of renaming photo to photo1 in all my code and all my pages and used your code. It works. Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368876 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 No problem. Just remember, try to use the same pattern with variable naming to make it easier in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/#findComment-368878 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.