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! Link to comment https://forums.phpfreaks.com/topic/48715-solved-return-true-upon-file-upload/ 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? Link to comment https://forums.phpfreaks.com/topic/48715-solved-return-true-upon-file-upload/#findComment-238704 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']))) Link to comment https://forums.phpfreaks.com/topic/48715-solved-return-true-upon-file-upload/#findComment-238714 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"])){} Link to comment https://forums.phpfreaks.com/topic/48715-solved-return-true-upon-file-upload/#findComment-238729 Share on other sites More sharing options...
clown[NOR] Posted April 26, 2007 Share Posted April 26, 2007 no problem... always fun to help Link to comment https://forums.phpfreaks.com/topic/48715-solved-return-true-upon-file-upload/#findComment-238829 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.