2014-01-05 95 views
-1

我試圖從JSON數組中創建一個.csv文件,該文件在頁面中檢測到提交事件後從JavaScript傳遞到php文件。將JSON數組從JavaScript傳遞到PHP並轉換爲csv

我到了可以看到從JavaScript返回的數組的位置,就像JSON一樣......它將它傳遞給php文件,然後讓php文件解析對象並將其輸出到.csv文件中文件

我已經包含了HTML,JavaScript和PHP,我正在使用它來做到這一點...我能指出正確的方向嗎?

我得到的錯誤有以下變化:

當我嘗試手動進我的數組:

http://localhost/~admin/dev/parse.php?submittedResults=[[%22uploads/featured/doublewm-3683.jpg%22,%22featured%22]] 

[Sat Jan 04 22:12:29 2014] [error] [client ::1] PHP Warning: file_get_contents($path): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 62 
[Sat Jan 04 22:12:29 2014] [error] [client ::1] PHP Warning: fopen(clients/downloads/csv/[["uploads/featured/doublewm-3683.jpg","featured"]].csv): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 65 
[Sat Jan 04 22:12:29 2014] [error] [client ::1] PHP Warning: Invalid argument supplied for foreach() in /Users/Admin/Sites/dev/parse.php on line 66 
[Sat Jan 04 22:12:29 2014] [error] [client ::1] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /Users/Admin/Sites/dev/parse.php on line 69 

當我試圖讓JavaScript的做數組傳:

[Sat Jan 04 22:15:36 2014] [error] [client ::1] PHP Notice: Array to string conversion in /Users/Admin/Sites/dev/parse.php on line 57, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:15:36 2014] [error] [client ::1] PHP Warning: file_get_contents($path): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 62, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:15:36 2014] [error] [client ::1] PHP Warning: fopen(clients/downloads/csv/Array.csv): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 65, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:15:36 2014] [error] [client ::1] PHP Warning: Invalid argument supplied for foreach() in /Users/Admin/Sites/dev/parse.php on line 66, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:15:36 2014] [error] [client ::1] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /Users/Admin/Sites/dev/parse.php on line 69, referer: http://localhost/~Admin/dev/index.htm 

HTML:

<form onsubmit""> 
     <div class="imageTile"> 
     <a class="fancybox" rel="rel" href="uploads/featured/doublewm-4097.jpg"><img src="uploads/featured/doublewm-4097.jpg" style="max-width: 120px; max-height: 110px;"></a><br> 
     <input class="data" type="hidden" name="imageFilename" value="uploads/featured/doublewm-4097.jpg"> 
     <input class="data" type="hidden" name="imageGalleryID" value="featured"> 
     <label for="uploads/featured/doublewm-4097.jpg">Keep<input class="checkbox" type="checkbox"></label> 
    </div> 
    <div class="imageTile"> 
     <a class="fancybox" rel="rel" href="uploads/featured/moon-5469.jpg"><img src="uploads/featured/moon-5469.jpg" style="max-width: 120px; max-height: 110px;"></a><br> 
     <input class="data" type="hidden" name="imageFilename" value="uploads/featured/moon-5469.jpg"> 
     <input class="data" type="hidden" name="imageGalleryID" value="featured"> 
     <label for="uploads/featured/moon-5469.jpg">Keep<input class="checkbox" type="checkbox"></label> 
    </div> 
    <div id="submit_buttons"> 
     <button type="reset">Reset</button> 
     <input class="submit" type="submit" onclick="return false" value="Submit"> 
    </div> 
</form> 

JAVASCRIPT:

$('.submit').click(function(event) { 
    event.preventDefault(); 
    var imageTile = this.parentElement.parentElement.getElementsByClassName('imageTile'); 
    var l = imageTile.length; 
    var data = []; 
    for (var i = 0; i < l; i++) { 
     if (imageTile[i].getElementsByClassName('checkbox')[0].checked) { 
      var dat = imageTile[i].getElementsByClassName('data'); 
      console.log(dat); 
      var ll = dat.length; 
      var datArr = []; 
      for (var j = 0; j < ll; j++) { 
       datArr.push(dat[j].attributes['value'].value); 
      } 
      data.push(datArr); 
     } 
    } 
    // Now 'data' is the array 

    // dataString is a JSON representation of the array to send to the server 
    var dataString = JSON.stringify(data); 

    // Test 
    console.log(data); 
    console.log(dataString); 

    $.ajax({ 
     url: 'parse.php?submittedResults' + dataString, 
     type: 'GET', 
     dataType: "json", 
     async: false 
    }).done(function(data) { 

    }) 
}); 

PHP:

