Android Question job.PostFile $_FILES['file'] Empty

Daniele Zanoni

Member
Licensed User
Longtime User
I'm using okHttp and OkHttpUtils2
In my application the user take a Photo, then he have to upload it,
but using j.PostFile or using j.PostMultipart the $_FILES is ALWAYS EMPTY.

UPLOAD:
Link = "http://mysite.com/upload.php"
myphoto = "myphoto.jpg"
'
Dim Files as List
Dim mp As MultipartFileData
Files.Inizialize
mp.Initialize
mp.Dir = File.DirDefaultExternal
mp.FileName = myphoto
mp.KeyName = "file"
mp.ContentType = "image/jpg" 
Files.Add(mp) 
'
Dim j As HttpJob
ProgressDialogShow("WAITING")
j.Initialize("JOBUPLOAD", Me)
'j.PostMultipart(Link, Null, Files)
j.PostFile(Link, File.DirDefaultExternal, myphoto)
'
'The Job Result is Success with php result ECHO.

I Read all forum but I not found a solution, $_FILES is always Empty.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
You should use PostMultiPart
 
Upvote 1

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: any code with File.DirDefaultExternal is broken code.
 
Upvote 0

Daniele Zanoni

Member
Licensed User
Longtime User
I confirm that I request permission and I can READ and WRITE Correctly from External Disk Space.
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")
AddPermission("android.permission.READ_EXTERNAL_STORAGE")
Before to Call upload function I check if file exist, and there is.

About PHP is simple:
PHP:
//....

    foreach($_FILES as $fname => $fvalue) {
        echo date("d.m.Y H:i:s", time()).": ".$fname."=".$fvalue."\r\n";
        foreach($fvalue as $name => $value) {
            echo date("d.m.Y H:i:s", time()).": ".$name."=".$value."\r\n";
        }
    }
        
    // CHECK IF UPLOAD COMPLETE   
    if (!isset($_FILES['file']) || !is_uploaded_file($_FILES['file']['tmp_name']))
    {
        echo "<script>alert('ERROR: FILE NOT ATTACHED');</script>";       
        exit;   
    }

The PHP script works correctly with a HTML POST Page, but with B4A the $_FILE is Empty.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Use https://ptsv2.com/ to see what your B4A app actually uploads via the POST (be it PostMultiPart or PostFile).
 
Upvote 0

Daniele Zanoni

Member
Licensed User
Longtime User
Thank you much !
using https://ptsv2.com/ i generate a new TOLIET, insert the link to my B4A application and I received the file with my photo.
Using j.PostMultipart works, using j.PostFile not works.

Therefore the problem is server side, but SOMEBODY CAN help me to understand WHERE IS the problem ?
Inside Server PHP I already have:
file_uploads = On;
post_max_size = 8M;
upload_max_filesize = 8M;

Daniele
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
foreach($_FILES as $fname => $fvalue) { echo date("d.m.Y H:i:s", time()).": ".$fname."=".$fvalue."\r\n"; foreach($fvalue as $name => $value) { echo date("d.m.Y H:i:s", time()).": ".$name."=".$value."\r\n"; } }
What is the output of these lines when using PostMultiPart from B4A?
 
Upvote 0

Daniele Zanoni

Member
Licensed User
Longtime User
the foreach exit because $_FILES count = 0

it seems that when the server receives the request it immediately rejects it and deletes the request parameters, in fact the connection takes place very quickly instead using the suggested site the file is transferred on 4-5sec.
it's surely a server problem but I do not understand what.

Daniele
 
Upvote 0
Top