Jump to content

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


FreakingOUT

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

 

 

 

 

 

Link to comment
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

 

Link to comment
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

 

Link to comment
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".

Link to comment
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

 

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...

Sorry, had problems logging back in here and other stuff hitting the fan.

It turns out part of my problem was apparently fgetcsv will not work on my ISP's "Dark Ages" version of PHP and they will not upgrade ;-(

Someone else came up with a workaround, but the initial php://input suggestion was indeed an accurate first step in the food chain, so thanks again !!!

-FreakingOUT

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.