if (isset($_GET['submittedResults'])) { 
    if (empty($_GET['submittedResults'])) { 
     die('Give me something to work with!!'); 
    } 
    $resultSet = (string) $_GET['submittedResults']; 
    $path = $resultSet; 
    if (strpos($path, '../') !== false || strpos($path, "..\\") !== false || strpos($path, '/..') !== false || strpos($path, '\..') !== false) { 
     http_response_code(403); 
    } else { 
     $getFile = file_get_contents('$path'); 
     $json_obj = json_decode($getFile); 
     echo "$json_obj"; 
     $fp = fopen("clients/downloads/csv/$path.csv", 'w'); 
     foreach ($json_obj as $row) { 
      fputcsv($fp, $row); 
     } 
     fclose($fp); 
    } 
} 

JSON:

[["uploads/featured/doublewm-4097.jpg","featured"],["uploads/featured/moon-5469.jpg","featured"]] 

所以,我沒有通知我缺少 '=' 號......我重做了Ajax調用匹配你的建議,行爲是一樣的。我還包括按要求如下這裏的回聲結果如下:

請求的回聲:

var_dump($_GET['submittedResults']) 

string(101) "[["uploads/featured/doublewm-3683.jpg","featured"],["uploads/featured/doublewm-3935.jpg","featured"]]" 

當前日誌:

[Sat Jan 04 22:27:43 2014] [error] [client ::1] PHP Warning: file_get_contents($path): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 63, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:27:43 2014] [error] [client ::1] PHP Warning: fopen(clients/downloads/csv/[[&quot;uploads/featured/doublewm-3683.jpg&quot;,&quot;featured&quot;],[&quot;uploads/featured/doublewm-3935.jpg&quot;,&quot;featured&quot;]].csv): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 66, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:27:43 2014] [error] [client ::1] PHP Warning: Invalid argument supplied for foreach() in /Users/Admin/Sites/dev/parse.php on line 67, referer: http://localhost/~Admin/dev/index.htm 
[Sat Jan 04 22:27:43 2014] [error] [client ::1] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /Users/Admin/Sites/dev/parse.php on line 70, referer: http://localhost/~Admin/dev/index.htm 

當前的JavaScript:

$.ajax({ 
    url: 'parse.php', 
    data: { submittedResults: JSON.stringify(data) }, 
    type: 'GET', 
    dataType: "json", 
    async: false 
}). 
+0

是否有問題? –

+0

我沒有得到預期的.csv文件......我相當積極的數組是讓它回到php腳本 – user3161804

+0

你需要解釋什麼是不工作,要詳細,將其添加到您的帖子 – meda

回答

0

PHP警告:file_get_contents($ path):失敗打開流:沒有這樣的 文件或目錄在/Users/Admin/Sites/dev/parse.php在線62

$resultSet = (string) $_GET['submittedResults']; 
$path = $resultSet; 

這是造成你一個問題,因爲$ PATH等於JSON數據。 這是錯誤的,路徑應該是一個文件名。試試這樣:

//$resultSet =(string) $_GET['submittedResults']; 
$resultSet = '[["uploads/featured/doublewm-4097.jpg","featured"], 
      ["uploads/featured/moon-5469.jpg","featured"]]'; 
$path = "myfile.csv"; //or clients/downloads/csv/myfile.csv 
if (strpos($path, '../') !== false || strpos($path, "..\\") !== false 
    || strpos($path, '/..') !== false || strpos($path, '\..') !== false) { 
    http_response_code(403); 
} else { 
    $json_obj = json_decode($resultSet); 
    echo "$json_obj"; 
    $fp = fopen($path, 'w'); 
    foreach ($json_obj as $row) { 
     fputcsv($fp, $row); 
    } 
    fclose($fp); 
} 

MYFILE。CSV:

uploads/featured/doublewm-4097.jpg,featured 
uploads/featured/moon-5469.jpg,featured 

編輯: 給出一個唯一的文件名

$json_obj = json_decode($resultSet); 
$path = $json_obj[0][1].".csv"; 

輸出

featured.csv

+0

[星期六1月4日22時41分07秒2014] [錯誤] [客戶:: 1] PHP的警告:FOPEN(myfile.csv):未能打開流:權限在第64行的/Users/Admin/Sites/dev/parse.php中被拒絕,引用:http://localhost/~Admin/dev/index.htm [Sat Jan 04 22:41:07 2014] [error ] [client :: 1] PHP注意:未定義的變量:第65行的/Users/Admin/Sites/dev/parse.php中的json_obj,參考:http://localhost/~Admin/dev/index.htm [Sat Jan 04 22:41:07 2014] [error] [client :: 1] PHP警告:無效的參數提供d用於第65行的/Users/Admin/Sites/dev/parse.php中的foreach(),referer:http://localhost/~Admin/dev/index.htm – user3161804

+0

@ user3161804我把完整的代碼,你可以測試它就像這樣'$ resultSet ='[[「uploads/featured/doublewm-4097.jpg」,「featured」],[「uploads/featured/moon-5469.jpg」,「featured」]]';'你也根本不需要'file_get_contents' – meda

+0

我試過並且沒有結果集硬編碼的錯誤是一樣的 – user3161804