Looking for help posting a csv file to a php script (for insertion into mySQL table) using postfile block (not posttext)

Here's the top part of the php code used to get the file. It was originally used in combination with an html page with a post file button. I thought the App Inventor postfile block would act similar to an http post in this case. I've tried various header pairs, changing path to file itself (the file is formatted correctly... it was made in the same App, using list of rows
and I can see fields in file viewer). So, I'm thinking its the connection between the postfile block and the php. P.S., I've used the posttext (as opposed to postfile) block in the past for inserting a single field into the same mySQL table, using a constructed URL with name value pairs, but this time I need to send a whole cvs file with multiple fields for insertion. Any help using postfile would be greatly appreciated!

<?php if(isset($_POST["Import"])){ $filename=$_FILES["file"]["tmp_name"]; if($_FILES["file"]["size"] > 0) { $file = fopen($filename, "r"); while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) { $query = "INSERT into... ?>

What's in global GopherTable ?

A list of lists?

Don't count on automatic csv conversion going into a PostFile block.

There are blocks to force that in the list blocks pallette.

Yes, the GopherTable is a list of lists... Here are full blocks showing construction of csv file. I first form a list of rows, then create a list of those row lists. I have can see the contents of the file on my phone and it presents as a csv... I also played around adding the GopherTable list (orange variable) directly to the path of the postfile block and I can see each comma delimited row with line breaks in the error message. The error message is always "Error 1104 : Unable to complete the given request with the file ..."


You have a timing problem.
14704ecf48f4f92bc73ebd42b96fcad987f244d2_2_565x500

See the circled section of code?

You are asking for those actions too soon.

Look in the File1 component blocks for the event that fires after completion of a SaveFile operation, and pulll one in from the File1 Blocks Pallette.
Move the circled blocks into that Event, so the file will be complete when you ask for it to be Posted.

Ok, I tried that, but still got same (1104) error...

That 1104 error opens a bag of worms, beyond this timing problem, and in areas I have avoided.

I suggest using the board search facility for '1104' to see how more adventurous users have dealt with this error.

OK, will do. Thank you for pointing out the timing error. That is a good start.

If PostText works for you, why not use that ?

or perhaps try:

Post text worked well for a single row, or a relatively small number of rows, but this part of the app requires sending possibly 100s of rows. I tried looping in the app and sending PostText for each row but the server seemed to be able to handle a set number, on average 60-100 before it would bomb out... meaning 60-100 rows would end up in the mySQL file but not all. I was going to bin the rows some how in volleys of say 25 and continue to use PostText, but I thought it would be better to send one PostFile request to the server, rather than possibly 100s of individual PostText requests. I thought the iteration would be best handled on the server side, rather than the app. But, I may end up having to do revert to the PostText method :frowning: but I'm stubbornly thinking this PostFile should work since the php file (for the PostFile method) is expecting a csv file so I'd like to get the app to send the file instead.

I was thinking that you would send the entire file "contents" in one go, to be handled server side. But I get your point :wink:

The php file I am using now for the PostFile method is very similar to the stackoverflow one so I think the problem is the PostFile block not seeing or sending the file... perhaps with the correct request headers? I've also tried various combinations of the URL string, with and without ?file= etc.

Perhaps simplify, and just try a file upload, if that works then the issue is server side with the script ?

or

(I note that neither of these options send any headers....)

I tried the upload script of Metric Rat and got the same (1104) error. I've seen the Pura Vida example before but I thought requiring listpicker was a bit of a long way around to getting a file. I haven't used the file blocks much but I'm wondering if the scope (which I have set to App) has anything to do with it... when I played around with that and set the scope to "asset", the file saved to a long path that basically duplicated the path on the phone... starting at the folder that the file is normally stored in when I set the scope to App (I did read about MIT file storage - external/internal - and what legacy means). I still think the problem lies in PostFile block finding the file. I'll try the list picker approach when I get a chance...

Yes, seems postFile requires an absolute path to the file:

These blocks work for me
image

the file is located at

/storage/emulated/0/edu.mit.appinventor.aicompanion3/files/cnvImg.jpg

for ASD

(/storage/emulated/0/<package-name>/files/file.jpg)

Adjust your blocks accordingly :slight_smile:

Ok, so far so good... I was able to find and upload the file using the full path trick. Funny, along the way I had tried placing the full path block directly to the PostFile block but didn't remove the file:// do it didn't work. Thank you!! Now I am working on (tweaking) the original php script to write the contents of the csv into the mySQL table... but that is a server side php problem now! This was a great help. Thanks again!

1 Like

Happy to report... success! PostFile sends (one) App produced csv file to server... from there php file parses rows into mySQL table. Just FYI, ended up storing csv file on server, then use fopen to parse - file on server is overwritten each time the app fires so it doesn't keep adding to same file (to save space). Many thanks again!

1 Like

Good to hear, and well done for persevering :slight_smile: