Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/73130-solved-simplifiy-code-array/
Share on other sites

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

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]);
}

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");

}

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.