Support » Developing with WordPress » How to write on csv in ajax php

  • Hello, I am trying to write on CSV file in PHP
    I have an array of values that I’m looping through to write on a CSV file
    when I write the code on a separate file and open the file in the browser then I can see my values in the CSV file but when I write the code in a function which is in functions.php file and I am calling the function in jquery ajax call.
    but when I use this by ajax call it do not write on csv file
    Why is this happening?
    This is the code I’m using

    $csvfile = 'activity.csv';
    $list = array
    (
        "write, values,in, CSV,file"
    );
    $file = fopen($csvfile,"w");
    foreach ($list as $line)
    {
     fputcsv($file,explode(',',$line));
    }
    fclose($file);

    I am using the same code in a separate file and n function which I’m calling in ajax
    but the code is working in a separate file and not working in ajax calls.
    BTW I’m getting a response in ajax call like
    ‘Resource Id#1322’

    Can anyone help?
    Thanks

Viewing 1 replies (of 1 total)
  • I guess you should specify an absolute path to the CSV file. I am unsure where the file would be saved if you just run it as you wrote.

    A recommendation: save it in the uploads directory. Example to define the path:

    $upload_dir = wp_upload_dir();
    $csvfile = $upload_dir['basedir'].'activity.csv';

    If this doesn’t solve the problem, try to include the output of each variable in your PHP code via var_dump(). You can also see them in the AJAX response in your browser console.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.