Android Question How Do You Test a Php Script Before You Use it With a B4A Project

Mahares

Well Known Member
Licensed User
I wrote a shaky php script to upload a text file from my device to a web server. But, before I test it with my B4A application, I like to test it by uploading a file located in my C drive to the server. Is this the proper way to test it using this URL format?

http://www.mywebsite.com/Httptest/httputils2upload.php?

file=c:/temp/httputils2uploadfile/CB1021813022613.txt

The script is located in this server path: Httptest/httputils2upload.php
The text file is located in this path on the C drive: c:/temp/httputils2uploadfile/CB1021813022613.txt
Thank you for any help
 

Mahares

Well Known Member
Licensed User
Thank you guys for your suggestions. I did manage to create an html submit form for now, but it seems the script is defective: I get the 'Error: 'No filename was available'
The form name is: httputils2upload.html and is as follows:
B4X:
<!DOCTYPE html>
<html>
<body>
<form action="httputils2upload.php" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
the httputils2uload.php script is:
B4X:
<?php
$filename="";

if (isset($_GET['file']) and $_GET['file']!="")
   {
   $filename=$_GET['file'];
   $strFile = file_get_contents($filename);     //read file into a string, image is permitted
   $saved_name = "Httpttest/uploads/$filename";  //folder on server where to save file
   if (is_writable($saved_name)) {
    if (!$fp = fopen($saved_name, 'w')) {
         echo "Cannot open file ($saved_name)";
         exit;
    }
    // Write to the just opened file.
    if (fwrite($fp, $strFile) === FALSE) {
        echo "Cannot write to file ($saved_name)";
        exit;
    }
        echo "Success, wrote to file ($saved_name)";
        fclose($fp);

    } else {
       echo "The file $saved_name is not writable";
    }
} else     {
    print_r("Error: No filename was available");
    }
?>
EDIT: When I changed 'post' to 'get' in the form, I get this error:
Warning: file_get_contents(CB1021813022613.txt) [function.file-get-contents]: failed to open stream: No such file or directory inD:\Hosting\9154849\html\Httptest\httputils2upload.php on line 7
The file Httpttest/uploads/CB1021813022613.txt is not writable

Please help with an easy response to understand, because when it comes to php I am as dumb as they come. Thank you
 
Last edited:

Mahares

Well Known Member
Licensed User
i might be missing something
I have successfully used FTP and Dropbox to upload files in the past, but I wanted to try HttpUtils2 as Erel suggests it is easy and I think it is probably more secure. Until now, I have not been able to. If you are familiar with Php and want to help me that is great. If not, the forum has some very talented Php members that hopefully can lend a hand.
 

walterf25

Expert
Licensed User
I have successfully used FTP and Dropbox to upload files in the past, but I wanted to try HttpUtils2 as Erel suggests it is easy and I think it is probably more secure. Until now, I have not been able to. If you are familiar with Php and want to help me that is great. If not, the forum has some very talented Php members that hopefully can lend a hand.
Wow, easy there big boy, i didn't mean any disrespect by my comment, with that being said, i'm not an expert with PHP, i have done several projects which include php scripts one of them being my "Pirate Bay App", and you are right there are plenty of talented members in the forum, most of them are probably too busy with their own projects but i'm sure you will figure this out.

Cheers,
Walter
 

DonManfred

Expert
Licensed User
if (isset($_GET['file']) and $_GET['file']!="")
{
$filename=$_GET[
'file'];
$strFile = file_get_contents($filename); //read file into a string, image is permitted
this will not work!
When you do a postfile-request then ALL the php get is the plain data from the fileupload. NO ONE extra data. Also not the paramater "file".
you will just get "the file itself"...

i wrote a test-app which is working with multipart-file-upload and wrote a small test-php-script

PHP:
<?php
$br = "<br />";
if (isset($_REQUEST['DeviceID'])){$DeviceID=trim($_REQUEST['DeviceID']);} else {$DeviceID="0";}

$file_handle = fopen('./b4a.fileupload.log', 'a+');
fwrite($file_handle, "======================================"."\r\n");
foreach($_REQUEST as $name => $value){
    $add = "";
    fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$name."=".$value." ".$add."\r\n");
}
fwrite($file_handle, "======================================"."\r\n");

#echo "DeviceID = ".$DeviceID.$br;
#echo "json = ".$json.$br;


