FreakingOUT Posted January 11, 2019 Share Posted January 11, 2019 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 11, 2019 Share Posted January 11, 2019 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? Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 11, 2019 Author Share Posted January 11, 2019 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 11, 2019 Share Posted January 11, 2019 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. Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 11, 2019 Author Share Posted January 11, 2019 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 12, 2019 Share Posted January 12, 2019 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". Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 12, 2019 Author Share Posted January 12, 2019 Ahhh... very interesting. Will give it a try. THANKS !!! - FreakingOUT Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 12, 2019 Author Share Posted January 12, 2019 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 12, 2019 Share Posted January 12, 2019 This "Java app" is showing you the response from its upload? Go back a second. What does print_r($_SERVER) output? Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 12, 2019 Author Share Posted January 12, 2019 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 ;-( Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 12, 2019 Author Share Posted January 12, 2019 (edited) 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 3Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/............./test.php on line 3 on line 3 -FreakingOUT Edited January 12, 2019 by FreakingOUT Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 14, 2019 Author Share Posted January 14, 2019 Finally .. this at least works as a starting point and verified the data is received: $entityBody = file_get_contents('php://input'); - FreakingOUT Top Quote Link to comment Share on other sites More sharing options...
requinix Posted January 14, 2019 Share Posted January 14, 2019 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. Quote Link to comment Share on other sites More sharing options...
FreakingOUT Posted January 28, 2019 Author Share Posted January 28, 2019 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.