bmccarthy Posted April 13, 2012 Share Posted April 13, 2012 I can't figure out what the heck is wrong with this code. When I attempt to use it, it get an error message from my browser that the file is under maintenance or configured incorrectly. I am attempting to have a user complete a form field on my website, upload a document, and send the email to me with the file attached. <?php $subject = 'I am from '.$_POST['MID'].' '; $emailadd = '[email protected]'; $req = '1'; $text = "I declare that I have read and understand the Guidelines and that I comply within these guidelines. :\n\n"; $space = ' '; $line = ' '; foreach ($_POST as $key => $value) { if ($req == '1') { if ($value == '') {echo "$key is empty";die;} } $j = strlen($key); if ($j >= 20) {echo "Name of form element $key cannot be longer than 20 characters";die;} $j = 20 - $j; for ($i = 1; $i <= $j; $i++) {$space .= ' ';} $value = str_replace('\n', "$line", $value); $conc = "{$key}:$space{$value}$line"; $text .= $conc; $space = ' '; } $file_name = $_FILES['userfile']; $new_file_name = $file_name . trim($_POST['MID']); $path= "Confirmation/" . $new_file_name; if($userfile) { if(copy($_FILES['userfile']['tmp_name'], $path)) { echo "Successful<BR/>"; echo "File Name :" . $new_file_name."<BR/>"; echo "File Size :" . $_FILES['userfile']['size']."<BR/>"; echo "File Type :" . $_FILES['userfile']['type']."<BR/>"; } else { echo "Error, file did not upload correctly!"; } mail($emailadd, $subject, $text, 'From: '.$_POST['Name'].' <'.$_POST['Email'].'>'); header("Location: compliance/confirmation.html"); ?> What am I doing wrong? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/ Share on other sites More sharing options...
batwimp Posted April 13, 2012 Share Posted April 13, 2012 Try using move_uploaded_file() instead of copy() http://us.php.net/manual/en/function.move-uploaded-file.php Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337100 Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 Where is $userfile being defined? You're checking for it before you perform the move. You should program with all errors showing. Check my sig. You'd be seeing an undefined variable notice. Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337103 Share on other sites More sharing options...
bmccarthy Posted April 13, 2012 Author Share Posted April 13, 2012 @batwimp I replaced copy() with move_uploaded_file(), but still receive the configuration error message. @xyph $userfile is the key/name within the my html form on the webpage. The html code for the form is: <form action="sendresults.php" method="post" enctype="multipart/form-data" name="PCI" target="_parent" id="PCI"> <table width="722" border="0"> <tr> <td width="193"> </td> <td width="161"><div align="left" class="style9">Name</div></td> <td width="199"><span id="sprytextfield5"> <label> <input name="Name" type="text" id="Name" size="45"> </label> <span class="textfieldRequiredMsg"></span></span></td> <td width="151"> </td> </tr> <tr> <td> </td> <td><div align="left" class="style9">Company Name</div></td> <td><span id="sprytextfield4"> <input name="Company" type="text" id="Company" size="45"> <span class="textfieldRequiredMsg"></span></span></td> <td> </td> </tr> <tr> <td> </td> <td><div align="left" class="style9">ID Number</div></td> <td><span id="sprytextfield3"> <input name="MID" type="text" class="style2" id="MID" size="54"> <span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td> <td> </td> </tr> <tr> <td> </td> <td><div align="left" class="style9">Email Address</div></td> <td><span id="sprytextfield2"> <input name="Email" type="text" class="style2" id="Email" size="54"> <span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td> <td> </td> </tr> <tr> <td> </td> <td><div align="left" class="style9">Phone Number</div></td> <td><span id="sprytextfield6"> <input name="Phone" type="text" class="style2" id="Phone" size="54"> <span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td> <td> </td> </tr> <tr> <td colspan="4"><div align="left"> <span id="sprycheckbox2"> <label></label> </span> <table width="100%" border="0" cellpadding="2"> <tr> <td><label for="userfile"> <div align="center">Select a file: </label> <input type="file" name="userfile"> <br /> </div></td> </tr> </table> <br> </div></td> </tr> <tr> <td colspan="4"><div align="left"> <span id="sprycheckbox1"> <label> <span class="style1"> <span class="style2"> <input type="checkbox" name="Certify" id="Certify"> </span></span></label> </span><span class="style9"> I declare that I have read and understand the Security Guidelines and that I comply within these guidelines. I am Compliant</span>.<br> </div></td> </tr> <tr> <td colspan="4"><div align="right"><span class="style12"> <label> *All Fields Are Required <div align="center"> <input type="submit" name="I am Compliant" value="Submit"> </div> </label></td> </tr> </table> </form> Also, where in my .php do I put the error checker? I tried placing it after the mail command, but...? Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337124 Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 No it isn't. Again, code with errors being displayed. You'll see it's undefined in your upper script. Read this http://www.php.net/manual/en/features.file-upload.post-method.php In the future, you should consider what people who want to help you are trying to say, rather than dismissing it as incorrect advice. Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337125 Share on other sites More sharing options...
batwimp Posted April 13, 2012 Share Posted April 13, 2012 xyph, could it be a register_globals thing? Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337127 Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 It could, but he's using the proper $_POST/$_FILES superglobal elsewhere. A 'better' assumption would be that he's using an undefined variable. Or he could be using a version of PHP older than 4.2. Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337131 Share on other sites More sharing options...
bmccarthy Posted April 13, 2012 Author Share Posted April 13, 2012 @xyph I apologize if I came across as dismissive. That was not my intention. I am a rookie at php so I don't necessarily know what I'm talking about. Where in the code do I place the error checker? I tried placing it just below the mail command, but that didn't work. Where is the proper placement of it? Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337150 Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 Google will help you with little things like that. The code in my sig should be placed at the top of your script. I'll modify my signature to include that. Quote Link to comment https://forums.phpfreaks.com/topic/260872-file-attachment-code-please-help/#findComment-1337169 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.