iOS Question Sample php for httpjob postfile ?

tufanv

Expert
Licensed User
Longtime User
Hello,

I want to try httpjob postfile method to post a file to my server using php. On b4i side I use :

B4X:
    Dim ds As HttpJob
    ds.Initialize("ds",Me)
    ds.PostFile("http://server/FileUpload2.php",File.DirDocuments,"tufan.jpg")

On php side I tried a sample php code from a google search

B4X:
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

I always get possible file upload attack from the php as a result. Is there any working php side example for a simple postfile method with a few lines of codes like this ? or can anyone who understand from php tell me what is wrong with this ?
TY
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

if you want to use the PostFile method you need to use php input (file_get_contents('php://input')). If you want to use this PHP code snippet change your B4i code:
B4X:
    Dim H As HttpJob
    H.Initialize("",Me)
  
   Dim MFD As MultipartFileData
   MFD.Initialize
   MFD.Dir = File.DirDocuments
   MFD.FileName = "tufan.jpg"
   MFD.KeyName = "userfile"
   MFD.ContentType = "application/octet-stream"
   H.PostMultipart("http://server/FileUpload2.php",Null,Array(MFD))

Jan
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Hi,

if you want to use the PostFile method you need to use php input (file_get_contents('php://input')). If you want to use this PHP code snippet change your B4i code:
B4X:
    Dim H As HttpJob
    H.Initialize("",Me)
 
   Dim MFD As MultipartFileData
   MFD.Initialize
   MFD.Dir = File.DirDocuments
   MFD.FileName = "tufan.jpg"
   MFD.KeyName = "userfile"
   MFD.ContentType = "application/octet-stream"
   H.PostMultipart("http://server/FileUpload2.php",Null,Array(MFD))

Jan

Thanks very much for answer. My file is m4a file , I wrote it wrong above , and i am getting the error :

B4X:
Application_Start

JobName = upload2, Success = true
<pre>Possible file upload attack!
Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => tufan.m4a
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpCpehgF
            [error] => 0
            [size] => 112555
        )
)
</pre>
Class (b4i_httpjob) instance released.

maybe it is because of the type and it is for an image and we have to change it to something else for an audio file ? (m4a)

I also tried : audio/mpeg but no luck. php file is not important i can change to anything , I just need a sample php code for postmultipart or postfile , not makes any difference for me.
 
Upvote 0
Top