Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

upload event.media via xhr post to PHP file

I am trying to capture the image passed to the php script.

I'm not having any luck.

Does anyone have a script they are using to share?

I'm using this:

inline code

$data = $_POST['image'];

$image_name=time() . ".jpg";
$newname2="sites/default/files/scans/bw_".$image_name;

imagejpeg($data, $newname2, 100);

header('content-type: text/xml; charset=utf-8');
print '<?xml version="1.0" encoding="utf-8"?>'."n";
print '<test><data>'.$newname2.'</data></test>'."n";

What am I doing wrong?

— asked May 4th 2010 by Justin Hall
  • binary
  • images
  • iphone
  • php
  • upload
  • xhr
0 Comments

8 Answers

  • The Twitpic example works great in the KitchenSink.

    How is Twitpic capturing the image?

    — answered May 4th 2010 by Justin Hall
    permalink
    0 Comments
  • Hi, the uploaded file should be available in the $_FILES superglobal array rather than $_POST.

    $data = $_FILES['image'];
    
    — answered May 4th 2010 by Simon Rabjohns
    permalink
    0 Comments
  • I made the change, but I'm still getting the same result.

    The file does not save.

    I have a form that tests the upload image php script and from the form the submission works without an issue.

    The file will not transfer from the iPhone to the $_Files['image']['name'] in the xhr post.

    Any idea why?

    — answered May 4th 2010 by Justin Hall
    permalink
    0 Comments
  • Hey Justin. If I remember right, with PHP an uploaded file gets saved to a temp directory and then you have to move it to where its supposed to go. Send me your script and I will play with it. Its been awhile since I did a file upload with PHP..

    — answered May 4th 2010 by Clint Tredway
    permalink
    0 Comments
  • Clint,

    Here is the PHP.

    incline code

    <?php
    $file = $_FILES['image'];
    $output = "";
    //$file = "sites/default/files/scans/bw_1272948817.jpg";
    if($file){
    
        $image = $_FILES['image'];
        $image_name=time() . ".jpg";
        $newname2="sites/default/files/scans/bw_".$image_name;
    
        ob_start();
            // output jpeg (or any other chosen) format & quality
            $im = imagecreatefromjpeg($image);
            // capture output to string
            $contents = ob_get_contents();
            // end capture
            ob_end_clean();
            $fh = fopen($newname2, "a+" );
                fwrite( $fh, $contents );
            fclose( $fh ); 
    
        print '<?xml version="1.0" encoding="utf-8"?>'."n";
        print '<test><data>'.$newname2.'</data></test>'."n"; 
    
    } else {
        print '<?xml version="1.0" encoding="utf-8"?>'."n";
        print '<test><data><![CDATA[' . $output . ']]></data></test>'."n"; 
    }
    ?>
    

    The problem is this: the $_FILES array has a count of '0' when I output the results back to the iPhone.

    I'm calling this from the iPhone.

    incline code

    Titanium.Media.showCamera({
    
            success:function(event)
            {
                Ti.API.debug("picture was taken");
                var image = event.media;
    
                Ti.API.info(image.height +' x '+ image.width);
    
                var imageView = Titanium.UI.createImageView({
                    image:image,
                    width:480,
                    height:640
                });
                var tmpimage = imageView.toImage();
                //
                var imgstr = Ti.Utils.base64encode(tmpimage);
    
                scan.add(imageView);
    
                //Ti.API.info(newimage.height +' x '+ newimage.width);
                actInd.show();
    
                var xhr = Titanium.Network.createHTTPClient();
                xhr.onerror = function(e) {
                  var a = Titanium.UI.createAlertDialog({ 
                    title:'Well, this is awkward...',
                    message: 'We had a problem scanning your barcode - please try again'
                  });
                    a.show();
                    actInd.hide();
                };
                xhr.onload = function() {
                    output = 'status ' + xhr.status + 'n';
                    output += 'connected ' + xhr.connected + 'n';
                    output += 'readyState ' + xhr.readyState + 'n';
                    output += 'responseText ' + xhr.responseText + 'n';
                    output += 'responseXML ' + xhr.responseXML + 'n';
                    output += 'responseData ' + xhr.responseData + 'n';
                    output += 'connectionType ' + xhr.connectionType + 'n';
                    output += 'location ' + xhr.location + 'n';
    
                    var a = Titanium.UI.createAlertDialog({ 
                    title:'Image Scan Results',
                    message: output
                  });
                    a.show();
                    scan.remove(imageView);
                    actInd.hide();
                };
    
                xhr.onsendstream = function(e) {
                    //document.getElementById("output").innerHTML = "<p" + e.progress + "</p>";
                };
    
              /**/
              xhr.open('POST','http://www.justinhall.com/scanner/scanimage_test.php');
              xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
              xhr.send({
                image:tmpimage,type:'submit'
              });
    
              /*xhr.open('POST','https://twitpic.com/api/uploadAndPost');
      xhr.send({
        media:tmpimage,
        username:'jhalljhall',
        password:'xxxxxx',
        message:'Testing upload'
      });*/
    
                // programatically hide the camera
    
    
            }
    
    — answered May 4th 2010 by Justin Hall
    permalink
    0 Comments
  • Hi, not sure if this is the issue but normally when posting file data the content-type should be "multipart/form-data" which I think it should default to if no setRequestHeader is specified.

    — answered May 4th 2010 by Simon Rabjohns
    permalink
    0 Comments
  • I changed the content-type to multipart-form and I still can't get my php file to output the $_FILES['image'].

    I am pulling together a response text called 'output' which collects the number of items in the global arrays ($_FILES, $_POST, $_REQUEST, $HTTP_POST_VARS) and I only get one item in the $_REQUEST array.

    I'm currently trying to parse that data to see if the file and other args are somehow located in the $_REQUEST array.

    Nothing else is telling me that the $_FILES array has binary data.

    Thanks for your help!

    — answered May 4th 2010 by Justin Hall
    permalink
    0 Comments
  • I changed the content-type to multipart-form and I still can't get my php file to output the $_FILES['image'].

    I am pulling together a response text called 'output' which collects the number of items in the global arrays ($_FILES, $_POST, $_REQUEST, $HTTP_POST_VARS) and I only get one item in the $_REQUEST array.

    I'm currently trying to parse that data to see if the file and other args are somehow located in the $_REQUEST array.

    Nothing else is telling me that the $_FILES array has binary data.

    Thanks for your help!

    — answered May 4th 2010 by Justin Hall
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.