EDIT: the question in the link you posted asks to append data to an already existing file. I'm asking a different question: a user chooses a file from their computer, then prior to uploading they fill out a few fields, then user clicks uploads where the file they chose along with the data entered are all in the same file. I don't think it's duplicate.
I'm able to upload a csv file to a folder on my server using the following PHP:
<?php
if ( isset($_POST['submit']) ) {
$file = $_FILES['file'];
$file_name = $_FILES['file']['name'];
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_error = $_FILES['file']['error'];
$file_type = $_FILES['file']['type'];
$file_ext = explode('.', $file_name);
$file_actual_ext = strtolower(end($file_ext));
$allowed = array('csv', 'CSV', 'xltx', 'xlx', 'pdf', 'xlx');
if( in_array($file_actual_ext, $allowed) ) {
if( $file_error === 0 ) {
if( $file_size < 100000000 ) {
$file_new_name = uniqid('', true).".".$file_actual_ext;
$file_dest = '/data_uploaded/'.$file_new_name;
move_uploaded_file($file_tmp_name, $file_dest);
echo 'Success! Data has been uploaded';
exit;
} else {
echo "File is too big...";
}
} else {
echo "Error occurred during upload...";
}
} else {
echo "Cannot upload files of this type..";
}
}
I can upload a csv file, but is there a way where I can add the data from the fields (Site, site ID, etc.) to the file I'm trying to upload and then upload it to the server? So providing a csv file with data such as 1, 2, 3. Then, adding the other data to the file so it becomes 1, 2, 3 [newline] Site, Site ID, Owner, Interval, Location.