Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
File upload from mobile
  • Is there anything special/different about uploading files from a mobile device vs. from the browser?
    I'm making an application that  requires users to upload a file through a mobile app... The mobile part will be done by another developer. I must simple prepare the functions so that they can be used by either the mobile application or web-browser.
    Namely, do mobile apps (android) user the same $_POST, $_FILES, etc variables when interacting with the server?

    thanks

  • HarroHarro
    Accepted Answer
    $_POST and $_FILES are server side variables, constructed by the PHP SAPI. Totally irrelevant with regards to the type of HTTP client used.

    PHP expects multipart/form data on a POST, so the app has be be compliant with that, but if the app uses a standard HTTP library to post the data (or the files), that should not be a problem.

    And it's easy to test. Create a test controller:

    <?php
    class Test extends Controller
    {
        public function action_index()
        {
            \Log::info(json_encode($_GET));
            \Log::info(json_encode($_POST));
            \Log::info(json_encode($_FILES));
        }
    }

    and have the android app post some data and upload a file to /test/index.
  • atabakatabak
    Accepted Answer
    i use cordova:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
        <title>File Transfer Example</title>
        <script type="text/javascript" charset="utf-8">
     
            document.addEventListener("deviceready", onDeviceReady, false);
     
            // device is ready
            function onDeviceReady() {
             // Do cool things here...
            }
     
            function getImage() {
                // Retrieve image file location from specified source
                navigator.camera.getPicture(uploadPhoto, function(message) {
                alert('get picture failed');
            },{
                quality: 50,
                destinationType: navigator.camera.DestinationType.FILE_URI,
                sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
            }
                );
     
            }
     
            function uploadPhoto(imageURI) {
                var options = new FileUploadOptions();
                options.fileKey="file";
                options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
                options.mimeType="image/jpeg";
     
                var params = new Object();
                params.value1 = "test";
                params.value2 = "param";
     
                options.params = params;
                options.chunkedMode = false;
     
                var ft = new FileTransfer();
                ft.upload(imageURI, "http://domain.com/upload.php", win, fail, options);
            }
     
            function win(r) {
                console.log("Code = " + r.responseCode);
                console.log("Response = " + r.response);
                console.log("Sent = " + r.bytesSent);
                alert(r.response);
            }
     
            function fail(error) {
                alert("An error has occurred: Code = " = error.code);
            }
     
            </script>
    </head>
    <body>
        <button onclick="getImage();">Upload a Photo</button>
    </body>
    </html>

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion