mitchellyachting Posted October 14, 2008 Share Posted October 14, 2008 I hope someone can steer me in the right direction with this. I have written an .inc file and a .php to take some member data and update a MySQL database. What I need to do now is offer the facility to upload two files one being the users picture and one being either a .doc or a .pdf file. I have seen lots of scripts on the web for file uploads but I am having problems understanding how I incorporate these into my form. Any thoughts please!! Link to comment https://forums.phpfreaks.com/topic/128295-problem-with-file-uploads-in-a-form/ Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 What don't you understand? Link to comment https://forums.phpfreaks.com/topic/128295-problem-with-file-uploads-in-a-form/#findComment-664607 Share on other sites More sharing options...
dropfaith Posted October 14, 2008 Share Posted October 14, 2008 Okay so this may or may not help you but its the code i use to process image uploads Form <form method="post" "action="processart.php" enctype="multipart/form-data"> <p> <label>Title: </label> <input type="text" name="Title" size="30" /><br /> </p> <p><label>File</label> <input id="my_file_element" type="file" name="file_1" > </p> <p> <input type="submit" name="submit" value="Send!"/> </p> </form> <?php !! This file does no security checking - this solely handles file uploads - !! this file does not handle any security functions. Heed that warning! You use this file at your !! own risk and please do not publically accept files if you don't know what you're doing with !! server security. at the end of this script you will have two variables $filenames - an array that contains the names of the file uploads that succeeded $error - an array of errors that occured while processing files if the max file size in the form is more than what is set in php.ini then an addition needs to be made to the htaccess file to accomodate this add this to your .htaccess file for this directory php_value post_max_size 10M php_value upload_max_filesize 10M replace 10M to match the value you entered above for $max_file_size */ // images dir - relative from document root // this needs to be a folder that is writeable by the server $image_dir = '/art/'; // upload dir $destination = $_SERVER['DOCUMENT_ROOT'].$image_dir; if(isset($_FILES)) { // initialize error var for processing $error = array(); // acceptable files // if array is blank then all file types will be accepted $filetypes = array( 'ai' => 'application/postscript', 'jpg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/x-png', 'gif' => 'image/gif', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', ); // function to check for accpetable file type function okFileType($type) { // if filetypes array is empty then let everything through if(count($GLOBALS['filetypes']) < 1) { return true; } // if no match is made to a valid file types array then kick it back elseif(!in_array($type,$GLOBALS['filetypes'])) { $GLOBALS['error'][] = $type.' is not an acceptable file type. '. $type.' has been ignored.'; return false; } // else - let the file through else { return true; } } // function to check and move file function processFile($file) { // set full path/name of file to be moved $upload_file = $GLOBALS['destination'].$file['name']; if(file_exists($upload_file)) { $GLOBALS['error'][] = $file['name'].' - Filename exists - please change your image filename'; return false; } if(!move_uploaded_file($file['tmp_name'], $upload_file)) { // failed to move file $GLOBALS['error'][] = 'File Upload Failed on '.$file['name'].' - Please try again'; return false; } else { // upload OK - change file permissions chmod($upload_file, 0755); return true; } } // check to make sure files were uploaded $no_files = 0; $uploaded = array(); foreach($_FILES as $file) { switch($file['error']) { case 0: // file found if($file['name'] != NULL && okFileType($file['type']) != false) { // process the file if(processFile($file) == true) $uploaded = $file['name']; } break; case (1|2): // upload too large $error[] = 'file upload is too large for '.$file['name']; break; case 4: // no file uploaded break; case (6|7): // no temp folder or failed write - server config errors $error[] = 'internal error - flog the webmaster on '.$file['name']; break; } } } include '../template/conf.php'; // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); echo "<hr>"; $cTitle = mysql_escape_string($_POST['Title']); $cName = mysql_escape_string($_POST['Name']); $Title = htmlentities($cTitle,ENT_QUOTES,"utf-8"); $Name = htmlentities($cName,ENT_QUOTES,"utf-8"); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $rt=mysql_query("insert into Artistimages (Title, Name, uploaded) values('$Title','$Name','$uploaded')") or die(mysql_error()); echo $rt; ?> Link to comment https://forums.phpfreaks.com/topic/128295-problem-with-file-uploads-in-a-form/#findComment-664621 Share on other sites More sharing options...
mitchellyachting Posted October 14, 2008 Author Share Posted October 14, 2008 My problem is that the .php I have written alows me to insert a row in the MySQl table but I do not know how to then call for an upload of a file to the file system. Here is my .php file:- <?php /* Program: Registration.php */ session_start(); include("dogs.inc"); switch (@$_POST['do']) { case "new": /* Check for blanks */ foreach($_POST as $field => $value) { if ($value == "") { $blanks[] = $field; } } if(isset($blanks)) { $message_new = "The following fields are blank. Please enter the required information: "; foreach($blanks as $value) { $message_new .= "$value, "; } extract($_POST); include("reg_form.inc"); exit(); } /* *******Validate data****** */ foreach($_POST as $field => $value) { if(!empty($value)) { if(eregi("firstName",$field) or eregi("lastName",$field)) { if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value)) { $errors[] = "$value is not a valid Name."; } } if(eregi("HomePort",$field)) { if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value)) { $errors[] = "$value is not a valid city."; } } if(eregi("phone",$field) ) { if(!ereg("^[0-9)(xX -]{7,20}$",$value)) { $errors[] = "$value is not a valid phone number. "; } } if(eregi("email",$field)) { if(!ereg("^.+@.+\\..+$",$value)) { $errors[] = "$value is not a valid email address."; } } if(eregi("Position",$field)) { if(!ereg("[0-9]{2}",$value)) { $errors[]="$value is not a valid position."; } } if(eregi("How",$field)) { if(!ereg("[0-9]{2}",$value)) { $errors[]="$value is not a valid entry for how you found out about the fare."; } } } // end if empty } // end foreach if(@is_array($errors)) { $message_new = ""; foreach($errors as $value) { $message_new .= $value." Please try again<br />"; } extract($_POST); include("reg_form.inc"); exit(); } /* ****End of Data Validation */ /* clean data */ $cxn = mysqli_connect($host,$user,$passwd,$dbname); foreach($_POST as $field => $value) { if($field != "Button" and $field != "do") { $fields[]=$field; $value = strip_tags(trim($value)); $values[] = mysqli_real_escape_string($cxn,$value); $$field = $value; } } /* check whether user name already exists */ $sql = "SELECT Email FROM crewattend WHERE Email = '$Email'"; $result = mysqli_query($cxn,$sql) or die("Couldn't execute select query."); $num = mysqli_num_rows($result); if ($num > 0) { $message_new = "This Email Address has already registered. Please re-enter your information."; include("reg_form.inc"); exit(); } /* Add new member to database */ else { $fields_str = implode(",",$fields); $values_str = implode('","',$values); $sql = "INSERT INTO crewattend "; $sql .= "(".$fields_str.")"; $sql .= " VALUES "; $sql .= "(".'"'.$values_str.'"'.")"; $result = mysqli_query($cxn,$sql) or die("Couldn't execute insert query. SQL statement is '$sql'"); $_SESSION['auth']="yes"; $_SESSION['email'] = $email; } break; default: include("reg_form.inc"); } ?> And this is the HTML:_ <?php /* File: reg_form.inc * Desc: Displays registration page. the * information needed to apply for a new account. */ include("function12.inc"); /* to include position wanted */ include("functionhow.inc"); /* how the user found out about the event */ ?> <html> <head> <title>Recruitment Fair Registration</title> <style type="text/css"><!-- .bold_right {font-weight: bold; text-align: left;} .gray_banner { font-weight: bold; color: white; background-color: gray; text-align: center; font-size: 3em;} .bold_large {font-size: 1.1em; font-weight: bold;} --></style> <SCRIPT TYPE="text/javascript"> <!-- function popup(mylink, windowname) { if (! window.focus)return true; var href; if (typeof(mylink) == 'tstring') href=mylink; else href=mylink.href; window.open(href, windowname, 'width=500,height=480,scrollbars=no,directories=no,status=no,toolbar=no'); return false; } //--> </SCRIPT> <script language="javascript" type="text/javascript"> var nReload = 5; function NewVerifyImage() { if (nReload <= 2) if (nReload <= 0) { alert("Sorry, too many reloads."); return; } else alert("Only " + nReload + " more reloads are allowed"); nReload--; // // This code now works in both IE and FireFox // var e_img; e_img = document.getElementById("vimg"); if (e_img) // // FireFox attempts to "optimize" by not reloading // the image if the URL value doesn't actually change. // So, we make it change by adding a different // count parameter for each reload. // e_img.setAttribute("src",e_img.src+'?count='+nReload); } // --> </script> </head> <body style="margin: 0"><center> <table width="64%" align="center" cellpadding="5"> <td><img src="triton_header_march_2007.gif"></td> </tr> </table> <!-- form for new member to fill in --> <form action="Registration.php" method="POST" enctype="multipart/form-data"> <table width="64%" align="center" name="SampleImgVerify"> <?php #60 if (isset($message_new)) { echo "<tr><td style='color: red; font-weight: bold' colspan='2'> <p>$message_new</p></td></tr>"; } ?> <tr><td class="bold_right">First Name</td> <td><input type="text" name="FirstName" value="<?php echo @$FirstName ?>" size="40" maxlength="40"></td></tr> <tr><td class="bold_right">Last Name</td> <td><input type="text" name="LastName" value="<?php echo @$LastName ?>" size="40" maxlength="40"></td></tr> <tr><td class="bold_right">Home Port</td> <td><input type="text" name="HomePort" value="<?php echo @$HomePort ?>" size="40" maxlength="40"></td></tr> <tr><td class="bold_right">Phone</td> <td><input type="text" name="Phone" value="<?php echo @$Phone ?>" size="15" maxlength="20"> </td></tr> <tr><td class="bold_right">Email Address</td> <td><input type="text" name="Email" value="<?php echo @$Email ?>" size="55" maxlength="67"></td></tr> <input type="hidden" name="do" value="new"> <tr><td class="bold_right">Desired Position</td> <td><select name="Position"> <?php $PositionName=getPositionName(); $PositionCode=getPositionCode(); for ($n=1;$n<=14;$n++) { $Position=$PositionName[$n]; $pcode=$PositionCode[$n]; echo "<option value='$pcode'"; if ($pcode== "1") echo " selected"; echo ">$Position\n"; } ?> </select> <tr><td class="bold_right">How did you find out about the recruitment fare?</td> <td><select name="How"> <?php $HowName=getHowName(); $HowCode=getHowCode(); for ($n=1;$n<=6;$n++) { $How=$HowName[$n]; $hcode=$HowCode[$n]; echo "<option value='$hcode'"; if ($hcode== "1") echo " selected"; echo ">$How\n"; } ?> </select> </table> <!-- This is where I want to put the browse facility for the file to upload --> <table width="64%" align="center"> <tr><td> </td> <td style="text-align: center"> <input type="submit" value="Register"></td> </tr> </table> </form> </td> </tr> <tr><td colspan="3" style="background-color: gray"> </td></tr> </table> </body> </html> Any pointers or guidance would be very much appreciated as I have been trying to figure out the logic on this for 2 days now without any success. Link to comment https://forums.phpfreaks.com/topic/128295-problem-with-file-uploads-in-a-form/#findComment-664625 Share on other sites More sharing options...
dropfaith Posted October 14, 2008 Share Posted October 14, 2008 Please use code tags <?php /* Program: Registration.php */ session_start(); include("dogs.inc"); switch (@$_POST['do']) { case "new": /* Check for blanks */ foreach($_POST as $field => $value) { if ($value == "") { $blanks[] = $field; } } if(isset($blanks)) { $message_new = "The following fields are blank. Please enter the required information: "; foreach($blanks as $value) { $message_new .= "$value, "; } extract($_POST); include("reg_form.inc"); exit(); } /* *******Validate data****** */ foreach($_POST as $field => $value) { if(!empty($value)) { if(eregi("firstName",$field) or eregi("lastName",$field)) { if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value)) { $errors[] = "$value is not a valid Name."; } } if(eregi("HomePort",$field)) { if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value)) { $errors[] = "$value is not a valid city."; } } if(eregi("phone",$field) ) { if(!ereg("^[0-9)(xX -]{7,20}$",$value)) { $errors[] = "$value is not a valid phone number. "; } } if(eregi("email",$field)) { if(!ereg("^.+@.+\\..+$",$value)) { $errors[] = "$value is not a valid email address."; } } if(eregi("Position",$field)) { if(!ereg("[0-9]{2}",$value)) { $errors[]="$value is not a valid position."; } } if(eregi("How",$field)) { if(!ereg("[0-9]{2}",$value)) { $errors[]="$value is not a valid entry for how you found out about the fare."; } } } // end if empty } // end foreach if(@is_array($errors)) { $message_new = ""; foreach($errors as $value) { $message_new .= $value." Please try again<br />"; } extract($_POST); include("reg_form.inc"); exit(); } /* ****End of Data Validation */ /* clean data */ $cxn = mysqli_connect($host,$user,$passwd,$dbname); foreach($_POST as $field => $value) { if($field != "Button" and $field != "do") { $fields[]=$field; $value = strip_tags(trim($value)); $values[] = mysqli_real_escape_string($cxn,$value); $$field = $value; } } /* check whether user name already exists */ $sql = "SELECT Email FROM crewattend WHERE Email = '$Email'"; $result = mysqli_query($cxn,$sql) or die("Couldn't execute select query."); $num = mysqli_num_rows($result); if ($num > 0) { $message_new = "This Email Address has already registered. Please re-enter your information."; include("reg_form.inc"); exit(); } /* Add new member to database */ else { $fields_str = implode(",",$fields); $values_str = implode('","',$values); $sql = "INSERT INTO crewattend "; $sql .= "(".$fields_str.")"; $sql .= " VALUES "; $sql .= "(".'"'.$values_str.'"'.")"; $result = mysqli_query($cxn,$sql) or die("Couldn't execute insert query. SQL statement is '$sql'"); $_SESSION['auth']="yes"; $_SESSION['email'] = $email; } break; default: include("reg_form.inc"); } ?> <?php /* File: reg_form.inc * Desc: Displays registration page. the * information needed to apply for a new account. */ include("function12.inc"); /* to include position wanted */ include("functionhow.inc"); /* how the user found out about the event */ ?> <html> <head> <title>Recruitment Fair Registration</title> <style type="text/css"><!-- .bold_right {font-weight: bold; text-align: left;} .gray_banner { font-weight: bold; color: white; background-color: gray; text-align: center; font-size: 3em;} .bold_large {font-size: 1.1em; font-weight: bold;} --></style> <SCRIPT TYPE="text/javascript"> <!-- function popup(mylink, windowname) { if (! window.focus)return true; var href; if (typeof(mylink) == 'tstring') href=mylink; else href=mylink.href; window.open(href, windowname, 'width=500,height=480,scrollbars=no,directories=no,status=no,toolbar=no'); return false; } //--> </SCRIPT> <script language="javascript" type="text/javascript"> var nReload = 5; function NewVerifyImage() { if (nReload <= 2) if (nReload <= 0) { alert("Sorry, too many reloads."); return; } else alert("Only " + nReload + " more reloads are allowed"); nReload--; // // This code now works in both IE and FireFox // var e_img; e_img = document.getElementById("vimg"); if (e_img) // // FireFox attempts to "optimize" by not reloading // the image if the URL value doesn't actually change. // So, we make it change by adding a different // count parameter for each reload. // e_img.setAttribute("src",e_img.src+'?count='+nReload); } // --> </script> </head> <body style="margin: 0"><center> <table width="64%" align="center" cellpadding="5"> <td><img src="triton_header_march_2007.gif"></td> </tr> </table> <!-- form for new member to fill in --> <form action="Registration.php" method="POST" enctype="multipart/form-data"> <table width="64%" align="center" name="SampleImgVerify"> <?php #60 if (isset($message_new)) { echo "<tr><td style='color: red; font-weight: bold' colspan='2'> <p>$message_new</p></td></tr>"; } ?> <tr><td class="bold_right">First Name</td> <td><input type="text" name="FirstName" value="<?php echo @$FirstName ?>" size="40" maxlength="40"></td></tr> <tr><td class="bold_right">Last Name</td> <td><input type="text" name="LastName" value="<?php echo @$LastName ?>" size="40" maxlength="40"></td></tr> <tr><td class="bold_right">Home Port</td> <td><input type="text" name="HomePort" value="<?php echo @$HomePort ?>" size="40" maxlength="40"></td></tr> <tr><td class="bold_right">Phone</td> <td><input type="text" name="Phone" value="<?php echo @$Phone ?>" size="15" maxlength="20"> </td></tr> <tr><td class="bold_right">Email Address</td> <td><input type="text" name="Email" value="<?php echo @$Email ?>" size="55" maxlength="67"></td></tr> <input type="hidden" name="do" value="new"> <tr><td class="bold_right">Desired Position</td> <td><select name="Position"> <?php $PositionName=getPositionName(); $PositionCode=getPositionCode(); for ($n=1;$n<=14;$n++) { $Position=$PositionName[$n]; $pcode=$PositionCode[$n]; echo "<option value='$pcode'"; if ($pcode== "1") echo " selected"; echo ">$Position\n"; } ?> </select> <tr><td class="bold_right">How did you find out about the recruitment fare?</td> <td><select name="How"> <?php $HowName=getHowName(); $HowCode=getHowCode(); for ($n=1;$n<=6;$n++) { $How=$HowName[$n]; $hcode=$HowCode[$n]; echo "<option value='$hcode'"; if ($hcode== "1") echo " selected"; echo ">$How\n"; } ?> </select> </table> <!-- This is where I want to put the browse facility for the file to upload --> <table width="64%" align="center"> <tr><td> </td> <td style="text-align: center"> <input type="submit" value="Register"></td> </tr> </table> </form> </td> </tr> <tr><td colspan="3" style="background-color: gray"> </td></tr> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/128295-problem-with-file-uploads-in-a-form/#findComment-664627 Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 You will find several examples here http://us2.php.net/features.file-upload Link to comment https://forums.phpfreaks.com/topic/128295-problem-with-file-uploads-in-a-form/#findComment-664628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.