Jump to content

Recommended Posts

I am calling a shell script that should return a tab delimited file in a variable name $results  but when I write result to a file the only thing that is written is  ARRAY and when I use serialize() and then write it to a file it add a whole bunch of special characters.

 

exec("path_to_Shell_script",$results);

$myFile = "data.csv";
$fh = fopen($myFile, 'w');
fwrite($fh, $results);
fclose($fh);

 

Any help with getting a tab delimited csv file out of this would be great.

Link to comment
https://forums.phpfreaks.com/topic/195602-exec-help/
Share on other sites

If you write an array like that, you get the word "array" as output.  Since $results is an array of lines, you will need to use a loop to write it -- something like this:

foreach ($results as $line) {
    fwrite($fh, $line);
}

 

Depending on whether or not the line-feeds are still on the lines after input, you may have to add them to the output.  If you try the above and everything is on one line then change the write to:

fwrite($fh, $line) . PHP_EOL;

 

You might be able to write it with a single command (getting rid of the fopen() and fclose() calls) by using file_put_contents().  Take a look at the PHP documentation http://us.php.net/manual/en/function.file-put-contents.php

Link to comment
https://forums.phpfreaks.com/topic/195602-exec-help/#findComment-1027787
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.