Created
April 23, 2013 22:25
-
-
Save ricardoalcocer/5447943 to your computer and use it in GitHub Desktop.
Quick and dirty proof-of-concept of uploading a flat CSV file onto a server and getting in return a JSON feed for that data. This is part of an idea I have for an online service that just does this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// **** | |
// Inspired by: https://gist.github.com/robflaherty/1185299 | |
class csv2json { | |
private static function csvToArray($file, $delimiter) { | |
if (($handle = fopen($file, 'r')) !== FALSE) { | |
$i = 0; | |
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { | |
for ($j = 0; $j < count($lineArray); $j++) { | |
$arr[$i][$j] = $lineArray[$j]; | |
} | |
$i++; | |
} | |
fclose($handle); | |
} | |
return $arr; | |
} | |
public static function parse($filePath){ | |
//header('Content-type: application/json'); | |
// Set your CSV feed | |
//$feed = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Akse3y5kCOR8dEh6cWRYWDVlWmN0TEdfRkZ3dkkzdGc&single=true&gid=0&output=csv'; | |
$feed = $filePath; | |
// Arrays we'll use later | |
$keys = array(); | |
$newArray = array(); | |
// Do it | |
$data = self::csvToArray($feed, ','); | |
// Set number of elements (minus 1 because we shift off the first row) | |
$count = count($data) - 1; | |
//Use first row for names | |
$labels = array_shift($data); | |
foreach ($labels as $label) { | |
$keys[] = $label; | |
} | |
// Add Ids, just in case we want them later | |
$keys[] = 'id'; | |
for ($i = 0; $i < $count; $i++) { | |
$data[$i][] = $i; | |
} | |
// Bring it all together | |
for ($j = 0; $j < $count; $j++) { | |
$d = array_combine($keys, $data[$j]); | |
$newArray[$j] = $d; | |
} | |
// Print it out as JSON | |
return json_encode($newArray); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<form action="upload_file.php" method="post" enctype="multipart/form-data"> | |
<label for="file">Filename:</label> | |
<input type="file" name="file" id="file"><br> | |
<input type="submit" name="submit" value="Submit"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// php setup | |
error_reporting(E_ALL); | |
ini_set('display_errors', 'On'); | |
date_default_timezone_set('America/Los_Angeles'); | |
ini_set("auto_detect_line_endings", true); | |
// | |
if ($_FILES["file"]["type"] == "text/csv"){ | |
if ($_FILES["file"]["error"] > 0){ | |
echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; | |
}else{ | |
$uploadPath=''; | |
$newFile=uniqid(); | |
move_uploaded_file($_FILES["file"]["tmp_name"],$uploadPath . $newFile); | |
// here's where the magic happens | |
include('csv2json.php'); | |
$csv2json=new csv2json; | |
header('Content-type: application/json'); | |
echo($csv2json->parse($uploadPath . $newFile)); | |
unlink($uploadPath . $newFile); | |
} | |
}else{ | |
echo "Invalid file"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment