Jump to content
FreakingOUT

How to read csv data in PHP from a direct Java App upload

Recommended Posts

After searching postings on multiple forums, I am officially now "Freaking OUT" trying to understand something that is probably very simple, but cannot seem to grasp.

I simply want to read basic .csv data that is sent/uploaded directly to a PHP page, and to then append each record that shows up to a single .csv file on  the server.  

The incoming data is supposed incoming via $_POST['csv'] ... at least that's what I was told.

Each.csv  record line being sent/uploaded is very simple (either single or multiple records in one small file):

text1,text2,text3,text4,text5

For additional processing I know about 'explode', etc., but right now I am stuck even trying to do an 'echo' to display the simple incoming data "as is".

One option I tried was:

$postdata = file_get_contents("php://input");

echo $postdata;

In the Java App monitoring I get the following after 3 records are sent/uploaded to the PHP URL:

Server response status line: HTTP/1.1 200 OK

I find this strange since I did not include...  http_response_code(200); ... in the page code.

Obviously, I do NOT know what I am doing here {SIGH}.

Any assistance or guidance is appreciated.

Thank you !

- FreakingOUT

 

 

 

 

 

Share this post


Link to post
Share on other sites

200 is the default. PHP will send it unless you say otherwise. It's a rather fundamental concept that.

var_dump($_POST) or readfile("php://input") should show you the data. Which is it?

Share this post


Link to post
Share on other sites

Thanks for your reply!

I put both var_dump($_POST) and readfile("php://input" on the page, but only this displayed before and after another upload:

array(0) { }

This is the info about what apparently the Java App uses

POST-request to upload data to WEB-server

(Example of upload schema)

POST /some_path HTTP/1.1
Content-type: text/csv; charset=ISO-8859-1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8080
Content-Length: 311

text1,text2,text3,text4 (etc) -can be multiple records per upload

- FreakingOUT

 

Share this post


Link to post
Share on other sites

If that's what it does then you do need php://input.

You can fopen() it like any file and do a fgetcsv() loop to read lines from it.

Share this post


Link to post
Share on other sites

Thanks, requinix.

I've found several examples, but still am baffled about how to use since I do not believe an  actual 'filename.csv' file is uploaded - only a .csv record line (or multiple records).  Nothing in the Java App upload actions I have monitored via a Command Prompt windows indicate a filename.

So I'm still "Stuck up in Lodi again" (old Creedence Clearwater Revival lyrics) on how to integrate the requisite code to simply append each incoming csv format record line (as is- with commas) to a local server 'somename.csv' file.'

fopen() example:

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);

fgetscv() example:

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

- FreakingOUT

 

Share this post


Link to post
Share on other sites

According to

Quote

POST /some_path HTTP/1.1
Content-type: text/csv; charset=ISO-8859-1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8080
Content-Length: 311

text1,text2,text3,text4 (etc) -can be multiple records per upload

PHP will be receiving the raw CSV data. No filenames.

Use whatever example you want on how to read CSV files, and where you need to put the filename use "php://input".

Share this post


Link to post
Share on other sites

I must still be missing something.

Tried the following but  still "No Joy"

NOTE: Added the first 2 lines and there is definitely a connection as the (Remote) IP address displayed OK:

<?php

$ipaddress = $_SERVER["REMOTE_ADDR"];
echo "$ipaddress";

$row = 1;
if (($handle = fopen("php://input", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
};

?>

So I remain "Baffled in Bytesville".

- FreakingOUT

 

Share this post


Link to post
Share on other sites

This "Java app" is showing you the response from its upload?

Go back a second. What does print_r($_SERVER) output?

 

Share this post


Link to post
Share on other sites

print_r($_SERVER) ..........  output is lots of Server, Gateway, Port, etc. Info.

I'm still trying some other code snippet, but this is very frustrating ;-(

Share this post


Link to post
Share on other sites

Hours of trying various things but nil.

I came back to this possibility before calling it quits after hours and hours ;-(

<?php
$myfile = fopen("php://input", "r") or die("Unable to open file!");
echo fread($myfile,filesize("php://input"));
fclose($myfile);
?>

Yielded the following with still no uploaded data being displayed:

Warning: filesize() [function.filesize]: stat failed for php://input in /home/............./test.php on line 3

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/............./test.php on line 3 on line 3

-FreakingOUT

Edited by FreakingOUT

Share this post


Link to post
Share on other sites
Finally .. this at least works as a starting point and verified the data is received:

$entityBody = file_get_contents('php://input');
 
- FreakingOUT

Share this post


Link to post
Share on other sites
On 1/11/2019 at 11:04 PM, FreakingOUT said:

print_r($_SERVER) ..........  output is lots of Server, Gateway, Port, etc. Info.

I'm still trying some other code snippet, but this is very frustrating ;-(

I was rather hoping for you post the output instead of just describe it... I mean, I know $_SERVER has lots of stuff, what I don't know is precisely what that stuff is. But since file_get_contents is indicating that stuff is working then it's no longer necessary.

On 1/12/2019 at 12:14 AM, FreakingOUT said:

Yielded the following with still no uploaded data being displayed:

Warning: filesize() [function.filesize]: stat failed for php://input in /home/............./test.php on line 3

filesize works with files. php://input is not a file. If anything there would be a value in $_SERVER that could get you the length, however file_get_contents is better.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.