Hello all - this is my first post as a newbie, so apologies if I get a few things wrong!
I have been tasked with the following problem, and although I am attempting to solve it I am having great trouble getting all the individual components to work together. I can get each of the two main elements working individually, but need to combine them properly.
Here's the outline -
Take the contents from a form and convert it to CSV. The csv must contain 30 or so values, seperated by commas. Text fields in the CSV must have " around them, eg 1, 33.2,"hello",24,"John Bloggs",
A user must be able to upload a file to the server, the path of which is saved as part of the csv, eg "/home/user/uploads/file.doc
I have got both the csv creation and file upload to work, but need to find a way to combine the two scripts -both are based on tutorials, but appear to be coded using different methods which has left me confused as to how to integrate them.
I am sure my code here is very poor and would be delighted to know how it could be optimised!
This is the first part, which creates the cv based on submitted form data:
<?php
//CSV file with data of submitted forms //////////////
$content = $_POST[external_ref];
$content .=',';
$content .='"';
$content .= $_POST[last_name];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[first_name];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[title];
$content .='"';
$content .=',';
$content .=',';
$content .= $_POST[mobile_telno];
$content .=',';
$content .='"';
$content .= $_POST[email_address];
$content .='"';
$content .=',';
$content .= $_POST[date_of_birth];
$content .=',';
$content .='"';
$content .= $_POST[gender];
$content .='"';
$content .=',';
$content .=',';
$content .= $_POST[day_telno];
$content .=',';
$content .='"';
$content .= $_POST[address_line_1];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[address_line_2];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[address_line_3];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[post_town];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[county_state];
$content .='"';
$content .=',';
$content .='"';
$content .= $_POST[zipcode];
$content .='"';
$content .=',';
$content .=',';
$content .= $_POST[telephone_number];
$content .=',';
$content .= $_POST[fax_number];
$content .=',';
$content .='"';
$content .= $_POST[permanent];
$content .='"';
$content .=',';
//$content .='"';
//$content .= $_POST[seeking];
//$content .='"';
$content .=',';
$content .= $_POST[income_required];
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[cvpath];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code1];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code2];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code3];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code4];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code5];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code6];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code7];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code8];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code9];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code10];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code11];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code12];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code13];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code14];
$content .='"';
$content .=',';
$content .=',';
$content .='"';
$content .= $_POST[code15];
$content .='"';
$content .=',';
$content .='\n';
$filename = 'email.csv';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($content) to file ($filename)";
fclose($handle);
}
else {
echo "The file $filename is not writable";
}
?>
The second element that provides the path to the uploaded file looks like this:
<?php if ($_POST['_stage']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();
}
function show_form($errors = '') {
if ($errors) {
print 'You need to correct the following errors: <ul><li>';
print implode('</li><li>',$errors);
print '</li></ul>';
}
print<<<_HTML_
<form enctype="multipart/form-data" method="POST"
action="$_SERVER[PHP_SELF]">
File to Upload: <input name="my_file" type="file"/>
<input type="hidden" name="MAX_FILE_SIZE" value="131072"/>
<input type="hidden" name="_stage" value="1">
<input type="submit" value="Upload"/>
</form>
_HTML_;
}
function validate_form() {
$errors = array();
if (($_FILES['my_file']['error'] == UPLOAD_ERR_INI_SIZE)||
($_FILES['my_file']['error'] == UPLOAD_ERR_FORM_SIZE)) {
$errors[] = 'Uploaded file is too big.';
} elseif ($_FILES['my_file']['error'] == UPLOAD_ERR_PARTIAL) {
$errors[] = 'File upload was interrupted.';
} elseif ($_FILES['my_file']['error'] == UPLOAD_ERR_NO_FILE) {
$errors[] = 'No file uploaded.';
}
return $errors;
}
function process_form() {
print "You uploaded a file called {$_FILES['my_file']['name']} ";
print "of type {$_FILES['my_file']['type']} that is ";
print "{$_FILES['my_file']['size']} bytes long.";
$safe_filename = str_replace('/', '', $_FILES['my_file']['name']);
$safe_filename = str_replace('..', '', $safe_filename);
$destination_file = 'mydomain.com/httpdocs/registrations/' . $safe_filename;
if (move_uploaded_file($_FILES['my_file']['tmp_name'], $destination_file)) {
print "Successfully saved file as $destination_file.";
} else {
print "Couldn't save file in /registrations.";
}
}
?>
So you see (hopefully) that I needd to combine these two bits of code so that it all happens together, and that the path to the cv file is included in the CSV (it's currently listed as 'cvpath'.
I'd be most grateful for some pointers on how to achieve this!
Many thanks!!