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

Discussion in 'Android Questions' started by Mahares, Feb 24, 2015.

  1. Mahares

    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
     
  2. DonManfred

    DonManfred Expert Licensed User

    you need to create a html form with an fileupload to test it. Search google for examples (you will find millions i believe)
    - The php-script can not "download" anything from your pc.
    - YOU (or your browser) must POST the file in a multipartform-payload to your php-script
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It depends on how you implemented the file upload in your PHP script.
     
  4. KMatle

    KMatle Expert Licensed User

  5. Mahares

    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:
    Code:
    <!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:
    Code:
    <?php
    $filename=
    "";

    if (isset($_GET['file']) and $_GET['file']!="")
       {
       $filename=$_GET[
    'file'];
       $strFile = file_get_contents($filename);     //read file into a stringimage 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: Feb 24, 2015
  6. warwound

    warwound Expert Licensed User

    thedesolatesoul likes this.
  7. Mahares

    Mahares Well Known Member Licensed User

    Thanks Warwound. I did try that script you have in your link before, But I could not get my project in B4A to work with it, so Erel suggested I use the file_get_contents method. That is why I created a new script here. Here is the link where Erel made the suggestion:
    See post#32 here: http://www.b4x.com/android/forum/threads/upload-file-with-php.37562/page-2#post-319323
    If you can help me correct my script here that is great.
     
  8. walterf25

    walterf25 Well-Known Member Licensed User

    I'm sorry but i might be missing something, if all you want to do is upload a text file, why not use the FTP library?

    Cheers,
    Walter
     
  9. Mahares

    Mahares Well Known Member 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.
     
  10. walterf25

    walterf25 Well-Known Member Licensed User

    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
     
  11. DonManfred

    DonManfred Expert Licensed User

    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_handledate("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_handledate("d.m.Y H:i:s"time()).": ".$fname."=".$fvalue."\r\n");
      }
         
    fwrite($file_handledate("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_handledate("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_handledate("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_handledate("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_handledate("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>';;
    ?>
     

    Attached Files:

    inakigarm, KMatle and ArminKH like this.
  12. Mahares

    Mahares Well Known Member Licensed User

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice