runnerjp Posted November 9, 2009 Share Posted November 9, 2009 Ok i have a form.. <form name="form1" method="post" action=""> <p>Name: <input name="Name" type="text" maxlength="20"> <br /> <br /> Email: <input name="Name" type="text" maxlength="20"> <br /> <br /> Event: <input name="Name" type="text" maxlength="20"> <br /> <br /> Venue: <input name="Name" type="text" maxlength="20"> </p> <p> </p> <p> <label> <input type="submit" name="submit" id="submit" value="Submit"> </label> <br /> <br /> </p> I want to have the option for users to upload a file (its an entry form to a compertition) but i wanted to know if i could add it to the current form so the same submit button is used. But i also want the form to be optional so if the dont have a file to upload they can still enter the data. Is this possible and if so how? Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/ Share on other sites More sharing options...
Bricktop Posted November 9, 2009 Share Posted November 9, 2009 Hi runnerjp, Have a look at this tutorial which will take you through the entire process. Also, on the code you have posted, all of your fields have the same name (e.g. name="Name"), this is going to cause you problems so make sure each field is named individually (e.g. name="Name", name="Event", "name="Email" etc) Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954015 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Ok followed it all and for some reason it wont let me upload a .doc saying invalid file here is my script <?php if ((($_FILES["file"]["type"] == "text/doc")) && ($_FILES["file"]["size"] < 20000)) { 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("include/entry" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "include/entry" . $_FILES["file"]["type"]; } } } else { echo "Invalid file".$_FILES["file"]; } print_r($_REQUEST['form']); ?> and my output is Invalid fileArray ( [name] => test [file] => cover.doc ) Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954027 Share on other sites More sharing options...
Bricktop Posted November 9, 2009 Share Posted November 9, 2009 Hi runnerjp, Try: $_FILES["file"]["type"] == "application/msword" Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954029 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 still get it Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954031 Share on other sites More sharing options...
Bricktop Posted November 9, 2009 Share Posted November 9, 2009 Hi runnerjp, Your first if statement has too many ( brackets. Change it to: if (($_FILES["file"]["type"] == "text/doc") && ($_FILES["file"]["size"] < 20000)) Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954034 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 I chnaged it and still get invalied file Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954035 Share on other sites More sharing options...
Bricktop Posted November 9, 2009 Share Posted November 9, 2009 Can you post your HTML code too. Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954038 Share on other sites More sharing options...
PravinS Posted November 9, 2009 Share Posted November 9, 2009 Also add enctype="multipart/form-data" for form. Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954040 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 here is html... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Iframe Ajax</title> <script type="text/javascript" src="webtoolkit.aim.js"></script> <script type="text/javascript"> function startCallback() { // make something useful before submit (onStart) return true; } function completeCallback(response) { // make something useful after (onComplete) document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1; document.getElementById('r').innerHTML = response; } </script> </head> <body> <form action="/include/addfixture.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback})"> <div><label>Name:</label> <input type="text" name="form[name]" /></div> <div><label>File:</label> <input type="file" name="form[file]" /></div> <div><input type="submit" value="SUBMIT" /></div> </form> <hr/> <div># of submited forms: <span id="nr">0</span></div> <div>last submit response (generated by form action - index.php file): <pre id="r"></pre></div> </body> </html> also here is the javascript just incase.. /** * * AJAX IFRAME METHOD (AIM) * http://www.webtoolkit.info/ * **/ AIM = { frame : function(c) { var n = 'f' + Math.floor(Math.random() * 99999); var d = document.createElement('DIV'); d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>'; document.body.appendChild(d); var i = document.getElementById(n); if (c && typeof(c.onComplete) == 'function') { i.onComplete = c.onComplete; } return n; }, form : function(f, name) { f.setAttribute('target', name); }, submit : function(f, c) { AIM.form(f, AIM.frame(c)); if (c && typeof(c.onStart) == 'function') { return c.onStart(); } else { return true; } }, loaded : function(id) { var i = document.getElementById(id); if (i.contentDocument) { var d = i.contentDocument; } else if (i.contentWindow) { var d = i.contentWindow.document; } else { var d = window.frames[id].document; } if (d.location.href == "about:blank") { return; } if (typeof(i.onComplete) == 'function') { i.onComplete(d.body.innerHTML); } } } Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954043 Share on other sites More sharing options...
Bricktop Posted November 9, 2009 Share Posted November 9, 2009 Change: {CODE] <div><label>File:</label> <input type="file" name="form[file]" /></div> [/code] to: <div><label>File:</label> <input type="file" name="file" /></div> Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954047 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Sadly no chnage Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954054 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 any more ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954149 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Ok i have even made it simple <?php $target_path = "/include/entrys/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!";} //print_r($_REQUEST['form']); ?> and i still get There was an error uploading the file, please try again! Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954184 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2009 Share Posted November 9, 2009 Did you ever do this - Also add enctype="multipart/form-data" for form. Because the code you posted after that did not contain it and you cannot upload a file using a form unless the <form tag contains that attribute. Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954188 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Ok i added it ... <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="form[name]" /></div> <div><label>File:</label> <input type="file" name="form" /></div> <div><input type="submit" value="SUBMIT" /></div> </form> but im still getting invalid file Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954196 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2009 Share Posted November 9, 2009 For debugging, add the following code immediately after your first opening <?php tag in your form processing code so that you can see exactly what you are getting from the form and what errors might be occurring - ini_set("display_errors", "1"); error_reporting(E_ALL); echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954202 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Undefined index on line 10 and 38 which are if ((($_FILES["file"]["type"] == "text/doc")) and echo "Invalid file".$_FILES["file"]; but i dont understand why :S Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954211 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2009 Share Posted November 9, 2009 The name="..." attribute of the form field does not match anything your code is checking. Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954218 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Ok over that yet still no upload..here out ouput POST:Array ( [form] => Array ( [name] => test ) ) FILES:Array ( [file] => Array ( [name] => ntu app.doc [type] => application/msword [tmp_name] => /tmp/phpvuQQNO [error] => 0 => 339968 ) ) Invalid fileArrayArray ( [name] => test ) and here is code <?php ini_set("display_errors", "1"); error_reporting(E_ALL); echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; 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("include/entry/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "include/entry/" . $_FILES["file"]["type"]; } } } else { echo "Invalid file".$_FILES["file"]; } print_r($_REQUEST['form']); Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954230 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 bmp Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954311 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2009 Share Posted November 9, 2009 The "Invalid file" message implies that the following statemet is FALSE - if ((($_FILES["file"]["type"] == "application/msword ")) && ($_FILES["file"]["size"] < 500000)) Since the ["size"] of the actual file is clearly < 500000, that would mean that the ["type"] of the actual file does not match the value in the conditional test. The value in the conditional test is "application/msword\n" and the value form the uploaded file is "application/msword". Those are not the same and an == comparison between them will be false. It matters if there is an extra \n on the end of a value that you copy/paste into code. Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954323 Share on other sites More sharing options...
runnerjp Posted November 9, 2009 Author Share Posted November 9, 2009 Hd a few more errors but i managed to sort them. Thank-you for helping me out! I have learnt that i sholdnt look so deeply into the errors , when these where small lazy mistakes! Quote Link to comment https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954370 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.