Jump to content

if else suggestion needed


TechMistress

Recommended Posts

Hello, I have this piece of code in my page, but I need to add an 'else' to it, but don't quite know how.

 

if(isset($_POST[s1]))
{
if(!empty($_FILES[images][name][0]))
{
	while(list($key,$value) = each($_FILES[images][name]))
	{
		if(!empty($value))
		{
			list($fname,$ext) = split("\.", $value);
			if ($ext == "jpg" || $ext == "gif" || $ext == "png") {
				$NewImageName = $t."_offer_".$value;
				copy($_FILES[images][tmp_name][$key], "re_images/".$NewImageName);

				$MyImages[] = $NewImageName;
			}
		}
	}

	if(!empty($MyImages))
	{
		$ImageStr = implode("|", $MyImages);
	}

}

 

Basically, I have an open-ended file extension portion. The part where it says

 

if ($ext == "jpg" || $ext == "gif" || $ext == "png") {

 

I want it to do NOTHING and show an error if it is NOT one of those extensions. People are able to upload anything at the moment, and I want to prevent it.

 

Thank you so much!

Link to comment
https://forums.phpfreaks.com/topic/189678-if-else-suggestion-needed/
Share on other sites

if(isset($_POST[s1]))

{

  if(!empty($_FILES[images][name][0]))

  {

      while(list($key,$value) = each($_FILES[images][name]))

      {

        if(!empty($value))

        {

            list($fname,$ext) = split("\.", $value);

            if ($ext == "jpg" || $ext == "gif" || $ext == "png") {

              $NewImageName = $t."_offer_".$value;

              copy($_FILES[images][tmp_name][$key], "re_images/".$NewImageName);

 

              $MyImages[] = $NewImageName;

            }

            else

            {

                    // your error message here

            }

        }

      }

 

      if(!empty($MyImages))

      {

        $ImageStr = implode("|", $MyImages);

      }

 

  }

Thank you very much for the reply!  The error message worked fine. It took me a little bit, but I also figured out the unlink function to delete temp file.

 

            else
            {
                   echo "You have tried to upload an invalid file type, <a href=\"javascript:history.go(-1)\">Please try again.</a>";
     unlink($_FILES['images']['tmp_name'][$key]);
            }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.