Copy file to webserver

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code writes the file to the POST stream:
B4X:
Sub Process_Globals
    Dim HttpClient1 As HttpClient
    Dim InputStream1 As InputStream
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        HttpClient1.Initialize("HttpClient1")
    End If
    Dim req As HttpRequest
    Dim size As Long
    size = File.Size(File.DirRootExternal, "somefile")
    InputStream1 = File.OpenInput(File.DirRootExternal, "somefile")
    req.InitializePost("http://....", InputStream1, size)
    HttpClient1.Execute(req, 1)
End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    InputStream1.Close
End Sub
I'm less familiar with the PHP code that reads the stream. I think that it should be pretty simple as described here: Reading raw POST data in PHP
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
PHP code

I tested below PHP code and it works (not all servers allow file_get_contents)

<?php

$postdata = file_get_contents("php://input");
//$filename=$_POST['file'];
$file = fopen("test.png","wb");
fwrite($file, $postdata);
fclose($file);

?>

A challenge is now to also pass the filename in a variable. Cannot get that to work.
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
As per your example code,i try to copy file from device simulator to webserver,but it is not copy file.pls advise where it is problem,my code is given below

Basic4Android Code:-

Dim req As HttpRequest
Dim size As Long
size = File.Size(File.DirRootExternal & "external_sd/Data/", "ExpNote.S3db")
InputStream1 = File.OpenInput(File.DirRootExternal & "/external_sd/Data/", "ExpNote.S3db")
req.InitializePost("http://www.kharind.com/experiment/File.php", InputStream1, size)
HttpClient1.Execute(req, 1)


PHP Code:- (File.php)

<?php

$postdata = file_get_contents("php://input");
//$filename=$_POST['file'];
$file = fopen("ExpNote.S3db","wb");
fwrite($file, $postdata);
fclose($file);

?>

pls give me a best example code,so that i can copy file to webserver by progrmatically.
 
Last edited:
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
When i run the script from simulator it doesn't response anything,same script when i run from browser it shows an following error

Warning: fopen(ExpNote.S3db) [function.fopen]: failed to open stream: Permission denied in /home/khardin/public_html/experiment/file.php on line 5

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/khardin/public_html/experiment/file.php on line 6

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/khardin/public_html/experiment/file.php on line 7


Please rectify my script because of i am new for PHP and Basic4Android
 
Upvote 0
Top