Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am attempting to create a .csv file from a JSON array passed into a php file from JavaScript after submit event is detected in the page.

I am to the point where I can see the array coming back from the JavaScript as JSON... It is passing it to the php file, and then having the php file parse the object and output it into a .csv file

I've included the HTML, JavaScript, and php that I'm using to make it this far... Can I get pointed in the right direction?

The errors I get vary with the following:

When I attempt to inject my array manually:

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

When I attempt to let JavaScript to do the array passing:

[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"]] 

So I did notice I was missing the '=' sign... I redid the ajax call to match what you suggested, the behavior is the same. I also included the echo as requested below here are the results:

The requested echo:

 var_dump($_GET['submittedResults'])

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

The current Logs:

[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

Current JavaScript:

$.ajax({
    url: 'parse.php',
    data: { submittedResults: JSON.stringify(data) },
    type: 'GET',
    dataType: "json",
    async: false
}).
share|improve this question
 
Is there a problem? –  Digital Chris yesterday
 
I do not get the .csv file as expected... I am fairly positive the array is making it back into the php script –  user3161804 yesterday
 
You need to be explain what is not working, be detailed, add it to your post –  meda yesterday
2  
fairly positive eh? Lets test that. var_dump($_GET['submittedResults']) –  Digital Chris yesterday
 
localhost/~Admin/dev/parse.php?submittedResults[[%22uploads/… string(101) "[["uploads/featured/doublewm-3683.jpg","featured"],["uploads/featured/doublewm-‌​3935.jpg","featured"]]" –  user3161804 yesterday
add comment

2 Answers

up vote 1 down vote accepted

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

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

this is causing you a problem because $path is equal to the json data. Which is wrong, path should be a filename. Try like this :

//$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

EDIT: Give a unique filename

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

output

featured.csv

share|improve this answer
 
[Sat Jan 04 22:41:07 2014] [error] [client ::1] PHP Warning: fopen(myfile.csv): failed to open stream: Permission denied in /Users/Admin/Sites/dev/parse.php on line 64, referer: localhost/~Admin/dev/index.htm [Sat Jan 04 22:41:07 2014] [error] [client ::1] PHP Notice: Undefined variable: json_obj in /Users/Admin/Sites/dev/parse.php on line 65, referer: localhost/~Admin/dev/index.htm [Sat Jan 04 22:41:07 2014] [error] [client ::1] PHP Warning: Invalid argument supplied for foreach() in /Users/Admin/Sites/dev/parse.php on line 65, referer: localhost/~Admin/dev/index.htm –  user3161804 yesterday
 
@user3161804 I put the full code, you can test it like this $resultSet = '[["uploads/featured/doublewm-4097.jpg","featured"],["uploads/featured/moon-5469‌​.jpg","featured"]]'; also you dont need file_get_contents at all –  meda yesterday
 
I tried with and without the result set hard coded the errors are the same –  user3161804 yesterday
 
[Sat Jan 04 22:48:21 2014] [error] [client ::1] PHP Notice: Array to string conversion in /Users/Admin/Sites/dev/parse.php on line 65, referer: localhost/~Admin/dev/index.htm [Sat Jan 04 22:48:21 2014] [error] [client ::1] PHP Warning: fopen(clients/downloads/csv/myfile.csv): failed to open stream: No such file or directory in /Users/Admin/Sites/dev/parse.php on line 66, referer: localhost/~Admin/dev/index.htm [Sat Jan 04 22:48:21 2014] [error] [client ::1] PHP Warning: fputcsv() expects parameter 1 to be resource, boolean given in /Users/Admin/Sites/dev/parse.php on line 68 –  user3161804 yesterday
 
I just tried it it works on my PC, the last error you posted show that the problem is when you cast it to a string (string) $_GET['submittedResults'];, I would hardcoded to make sure it work, then troubleshoot your way out –  meda yesterday
show 10 more comments

From your comment, you show the ajax call being:

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

There needs to be an equal sign between "submittedResults" and the JSON string. You could put one in, but it would be better to let jQuery do it by using:

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

Note: I would think with what you have, the following test would always be false:

if (isset($_GET['submittedResults'])) {
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.