TechMistress Posted January 25, 2010 Share Posted January 25, 2010 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 More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 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); } } Link to comment https://forums.phpfreaks.com/topic/189678-if-else-suggestion-needed/#findComment-1001164 Share on other sites More sharing options...
TechMistress Posted January 25, 2010 Author Share Posted January 25, 2010 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]); } Link to comment https://forums.phpfreaks.com/topic/189678-if-else-suggestion-needed/#findComment-1001175 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.