runnerjp Posted November 10, 2009 Share Posted November 10, 2009 ok so i have my form and in it you can add a entry form via an upload. The thing is its just an option so when i run my script <?php ini_set("display_errors", "1"); error_reporting(E_ALL); if ((($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 500000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; //echo "Type: " . $_FILES["file"]["type"] . "<br />"; // echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; // echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("entrys/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "entrys/" . $_FILES["file"]["name"]); echo "Stored in: " . "entrys/" . $_FILES["file"]["type"]; } } } else { echo "Invalid file".$_FILES["file"]["name"]; } //print_r($_REQUEST['form']); ?> to obvius im going to get echo "Invalid file".$_FILES["file"]["name"];. Is there away to check if there is any information within the <div><label>File:</label> <input type="file" name="file" /></div> and if not dont run the file script? Just to add i want everything to work off this script so adding names ect so form looks like this <form action="/include/addfixture.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback})"enctype="multipart/form-data"> <div><label>Name:</label> <input type="text" name="name" /></div> <div><label>File:</label> <input type="file" name="file" /></div> <div><label>Email:</label> <input type="text" name="email" /></div> <div><label>Event:</label> <input type="text" name="event" /></div> <div><label>Venue:</label> <input type="text" name="venue" /></div> <div><input type="submit" name="form"value="SUBMIT" /></div> Link to comment https://forums.phpfreaks.com/topic/181006-option-to-add-file-or-not/ Share on other sites More sharing options...
Adam Posted November 10, 2009 Share Posted November 10, 2009 You could wrap the whole thing with: if (isset($_FILES['file'])) { // ... } .. If I understand you right. Link to comment https://forums.phpfreaks.com/topic/181006-option-to-add-file-or-not/#findComment-954968 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 I tried that with if (isset($_FILES['file'])) { if ((($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 500000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; //echo "Type: " . $_FILES["file"]["type"] . "<br />"; // echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; // echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("entrys/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "entrys/" . $_FILES["file"]["name"]); echo "Stored in: " . "entrys/" . $_FILES["file"]["type"]; } } } else { echo "Invalid file".$_FILES["file"]["name"]; } } but i still get invalied file... wont it prosses it due to it being within the form even if it has nothing in it...i would have thought u had to see if it was blank and if so dont do it Link to comment https://forums.phpfreaks.com/topic/181006-option-to-add-file-or-not/#findComment-954996 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 Ok i just thought when i was typing it i answered me own question but would this be ok if (!$_FILES['file']["name"]== '') Link to comment https://forums.phpfreaks.com/topic/181006-option-to-add-file-or-not/#findComment-954997 Share on other sites More sharing options...
Adam Posted November 10, 2009 Share Posted November 10, 2009 Ah yeah my mistake. Seems it still passes the $_FILES array but the 'error' property is set to 4; meaning 'no file was uploaded'. You could use: if ($_FILES['file']['error'] != 4) { Link to comment https://forums.phpfreaks.com/topic/181006-option-to-add-file-or-not/#findComment-955018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.