0

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.

upload form

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.

wa7d
  • 129
  • 1
  • 2
  • 12
  • @mario 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. Can you remove the duplicate mark? – wa7d Oct 04 '18 at 01:22
  • Once you copy the uploaded file, you have an existing file on the server to append to. I don't see how the existing answer would not apply. – mario Oct 04 '18 at 01:26
  • I'm interested in adding the data from the field to the file prior to uploading it. The folder on the server is empty until the file along with the data are all in the same file, then the file is uploaded. I'm not interested in uploading then updating. Do you see why it doesn't apply? I'm not trying to deal with an existing file. – wa7d Oct 04 '18 at 01:33
  • Why the inappropriate language? If you know things already others are trying to learn. – wa7d Oct 04 '18 at 01:41

0 Answers0