Advertise here




Advertise here

Howdy, Stranger!

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

Uploading an image via HTTP POST request

cjpearlcjpearl Posts: 111Registered Users
edited April 2010 in iOS SDK Development
I have found several threads on this topic, but haven't been able to find where I'm going wrong in my code.

I'm trying to implement posting images to a website using this method, in post #11: http://www.iphonedevsdk.com/forum/iphone-sdk-development/3074-posting-file-web-site.html . I have little to no experience with PHP, and am thinking that my error is probably on that side of things. I've tried using the Obj-C code as posted (changing the URL and filename as necessary), or commenting out the lines as mentioned in post #16, with no success.

I have the following file in my root directory for PHP scripts, saved as upload.php:
<?php
$uploaddir = '/myappname/uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://www.mycompanywebsite.com/myappname/uploads/{$file}";
}

?>

...with no success upon sending POST requests with my app. I have tried commenting out lines in the app as suggested by post #16, and the response that gets logged to the console is either blank, or an error message that seems to mean that the file couldn't be copied from the temporary directory to /myappname/uploads (I'm away from my Mac at the moment and don't have the exact message :( ). Nothing gets uploaded to the server.

I am attempting to upload to a webhost for which I pay a monthly fee. I've tried changing the upload directory's file permissions to RWX (making it as open as possible).

Can anyone see where I may be going wrong?

Thanks!
Post edited by cjpearl on
· ·

Replies

  • lbendlinlbendlin Posts: 429Registered Users
    edited February 2009
    yes - the php file needs to sit on the server, not on your device.
    · ·
  • cjpearlcjpearl Posts: 111Registered Users
    edited February 2009
    Heh, I'm not quite that clueless :) It's on the server, in the root directory that my webhost specifies for PHP scripts. So its address is http://www.mycompanyname.com/upload.php.
    · ·
  • RickMaddyRickMaddy Posts: 2,122New Users
    edited February 2009
    One thing is most likely wrong in that PHP script is the upload path. You are giving an absolute path. I highly doubt that it's correct. Change 'uploaddir' to be just 'myappname/uploads/' (no leading slash).

    And make sure the directories already exist. 'myappname' should be in the same directory as this php file and of course 'uploads' inside that directory.
    · ·
  • lbendlinlbendlin Posts: 429Registered Users
    edited February 2009
    cjpearl wrote: »
    Heh, I'm not quite that clueless :)

    I know. I just couldn't resist.

    Build a simple webform on your server where you can test the POST - if it works there then it will also work from code. Temporarily (!!!) enable verbose php error messages.
    · ·
  • cjpearlcjpearl Posts: 111Registered Users
    edited February 2009
    OK. I have the following two files on the server.

    upload.php:
    <?php
    $uploaddir = 'myappname/uploads/';
    $file = basename($_FILES['userfile']['name']);
    $uploadfile = $uploaddir . $file;
    
    if (!is_uploaded_file($_FILES['userfile']['tmp_name'])) {
            exit("File was not uploaded to temporary directory!");
    }
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
            echo "http://www.mycompanyname.com/myappname/uploads/{$file}";
    }
    
    ?>
    

    and uptest.html:
    <html>
    <head>
    <title>File Upload Form</title>
    </head>
    <body>&#8232;This form allows you to upload a file to the server.<br>
    <form action="upload.php" method="post" enctype="multipart/form-data"><br>&#8232;
    Type (or select) Filename: 
    <input type="file" name="userfile">
    <input type="submit" value="Upload File">
    </form>
    </body>
    </html>
    

    I have both files in the base directory, and the 'myappname/uploads' directory in the base directory with them. I consistently get the 'File was not uploaded' error, so it looks like the file stream isn't making it to the temporary directory.

    I'll try enabling verbose php error messages as well (when I figure out how...being a PHP noob :( ), but that's where I'm at for the moment.
    · ·
  • cjpearlcjpearl Posts: 111Registered Users
    edited February 2009
    Got it working. I tested it so many times with so many minute changes that I'm actually not sure what caused it to start working, but it's fine now :p

    For anyone who's curious, here's my PHP file:
    <?php
    $uploaddir = '';      //Uploading to same directory as PHP file
    $file = basename($_FILES['userfile']['name']);
    $uploadfile = $uploaddir . $file;
    
    if (is_uploaded_file($_FILES['userfile']['tmp_name']))
    {
    echo "Temp file uploaded. \r\n";
    } else {
    echo "Temp file not uploaded. \r\n";
    }
    
    if ($_FILES['userfile']['size']> 300000)     //Limiting image at 300K
    {
    exit("Your file is too large."); 
    }
    
    if ((!($_FILES['userfile']['type'] == "image/jpeg")) &&  //Also allowing (!($_FILES['userfile']['type'] == "text/html")))           //HTML files
    {
    exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you uploaded."); 
    }
    
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
            $postsize = ini_get('post_max_size');   //Not necessary, I was using these
            $canupload = ini_get('file_uploads');    //server variables to see what was 
            $tempdir = ini_get('upload_tmp_dir');   //going wrong.
            $maxsize = ini_get('upload_max_filesize');
            echo "http://www.mycompanyname.com/myappname/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    }
    
    ?>
    

    My Obj-C code is literally exactly the same as post #11 here, only changing the filename and URL.

    Thanks for the help guys! :)
    · ·
  • lbendlinlbendlin Posts: 429Registered Users
    edited February 2009
    Now - immediately turn the php verbose logging off, and work on hardening the solution. Otherwise you risk creating a cheap data dump for them script kiddies.
    · ·
  • cjpearlcjpearl Posts: 111Registered Users
    edited February 2009
    Any suggestions on that? All I've done so far are those file size and type limitations in the script itself, and set the permissions on the upload folder as restrictive as possible while still being able to upload from the phone and view the files themselves.
    · ·
  • lbendlinlbendlin Posts: 429Registered Users
    edited February 2009
    Use a password (and hash it) as part of the post so that only registered iPhones can upload, and crooks can't easily see it with a packet sniffer.
    · ·
  • supermandtsupermandt Posts: 5New Users
    edited February 2010
    lbendlin wrote: »
    Use a password (and hash it) as part of the post so that only registered iPhones can upload, and crooks can't easily see it with a packet sniffer.

    Thanks a million guys! now I can do the uploading all in .NET !! no more php for uploading! works great! thanks again!

    DashyApps.com
    · ·
  • ZunePodZunePod Posts: 1,031Registered Users
    edited April 2010
    How would I change this code for posting actual files?

    And also the Obj-C code.
    <div align="center">Will code for food</div>
    · ·
Sign In or Register to comment.