emediastudios Posted November 9, 2010 Share Posted November 9, 2010 Hi everyone, im a newbie and have written this script, has taken me a day to get here. The form inserts data into the database, uploads a file and sends an email. All this works fine, what i am trying to do is rename the file to the id of the record and check to make sure it is a pdf. I would really appreciate any help. I've tried and tried and just cant get it to work. <?php switch ($_REQUEST['action']) { case 'recruit': foreach($_POST as $key=>$value){ $$key = $value; } if ((!$name) || (!$email) || (!$phone)) { $error_msg = 'Fields marked<span class="gold"> * </span>are required to submit the form'; }elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $error_msg = 'Invalid email address'; } echo "$error_msg","<br><br>"; if ($error_msg == ''){ $date=date("d/m/y", time()); $add="recruitment/".$_FILES[userfile][name]; $cleaned = stripit($add); $add2 = $cleaned; if(move_uploaded_file ($_FILES[userfile][tmp_name], $add2)); $Q = mysql_query("INSERT INTO recruitment (`name`,`phone`,`email`, `qual`,`exper`,`file`) VALUES ('$name','$phone','$email','$qual','$exper','$cleaned')"); foreach($_POST as $key=>$value){ $$key = htmlentities(stripslashes($value)); } $companyname = 'Mead Business college'; $companyemail = 'ross@emediastudios.com.au'; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: ".$name." <".$email.">\r\n"; $headers .= "Reply-To: ".$name." <".$email.">\r\n"; $to = "".$companyname."<".$companyemail.">"; $subject = "Mead Business College Recruitment Form Submission"; $message = '<style type="text/css>"; <!-- .style { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; } --> </style> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="style"> <b>Details:</b><br /><br /> <b>Name:</b> '.$name.'<br /> <b>Email:</b> '.$email.'<br /> <b>Mobile No:</b> '.$phone.'<br /> <b>Qualifications:</b><br> '.$qual.'<br /> <b>Experience:</b><br /><br />'.$exper.'<br /><br /> <b>Uploaded resume:</b> http://www.mydomain.com.au/'.$cleaned.' </td> </tr> </table>'; mail($to, $subject, $message, $headers); echo '<table width="99%" border="0" cellpadding="0" cellspacing="0"> <tr> <td class="mybody">Hi '.$name.',<p />Thank you for your enquiry. An MBC Consultant will contact you shortly.<br /> <br /> </td> </tr> </table>'; }else{ foreach($_POST as $key=>$value){ $$key = htmlentities(stripslashes($value)); } echo ' <FORM ENCTYPE="multipart/form-data" id="form" name="Contact Form" method="post" action="'.$_SERVER['PHP_SELF'].'?action=recruit"> <table width="489" border="0" cellspacing="5" cellpadding="0" class="formsw"> <tr> <td width="227">Name:</td> <td width="358"><input type="text" name="name" id="name" value="'.$name.'" /></td> </tr> <tr> <td>Contact Mobile:</td> <td><input type="text" name="phone" id="phone" value="'.$phone.'" /></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" id="email" value="'.$email.'" /></td> </tr> <tr> <td>Relevent Qualifications:</td> <td><textarea name="qual" id="qual" cols="45" rows="5" value="'.$qual.'"></textarea></td> </tr> <tr> <td>Recent Experience:</td> <td><textarea name="exper" id="exper" cols="45" rows="5" value="'.$exper.'"></textarea></td> </tr> <tr> <td>Upload Resume:</td> <td><input type="file" name="userfile" id="userfile" /></td> </tr> <tr> <td><input type="submit" name="submit" id="submit" value="Submit" /></td> <td> </td> </tr> </table> </form>'; } break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/ Share on other sites More sharing options...
OldWest Posted November 9, 2010 Share Posted November 9, 2010 To check if it's a valid pdf, you should probably use ereg("\.pdf$",$filename) .. Something along those lines.. And for the renaming of the file try rename("$originalfile_name", "$newfile_name") Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132050 Share on other sites More sharing options...
gizmola Posted November 9, 2010 Share Posted November 9, 2010 When you call move_uploaded_file, just specify the name you want the file to have in the destination parameter along with the path. Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132054 Share on other sites More sharing options...
emediastudios Posted November 9, 2010 Author Share Posted November 9, 2010 Thnks for the replies. I tried but no luck, i tried adding a random number function and failed. Anychance of a sample please, i'll keep reading up and see if i can get it. the script does everything it should, except the rename and pdf check. Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132065 Share on other sites More sharing options...
OldWest Posted November 9, 2010 Share Posted November 9, 2010 MIght be causing an issue is you have a space in your function name: if(move_uploaded_file ($_FILES[userfile][tmp_name], $add2)); ...ded_file ($_FIL... Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132068 Share on other sites More sharing options...
emediastudios Posted November 9, 2010 Author Share Posted November 9, 2010 Na, that space is ok, script works fine, just want to add rename of file and check to make sure is a PDF, will pay pal someone a Quick $20 if they can do it for me, i'm over it and have heaps of other work to do. Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132070 Share on other sites More sharing options...
OldWest Posted November 9, 2010 Share Posted November 9, 2010 This might help on 1/2 half of your issue: if (preg_match("/.pdf/i",$add)) { $cleaned = stripit($add); } else { echo "Sorry bud. That's not a pdf!"; exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132080 Share on other sites More sharing options...
OldWest Posted November 9, 2010 Share Posted November 9, 2010 And what do you want the file named to. In other words did you have some naming convention in mind? A number, letters, combo? Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132081 Share on other sites More sharing options...
emediastudios Posted November 9, 2010 Author Share Posted November 9, 2010 Thanks champ, that the pdf issue resolved. As for the rename of the file, i was thinking, rename it to the persons name submitting the pdf, the $name value of the form and put a random number on the end for good measure, or if easier just the id number. Thanks again for your help Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132084 Share on other sites More sharing options...
OldWest Posted November 9, 2010 Share Posted November 9, 2010 Try adding some ' 's to your multi-array: $add="recruitment/".$_FILES['userfile']['name']; Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132092 Share on other sites More sharing options...
OldWest Posted November 9, 2010 Share Posted November 9, 2010 And your other one too: if(move_uploaded_file ($_FILES['userfile']['tmp_name'], $add2)); Quote Link to comment https://forums.phpfreaks.com/topic/218159-rename-uploaded-file/#findComment-1132096 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.