2016-04-15 381 views
0

我有一個處理上傳的CSV到臨時目錄的PHP腳本,然後我有5行代碼將CSV轉換爲JSON。使用file_get_contents()處理上傳文件

我的PHP腳本:

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 

的問題是在這條線:print_r($csvToJson);這是輸出:

[["\/tmp\/phpYeuuBB"]] 

這是我的臨時文件的文件路徑,我究竟做錯了什麼?

這裏是我的CSV是什麼樣子 -

CSV Demo

更新:我的JSON是不正確格式化「和\旁邊名

{"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]]"]} 

應該如何 -

{"data":[["Debra Brown"]]} 

當我打印$csv

enter image description here

回答

2

看起來你正在輸入路徑到您的CSV到str_getcsv

$csvArray = array_map("str_getcsv", explode("\n", $csvFile)); 

,而不是實際的CSV內容

$csvArray = array_map("str_getcsv", explode("\n", $csv)); 
2

您在文件分配給變量$csv但你這樣做是:

$csvArray = array_map("str_getcsv", explode("\n", $csvFile)); 

變量$csvFile仍然包含的文件路徑您你之前設置爲$sourcePath的文件。從file_get_contents()返回的實際文件字符串是$csv。如果將其更改爲如下所示:

$csvArray = array_map("str_getcsv", explode("\n", $csv)); 

您可能會發現這解決了您的問題。

這裏有the docsfile_get_contents()

+0

完美工作的感謝,任何機會你會知道爲什麼我的JSON在它和引號之前有\ \檢查我更新的帖子。 – Kieron606

+0

@ Kieron606首先,如果您打印這些文件,是否在'$ csv'中存在這些斜線?其次,在'str_getcsv()'中會發生什麼?也許這是你遇到的問題的一部分。 – Henders

+0

'$ csv'看起來很完美 – Kieron606