$res = array();
$res["resultcode"] = 0;
$uploads[] = array();
fwrite($file_handle, "======= FILES ========================"."\r\n");
foreach($_FILES as $name => $value){
  $uploads[$name] = $value;
  foreach($value as $fname => $fvalue){
      fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$fname."=".$fvalue."\r\n");
  }
     fwrite($file_handle, date("d.m.Y H:i:s", time()).": Upload of \"".$name."\"\r\n");
  if($name=="File"){
    $uploaddir = './uploads/';
    $uploadfile = $uploaddir . basename($_FILES[$name]['name']);
       fwrite($file_handle, date("d.m.Y H:i:s", time()).": MoveUploadedFile(".$_FILES[$name]['name'].")\r\n");
    if (move_uploaded_file($_FILES[$name]['tmp_name'], $uploadfile)) {
      $uploads[$name]["status"] = $_FILES[$name]['name']." saved successfull";
      $uploads[$name]["url"] = "http://pdf.basic4android.de/uploads/".$_FILES[$name]['name'];
        fwrite($file_handle, date("d.m.Y H:i:s", time()).": ->moving ".$_FILES[$name]['name']." successfull\r\n");
      #echo "Datei ist valide und wurde erfolgreich hochgeladen.\n";
    } else {
      $uploads[$name]["status"] = $_FILES[$name]['name']." failed to save!";
         fwrite($file_handle, date("d.m.Y H:i:s", time()).":->moving ".$_FILES[$name]['name']." NOT successfull\r\n");
      #echo "Möglicherweise eine Dateiupload-Attacke!\n";
    }
  }
  foreach($value as $fname => $fvalue){
      fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$fname."=".$fvalue."\r\n");
  }
  fwrite($file_handle, "======================================"."\r\n");
    #print_r($uploads);
  $appresult["uploads"] = $uploads;
}
echo json_encode($appresult);
fclose($file_handle);
#echo '<a href="PDFs/output.pdf" target="_blank">output.pdf</a>';;
?>
======================================
24.02.2015 19:30:06: note1=abc
24.02.2015 19:30:06: note2=def
24.02.2015 19:30:06: action=upload
======================================
======= FILES ========================
24.02.2015 19:30:06: name=badgeview1.png
24.02.2015 19:30:06: type=application/octet-stream
24.02.2015 19:30:06: tmp_name=/tmp/phpVdLOUT
24.02.2015 19:30:06: error=0
24.02.2015 19:30:06: size=532397
24.02.2015 19:30:06: Upload of "File"
24.02.2015 19:30:06: MoveUploadedFile(badgeview1.png)
24.02.2015 19:30:06:->moving badgeview1.png NOT successfull
24.02.2015 19:30:06: name=badgeview1.png
24.02.2015 19:30:06: type=application/octet-stream
24.02.2015 19:30:06: tmp_name=/tmp/phpVdLOUT
24.02.2015 19:30:06: error=0
24.02.2015 19:30:06: size=532397
======================================
======================================
24.02.2015 19:31:27: note1=abc
24.02.2015 19:31:27: note2=def
24.02.2015 19:31:27: action=upload
======================================
======= FILES ========================
24.02.2015 19:31:27: name=badgeview1.png
24.02.2015 19:31:27: type=application/octet-stream
24.02.2015 19:31:27: tmp_name=/tmp/phphRjLFh
24.02.2015 19:31:27: error=0
24.02.2015 19:31:27: size=532397
24.02.2015 19:31:27: Upload of "File"
24.02.2015 19:31:27: MoveUploadedFile(badgeview1.png)
24.02.2015 19:31:27: ->moving badgeview1.png successfull
24.02.2015 19:31:27: name=badgeview1.png
24.02.2015 19:31:27: type=application/octet-stream
24.02.2015 19:31:27: tmp_name=/tmp/phphRjLFh
24.02.2015 19:31:27: error=0
24.02.2015 19:31:27: size=532397
======================================
 

Attachments

Mahares

Well Known Member
Licensed User
i wrote a test-app which is working with multipart-file-upload and wrote a small test-php-script
Thank you so much Don. The code works great when I use it as is to upload your file to your web site which does not require a user and pwrd , but if I change it to send a file to mine, I get this error: '401, Unauthorized Access due to invalid credentials'. It is obvious it is looking for the user and password as we discussed in previous occasions and which are neither in the script nor the B4A code. If you can figure a way to embed the USER and PWD in the script, which is preferable to embedding them in the B4A code, I think we have a chance. At any rate, your efforts have been worth while.
 
Top