Ich habe ein PHP-Skript, das hochgeladene CSVs in ein temporäres Verzeichnis behandelt, ich habe dann 5 Zeilen Code, um von CSV in JSON zu konvertieren.Handhabung der Upload-Datei mit file_get_contents()
Mein PHP-Skript:
if (isset($_FILES['csvList']['type'])) {
$validExtensions = array("csv");
$temporary = explode(".", $_FILES["csvList"]["name"]);
$file_extension = end($temporary);
if (in_array($file_extension, $validExtensions)) {
if ($_FILES["csvList"]["error"] > 0) {
echo "Return Code: " . $_FILES["csvList"]["error"] . "<br/><br/>";
} else {
if (file_exists("/var/www/tmp/" . $_FILES["csvList"]["name"])) {
echo $_FILES["csvList"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
} else {
$sourcePath = $_FILES['csvList']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "/var/www/tmp/".$_FILES['csvList']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
$csvFile = $sourcePath;
$csv = file_get_contents($csvFile);
$csvArray = array_map("str_getcsv", explode("\n", $csvFile));
$csvToJson = json_encode($csvArray);
print_r($csvToJson);
}
}
}
}
$sourcePath = $_FILES['csvList']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "/var/www/tmp/".$_FILES['csvList']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
Das Problem ist auf dieser Linie:print_r($csvToJson);
. und dies ist die Ausgabe:
[["\/tmp\/phpYeuuBB"]]
Dies ist der Dateipfad meiner temporären Datei, was mache ich falsch?
Hier ist, was meine CSV aussieht -
Update: Meine JSON nicht richtig mit "und \ neben Namen
{"data":["[[\"Debra Brown\"],[\"Jacqueline Garza\"],[\"Kenneth Foster\"],[\"Antonio Howell\"],[\"Fred Rogers\"],[\"Robert Stanley\"],[\"Jesse Price\"],[\"Henry Bishop\"],[\"Marilyn Phillips\"],[\"Charles White\"],[\"Dennis Lawrence\"],[\"Nicholas Thompson\"],[\"Chris Graham\"],[\"Louis Dean\"],[\"Katherine Green\"],[\"Janice Peters\"],[\"Bobby Wood\"],[\"Bruce King\"],[\"Diane Mills\"],[\"Jane Fields\"],[\"Amanda Gutierrez\"],[\"Russell Cunningham\"],[\"Judith Matthews\"],[\"Carol Franklin\"],[\"Jose Murray\"],[\"Kathryn Cole\"],[\"Katherine Gardner\"],[\"Lois Woods\"],[\"Andrew Bryant\"],[\"Victor Wright\"],[\"Adam Russell\"],[\"Tina Gilbert\"],[\"Shawn Boyd\"],[\"Wanda Porter\"],[\"Rose Morris\"],[\"John Mccoy\"],[\"Frances Gibson\"],[\"Willie Lopez\"],[\"Chris Reyes\"],[\"Craig Vasquez\"],[\"Diane Simmons\"],[\"Mary Little\"],[\"Patricia Fowler\"],[\"Jane Perkins\"],[\"Juan Brooks\"],[\"Bruce Howard\"],[\"Tammy Richardson\"],[\"Jane Gomez\"],[\"Tammy Matthews\"],[\"Matthew Fox\"],[null]]"]}
Wie es sein sollte formatiert ist -
{"data":[["Debra Brown"]]}
Wenn ich drucken $csv
dank Arbeitete perfekt str_getcsv, jede Chance Du würdest wissen wieso mein JSON \ davor hat und zitiert? Überprüfen Sie meinen aktualisierten Beitrag. – Kieron606
@ Kieron606 Erstens, gibt es diese Schrägstriche in $ csv, wenn Sie das drucken? Zweitens, was passiert in 'str_getcsv()'? Vielleicht ist das ein Teil des Problems, das du hast. – Henders
'$ csv' sieht perfekt aus – Kieron606