Bladeoz Posted August 22, 2008 Share Posted August 22, 2008 Hey guys - nice forum you have here! I've just started getting really into PHP, and this place looks like a good resource for the future I wish this wasnt my introduction - asking for help first up :s Originally, I created php that takes information from a form and places it into an array. The information is then sent and the array data is looped out. This works perfectly fine. Now I need to convert this to upload doc and pdf files. I've spent hours looking through and testing various methods, but really I needed something simple that I can build upon after the basics work. In the end I found a script. I cannot remember the site I got this code from, but I am having issues with it. It was the simplist in uploading and testing basics (such as using explode() and then comparing to mime types), but it keeps flagging the error from the elseif statement regarding supported types. I cannot find why it is doing this. Any help would be greatly appreciated PHP <?php $fields = array(); $fields{"first_name"} = "first_name"; $fields{"last_name"} = "last_name"; $fields{"phone"} = "phone"; $fields{"mobile"} = "mobile"; $fields{"email"} = "email"; $fields{"address"} = "address"; $fields{"suburb"} = "suburb"; $fields{"state"} = "state"; $fields{"postcode"} = "postcode"; $fields{"how_did_you_hear"} = "how_did_you_hear"; $fields{"what_sort_of_role"} = "what_sort_of_role"; $fields{"how_soon_can_you_start"} = "how_soon_can_you_start"; //Gather file data and other things $tmp = $_FILES['upload']['tmp_name']; $sep = md5(time()); $filename = $_FILES['upload']['name']; $filedata = file_get_contents($tmp); //Get file contents $fdata = chunk_split(base64_encode($filedata)); //Encode data into text form $to = "[email protected]"; //Determine mime type $ext = explode('.', $filename); $ext = $ext[1]; if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") { $mime_type = "image/jpeg"; } elseif($ext == "gif" || $ext == "GIF") { $mime_type = "image/gif"; } else { exit("Error: Wrong file type!"); } $message = "The following person would like to be contacted:\n\n"; foreach($fields as $a => $b){ $message .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } //Begin the headers $headers = "From: \"From Name\" <{$email}> MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary=\"$sep\" charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit --$sep Content-Type: $mime_type; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=\"$filename\" $fdata --$sep"; $subject = "Job resume submitted via form"; mail($to, $subject, $message, $headers); ?> HTML <p class="white_text" />upload your cv here: <!-- Name of input element determines name in $_FILES array --> <br /><input name="upload" type="file"> <p /> <button type="submit">Submit</button> <button type="reset" value="reset">Reset</button> </div> </form> Link to comment https://forums.phpfreaks.com/topic/120818-issue-with-form-that-sends-user-details-attachment/ Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Share Posted August 22, 2008 Okay, well instead of posting code in quote tags, post it in code tags. Doesn't matter but it's easier that way. I just looked at the extension part. Replace this line: $ext = $ext[1]; With this: $ext = $ext[count($ext)-1]; Not sure if it will help, but it could be that your file you are uploading has a period before the extension period. Link to comment https://forums.phpfreaks.com/topic/120818-issue-with-form-that-sends-user-details-attachment/#findComment-622794 Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 If it's the $ext that is giving you problems, try printing it's value to check if it is getting one. Do the same thing for the $filename and $tmp. Also your code for getting the file extension should work, but not if you have a file: "some.thing.jpg". In that case you would get "thing" as the extension. I usually use this code to get file extensions: <?php $file = 'some.thing.jpg'; $ext = strrchr($file, '.'); //get the last instance of a dot, resulting to: ".jpg" $ext = substr($ext, 1); //remove the dot as we don't need it $ext = strtolower($ext); //convert to lowercase //all the above functions could be written in one line $ext = strtolower(substr(strrchr($file, '.'), 1)); ?> And also a final note. Usually arrays are written as $array['index']. Using curly braces for arrays works, but would result in a confusing code in my opinion. EDIT: The method provided by ProjectFear for getting the extension should work too. Link to comment https://forums.phpfreaks.com/topic/120818-issue-with-form-that-sends-user-details-attachment/#findComment-622795 Share on other sites More sharing options...
Bladeoz Posted August 23, 2008 Author Share Posted August 23, 2008 thank you for the quick response and code guys I have tried to substitute both of your responses with no luck unfortunately. The same error is playing up. I have also tested the values of $filename, $tmp and also $ext within their respective scopes. If I test the variable type, I get an error and if I try to print the output to screen I get nothing. At worst, can anyone link me to a really basic script that allows me to upload form entires and add an attachment? Link to comment https://forums.phpfreaks.com/topic/120818-issue-with-form-that-sends-user-details-attachment/#findComment-623682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.