GregL83 Posted April 26, 2007 Share Posted April 26, 2007 So another noob question from GregL83 . I want to run some code only if a file is selected to be uploaded. if(is_uploaded_file($_FILES["file"])) { if(($_FILES["file"]["size"] < 250000)) { if($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]."<br/>"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $ftype = substr($_FILES["file"]["type"],(strlen($_FILES["file"]["type"]))-3); echo $ftype."<br/>"; move_uploaded_file($_FILES["file"]["tmp_name"], "images/main_image.".$ftype); echo "Stored in: "."images/".$_FILES["file"]["name"]; } } } The line of importance is at the top! I want the functions to run if a file is uploaded. Let me know if you need the form, but i dont think it is nessecary to identify the proper method. Thanks once again. You guys are great! Quote Link to comment Share on other sites More sharing options...
GregL83 Posted April 26, 2007 Author Share Posted April 26, 2007 Something else I tried is: if($_POST['file']) {} Problem with this is I get a undefined index at the line because the variable $_POST['file'] doesn't exist if no file is uploaded... How do i get php to run a bit of code only if the the variable exists? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 26, 2007 Share Posted April 26, 2007 maybe try if (isset($_POST['file'])) or maybe if (isset(basename($_FILES['file']['name']))) Quote Link to comment Share on other sites More sharing options...
GregL83 Posted April 26, 2007 Author Share Posted April 26, 2007 Clown, thanks for the reply... I have now learned more about the isset function. My PHP teacher sucks! anyhow, what worked for me, was if(isset($_FILES["file"])){} Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 26, 2007 Share Posted April 26, 2007 no problem... always fun to help Quote Link to comment 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.