silverglade Posted May 20, 2010 Share Posted May 20, 2010 hi, i was wondering if someone could look at my code and see what im doing wrong, it kind of works. but it doesnt. lol. any help GREATLY appreciated. here is the code. <?php // NOTE GOT THIS CODE FROM W3SCHOOLS. if ((($_FILES["file"]["type"] == "image/gif") /* I DONT UNDERSTAND ANY OF THIS*/ || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 400000))// I INCREASED FILE SIZE FROM 20K TO 400K I THINK HERE { 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"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; header("Location:index.php"); //redirects them to updated gallery index page after submit } } } else { echo "invalid input"; } ////end file upload code $fileName=$_POST['fileName']; //get the stuff from fields in form and put in variables to check $file=$_POST['file']; function checkLength($fieldname) { if (isset($_POST['submitButton']) && strlen($_POST[$fieldname]) < 5) //If var length is less than 5 echo ' your file name under 5 characters.';//Set an error for later output elseif (isset($_POST['submitButton']) && strlen($_POST[$fieldname]) > 50) //if over 50 chars, output error return 'your file name is over 50 characters.';//Set an error for later output } $photos[] = 'upload/firstphoto.jpg'; $photos[] = 'upload/secondphoto.jpg'; $photos[] = 'upload/thirdphoto.jpg'; $photos[] = 'upload/fourthphoto.jpg'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="stylesheet" href="style.css" type="text/css" /> <title>Derek's Gallery</title> <style type="text/css"> <!-- .style1 {font-size: larger} .style2 {color: #FFFFFF} --> </style> </head> <body> <div id="container"> <!--YOU MUST BE USING EASYPHP LOCALHOST FOR THIS TO WORK, NOT THE "BROWSE" FUNCTION IN DW.--> <?php include('includes/header.php'); ?> <?php echo "<p> </p><div align='center'> <h3> Welcome to the gallery, todays date is ". date("m/d/Y") . "</h3><p></p></div>"; echo "<html><body>"; echo "<table align='center'><tr>"; foreach($photos as $file_name) { echo "<td>"; echo '<a href="picture.php?filename='.urlencode($file_name).'"><img src="'.$file_name.'" ."height="80" width="80" /></a>'; echo "</td>"; } echo "</tr></table> </body></html>"; ?> </div> <form action="" method="post" //--NOTE GOT THIS CODE FROM W3SCHOOLS enctype="multipart/form-data"> <br /> <table align="center" width="270" border="1"> <tr> <td><span class="style2"> <label for="file"><span class="style1"> Please upload your image</span></label> <br /> <br /> <span class="style1">File Name</span> <input type="text" name="fileName" id="fileName" /><!--ENTER FILE NAME--> <br /> <?php //If both submit button is set and filename is empty if(isset($_POST['submitButton']) && empty($fileName)) { //Echo out the error echo "Please enter a file name."; } ///now check for file length checkLength($fileName); ?> <br /> </span> <input type="file" name="file" id="file" /> </td><!--ENTER FILE--> </tr> <?php //If both submit button is set and file is empty if(isset($_POST['submitButton']) && empty($file)) { //Echo out the error echo "Please choose a file"; } //now check length checkLength($file); ?> <tr> <td> <input type="submit" name="submitButton" value="Submit" /> </td> </tr> </table> </form> <!--end container--> </body> </html> Link to comment https://forums.phpfreaks.com/topic/202447-need-help-with-form-checker-please/ Share on other sites More sharing options...
kenrbnsn Posted May 20, 2010 Share Posted May 20, 2010 Please tell us what the script is supposed to do and why you think it's broken. Ken Link to comment https://forums.phpfreaks.com/topic/202447-need-help-with-form-checker-please/#findComment-1061412 Share on other sites More sharing options...
silverglade Posted May 20, 2010 Author Share Posted May 20, 2010 sorry , the script is just supposed to check if the user added input into the file name and file fields. and checks if they are over 5 chars and under 50 chars, if so , echo an error. if user submits correctly, the page uploads an image to a directory on my server. but as it stands now , its not working. here are the errors i get Notice: Undefined index: file in C:\Program Files\EasyPHP5.3.0\www\PHOTOGALLERY\index.php on line 4 Notice: Undefined index: file in C:\Program Files\EasyPHP5.3.0\www\PHOTOGALLERY\index.php on line 5 Notice: Undefined index: file in C:\Program Files\EasyPHP5.3.0\www\PHOTOGALLERY\index.php on line 6 invalid input Notice: Undefined index: fileName in C:\Program Files\EasyPHP5.3.0\www\PHOTOGALLERY\index.php on line 42 Notice: Undefined index: file in C:\Program Files\EasyPHP5.3.0\www\PHOTOGALLERY\index.php on line 43 Link to comment https://forums.phpfreaks.com/topic/202447-need-help-with-form-checker-please/#findComment-1061416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.