Android Question POSTFILE uploads but still JOB.SUCCESS=FALSE

mokrokuce

Member
Licensed User
Longtime User
I'm trying to upload a file via HTTPUTILS.POSTFILE and when I check on the server, the files are uploaded, but I still get Job.Success=False in JobDone sub.

This is the code that's sending the file:
B4X:
Dim myHttpJob As HttpJob
url = Main.ServerName & "/upload.php?file=" & ImeZipFajla
myHttpJob.Initialize("hc", Null)
myHttpJob.PostFile(url,File.DirDefaultExternal & "/send" ,ImeZipFajla)

This is the PHP page code:
PHP:
<?php
$filename="";
$user="";

        $str = file_get_contents('php://input', 'r');
        $saved_name="upload/$user/$filename";
        $fp=fopen($saved_name,"w");
        if(!fwrite($fp,$str))
            {echo "Cannot write to file :$saved_name";}
        else
            {$zip = new ZipArchive;
            $res=$zip->open("$saved_name");
            if ($res === TRUE)
                {$zip->extractTo('upload/'.$user.'/');
                $zip->close();}
                else
                {                echo 'Error: unzip!';}
            }
        fclose($fp);
                #unlink('upload/'.$filename);
?>

I have one JobDone sub, and it goes like this:
B4X:
Sub JobDone (Job As HttpJob)
Dim req As HttpRequest

ProgressDialogHide
Log("Job:" & Job.JobName & " - " & Job.Success)

If Job.Success=True AND Posao="Upload" Then
    hc.Initialize("hc")
    req.InitializeGet(Main.ServerName & "/upis.php?file=" & ImeFajla)
    hc.Execute(req,999)   
End If

End sub

So, I need to have Job.Success=True in order to continue with the code.

I've had the same code and the same PHP page on another server, and all went fine. Could it be that there are some settings on PHP server that I have to enable in order to get a valid response after file upload?
 

DonManfred

Expert
Licensed User
Longtime User
Did you not get an error???
 
Upvote 0

mokrokuce

Member
Licensed User
Longtime User
Please upload your project (export as zip)...

Are you sure

B4X:
If Job.Success=True AND Posao="Upload" Then

that Posao is "Upload"?
Yes, Posao="Upload"

B4X:
Log("Job:" & Job.JobName & " - " & Job.Success)
This line in the log shows:
Job:hc - false
 
Upvote 0

mokrokuce

Member
Licensed User
Longtime User
If you need an working example of how to upload a file to a php-script you can have a look at the link in my signature. Youll find Example B4A and the php it is using.

Thanks, but your example is for multipartpost, and I would like to avoid changing code.
I am primarily concerned about the fact that it is working properly on one server, and not on another (same PHP code, same PHP version).
BTW, why does it say "connection reset by peer"? Is that a connectivity problem?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It´s more this than an connectivity problem:

"connection reset by peer" means that a TCP RST was received and the connection is now closed. This occurs when a packet is sent from your end of the connection but the other end does not recognize the connection; it will send back a packet with the RST bit set in order to forcibly close the connection. This can happen if the other side crashes and then comes back up, or if it calls close() on the socket while there is data from you in transit, and is an indication to you that some of the data that you previously sent may not have been received. It is up to you whether that is an error; if the information you were sending was only for the benefit of the remote client then it may not matter that any final data may have been lost. However you should close the socket and free up any other resources associated with the connection.

So i would like to say it´s more the php-configuration (or the webservers filewall) if the phpscript working well on an other webside
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You can have a look at the phpinfo-page to get more informations about the php-configuration on that server...

Create a php with this content and then call it

PHP:
<php
phpinfo();
?>

Then post the result here (or, if you dont want to post it in forum, you can post it in a PM to me here in forum if you want)

For example:

The php-command file_get_contents is restricted in a lot of php-configurations.
 
Upvote 0

mokrokuce

Member
Licensed User
Longtime User
Ok, so I've messed about with permissions on the server and here's what happened:
The device via the PHP page uploads the file and unzippes it to a folder. If this folder doesn't exist, it is being created by the PHP script. After that another PHP page should be called by the device to process the file.
I have deleted the entire folder in which the files are created, and on the first occurence (when the PHP makes the folder and device uploads the file and unzippes it) everything runs smooth. The second time (when the folder is in existence) I get the Job.Success=false error.
I have made the folder with 777 permissions, but I don't know what else to do.
The server is a Win8 Server, and I have Wamp and PHP 5.2.6 on it.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe you want to show me your php-script? As PM if you want
I´m coding PHP since years... Maybe i have an idea

BTW: One moment... Saying Win8 SERVER and in the same sentence you telling about an Wamp. They both dont fit together :D
On an Windows SERVER you normally have an IIS running.
You are using a normal Win8 AS a server, right?
 
Upvote 0

mokrokuce

Member
Licensed User
Longtime User
Yes, Windows Server 2012, I turned IIS off.

Here's full PHP code:
PHP:
<?php
$filename="";
$user="";

if (isset($_GET['file']) and $_GET['file']!="")
    {
    $filename=$_GET['file'];
    $pos=strpos($filename,"-");
    $user=substr($filename,0,$pos);
    mkdir("upload/$user",0777);
    }
else
{
    print_r("Error: No filename provided");
    exit;
}
   $str = file_get_contents('php://input', 'r');
   
         $saved_name="upload/$user/$filename";
        $fp=fopen($saved_name,"w");
        if(!fwrite($fp,$str))
            {
            echo "Cannot write to file :$saved_name";
            }
        else
            {
            #echo "Recieved : $saved_name.";
            #odzipuj u folder za komercijalistu
            $zip = new ZipArchive;
            $res=$zip->open("$saved_name");
            if ($res === TRUE) 
                {
                $zip->extractTo('upload/'.$user.'/');
                $zip->close();
                echo "Received request";
                } else 
                {
                echo 'Error: unzip!';
                }
           
            }
        fclose($fp);
                #unlink('upload/'.$filename);
?>

I'm pretty sure it's something to do with permissions on folders, but don't know what.
 
Upvote 0

mokrokuce

Member
Licensed User
Longtime User
Solved
at the server side.
I was trying to create a folder, and PHP was supposed to continue if the folder existed no matter what, and it worked on an XP machine as a server. So this line in PHP:
PHP:
mkdir("upload/$user",0755);
was making the folder, and everything went fine the first time it was created, but when the page tried making the folder that existed something went wrong. I'm not such an expert to tell exactly what, but i added this check before creating the folder:
PHP:
 if (file_exists("upload/$user")===FALSE)
 {
 mkdir("upload/$user",0755);
 }
...and everything worked.

I guess sometimes you just need to sleep on it.

Thanks anyway for the help.
 
Upvote 0
Top