Android Question upload file With php

Spinter

Active Member
Licensed User
Longtime User
I'm sorry my english with google translate
I need help. I would like to upload a file or an image with the php page on the webserver ?

I found and tried many posts but nothing that would lead me to solve. Can you help me?
 

Douglas Farias

Expert
Licensed User
Longtime User
ty @DonManfred
to upload a file to this folder how can i make?
i m tring this

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

but it make a my jpg file = 10011010.jpg o_O
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Learn php! You did not learn from copy and paste!
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
Hi DonManfred i have added your code in my project and show me this error
how can i fix?

Error description: MultipartPost is declared twice. You should either remove the library reference or the code module.

i dont make any change in your code only put this on my project
 
Upvote 0

RjCl

Member
Licensed User
Longtime User
Hi DonManfred i have added your code in my project and show me this error
how can i fix?

Error description: MultipartPost is declared twice. You should either remove the library reference or the code module.

i dont make any change in your code only put this on my project

Using DonManfreds .bas code, i am using just this for uploading (its just one file and no keynames etc)

B4X:
hc.Initialize("hc")
        Dim Response As HttpResponse
        Dim fromserver As String

    'Add files
    Dim files As List
    files.Initialize
    Dim fd As FileData
    fd.Initialize
    fd.Dir = File.DirRootExternal
    fd.FileName = file_to_upload
    fd.KeyName = "files"
    fd.ContentType = "application/octet-stream"
    files.Add(fd)
    'Add second file
    'Dim fd As FileData
    'fd.Initialize
    'fd.Dir = File.DirAssets
    'fd.FileName = "1.png"
    'fd.KeyName = "upfile"
    'fd.ContentType = "application/octet-stream"
    'files.Add(fd)
    'Add name / values pairs (parameters)
    'Dim NV As Map
    'NV.Initialize
    'NV.Put("note1", "abc")
    'NV.Put("note2", "def")
    Dim req As HttpRequest
    req = MultipartPost.CreatePostRequest("http://www.xxxxx.com/upload.php", files)
    hc.Execute(req, 1)

and this is some php :-

B4X:
if (!empty($_FILES["files"])) {
 
    $uploaded_file_name = $_FILES["files"]["name"];
    $uploaded_file = $_FILES["files"]["tmp_name"];

    // test if you got the file
    echo "the file uploaded was " . $upload_file_name;
    }

$_FILES["files"]["tmp_name"]; is the actual file uploaded and $_FILES["files"]["name"]; will give you the name of the actual file name that the user had on device. After that you can change name / use file as you want.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I was trying to implement the project that @DonManfred posted in post #6 for a few days, but was unable to make it work. Although I read several threads pertaining to HttpUtils2 upload capabilities, I could not reach my goal which is: To upload one small text file maximum 100 Kb in size to my web server. Is there an easier way to upload one file using Httputils2. I could not figure out what name / values pairs (parameters) role is in his php script. I also could not find anywhere in his script a request for credentials to be able to upload files. I can upload the file using FTP and Dropbox, but not using HttpUtils2. Erel claims HttpUtils2 is simple. I have not found that to be the case. Can someone direct me to a thread that can make a believer out of me regarding file upload using HttpUtils2.
Thank you
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Which file upload methods does your server support?
I am not sure I understand your question. However, the web site is hosted by GoDaddy. It is Windows (not Linux) and supports IIS7. It uses php 5.2.17. I can use this php script by browsing a file on my PC and upload it using a submit form, but have not figured out how to use B4A to intereact with it:
B4X:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is not a text file - <br>" . $check["mime"] . ".";
        $uploadOk = 0;
    } else {
        echo "File is  a text file.<br>";
        $uploadOk = 1;
    }
}

/*
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.<br>";
    $uploadOk = 0;
}
*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($FileType != "txt" && $FileType != "tab" && $FileType != "csv"
&& $FileType != "asc" ) {
    echo "Sorry, only txt, tab, csv and asc files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
you are checking for the file fileToUpload but you set
B4X:
fd.KeyName = "files"
in your p4a code.
change your b4a to
B4X:
fd.KeyName = "fileToUpload"
to match your php-script
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
you are checking for the file fileToUpload but you set
I have not tried to use that script with B4A yet because I am not sure how to do it. It is not the problem. But my problem is: I tried your project in Post #6, but could not get it to work. I did not understand what name values, note1, note2, etc meant. Don't you also use a user and a password to access the web site?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I did not understand what name values, note1, note2, etc meant.

The complete request is, for php, likely a submit of a webform.
In this case with the textfields note1, note2 and a fileupload object with the name fileToUpload as identifier.
No, i dont use user and password. BUT if the need to be part of the postrequest for your website then probably note1, note2 should be changed or you add the right values into the request

For example: if your site expect the username in the field uname and the password in upass
then you can try
B4X:
 Dim NV As Map
    NV.Initialize
    NV.Put("uname", "mysuperduperusername")
    NV.Put("upass", "secret")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What does it take to get Donmanfred's script and B4A project in post #6 to work using HttpUtils2 ? . I have been trying for 3 days unsuccessfully. I experimented with all kind changes to it, to no avail. Does anyone have a simple project that they can share that comprises a script and the B4A code that allows me to simply upload one txt file from device to web server using HttpUtils2 and a small php script.
 
Upvote 0

yes

Member
Licensed User
Please update to B4A-Bridge v2.30+
Logger connected to: samsung SM-J710GN-358447073659196
--------- beginning of main
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
Error occurred on line: 36 (MultipartPost)
java.io.FileNotFoundException: /data/user/0/b4a.example.multipartpost/files/virtual_assets/snap1.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:199)
at b4a.example.multipartpost.multipartpost._createpostrequest(multipartpost.java:127)
at b4a.example.multipartpost.main._activity_create(main.java:455)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.example.multipartpost.main.afterFirstLayout(main.java:102)
at b4a.example.multipartpost.main.access$000(main.java:17)
at b4a.example.multipartpost.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 20 more
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **


Have a look at this example it uses erels multipartpost.bas. It uploads two files to a phpscript. On phpside the uploads are written to uploads-folder

Edit 29. August 2015:
I´ve added a new version of the example. This example uses the new okHTTP-Library.
Please note that i need to change the module multipartpost.bas to be compatible with the new okHTTP

I not change anything to the code but i got the errors. can any1 help me? i stucked with this for days and need to get the code running urgently.
 
Upvote 0
Top