forms - Prevent PHP script from being executed when submitting to self -
i have form:
<form name="commentform" id="commentform" action="comment.php" method="post" enctype="multipart/form-data"> name: <textarea maxlength="60" rows="1" cols="62" class="margin" name="name" id="name"> </textarea> <br><br> submit picture <input type="file" name="pic" id="pic" /> <br><br> <input type="submit" value="submit" /> </form> this php validate picture (from w3schools.com):
<?php if ((($_files["file"]["type"] == "image/gif") || ($_files["file"]["type"] == "image/jpeg") || ($_files["file"]["type"] == "image/pjpeg")) && ($_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("upload/" . $_files["file"]["name"])) { echo $_files["file"]["name"] . " exists. "; } else { move_uploaded_file($_files["file"]["tmp_name"], "upload/" . $_files["file"]["name"]); echo "stored in: " . "upload/" . $_files["file"]["name"]; } } } else { echo "invalid file"; } ?> i submitting form same page, php executed webpage loads. how can make load form submitted? also, script not seem working.
you need check if form submitted before process file upload:
if ( isset($_post['pic'])) { //save file here. } edit: looks not referring right post variable - have file element called 'pic' in form referring $_post['file'] in php code not exist.
also: if starting out php, (imho) w3schools.com worse place can - i've seen bad examples of how code should not written in there..
Comments
Post a Comment