Example #1
0
/**
 * Handle and read uploaded file
 * @param array - fileArray from form
 * @param array - postArray from form
 */
function handleUpload($_FILES, $_POST)
{
    require_once "JotForm.php";
    $path = mktime() . '_' . $_FILES['file']['name'];
    $key = $_POST['APIkey'];
    $form = $_POST['formID'];
    $jot = new JotForm($key);
    $error = "";
    if (move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
        $fileType = getFileType($path);
        $columns = array();
        if ($fileType == 'csv') {
            if (($handle = fopen($path, "r")) !== FALSE) {
                if (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    foreach ($data as $title) {
                        array_push($columns, $title);
                    }
                }
                $error = 'File must contain at least two rows - the first represents the field titles';
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    $error = '';
                    $result = $jot->createFormSubmissions($form, writeData($data, $columns));
                }
                fclose($handle);
            } else {
                $error = 'Could not open file';
            }
        } else {
            require_once 'Excel/reader.php';
            $excel = new Spreadsheet_Excel_Reader();
            $excel->read($path);
            if ($excel->sheets[0]['numRows'] > 1) {
                for ($i = 1; $i <= $excel->sheets[0]['numCols']; $i++) {
                    $title = $excel->sheets[0]['cells'][1][$i];
                    array_push($columns, $title);
                }
                for ($i = 2; $i <= $excel->sheets[0]['numRows']; $i++) {
                    $data = array();
                    for ($j = 1; $j <= $excel->sheets[0]['numCols']; $j++) {
                        array_push($data, $excel->sheets[0]['cells'][$i][$j]);
                    }
                    $jot->createFormSubmissions($form, writeData($data, $columns));
                }
            } else {
                $error = 'File must contain at least two rows - the first represents the field titles';
            }
        }
    } else {
        $error = 'No File Found';
    }
    if (strlen($error) > 0) {
        return $error;
    } else {
        return 'none';
    }
}