Uploading an image.

Hi,

I have more or less finished an app and it works perfectly on my shared server…  I have a VPS which I now intent to use because it should be more reliable and faster.  I have moved all my files over to the new server and everything seems to work perfectly except for the image uploads…  It appears that no content-type is getting passed across.  I get the following error (from the php error log file) at the moment - “PHP Notice:  Undefined index: myfile”, this comes from the line "echo “Named: " .$_FILES[“myfile”][“name”]. “<br>”;”  I am using the multipart form example found here.  As I said this worked perfectly on the old server.  Does anyone have any ideas why it does not work on the new server,  I have asked my server and they are looking into it,  but at the moment they think the error is on my side not theirs.

I have also tried using network.upload() and cannot get this to work on either my old or new server.  Here is the code I am using…  It also appears that no content type is being passed across. (the PHP file jumps too    - echo “Invalid file - No content type”; - which indicates that there is no content type.

Thanks Craig

[lua]

local function networkListener( event )

if ( event.isError ) then

                print( “Network error!”)

        elseif ( event.phase == “ended” ) then

                print ( “Upload complete!” )

                print ( "RESPONSE: " … event.response)

        end

end

network.upload( 

       “http://inspectionapp.myserver.co.nz/upload_files.php”, 

        “POST”, 

        networkListener,  

        “Image.jpg”, 

        system.DocumentsDirectory, 

        “image/jpg” 

[/lua]

And my php file 

[lua]

<?php

$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);

$extension = end(explode(".", $_FILES[“file”][“name”]));

echo "Named: " .$_FILES[“filename”][“name”]. “<br>”;

echo "Type: " .$_FILES[“filename”][“type”] . “<br>”;

if ((($_FILES[“file”][“type”] == “image/gif”)

|| ($_FILES[“file”][“type”] == “image/jpeg”)

|| ($_FILES[“file”][“type”] == “image/jpg”)

|| ($_FILES[“file”][“type”] == “image/pjpeg”)

|| ($_FILES[“file”][“type”] == “image/x-png”)

|| ($_FILES[“file”][“type”] == “image/png”)))

  {

  if ($_FILES[“file”][“error”] > 0)

    {

    echo "Return Code: " . $_FILES[“file”][“error”] . “<br>”;

    }

  else

    {

    echo "Upload: " . $_FILES[“file”][“name”] . “<br>”;

    echo "Type: " . $_FILES[“file”][“type”] . “<br>”;

    echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " kB<br>”;

    echo "Temp file: " . $_FILES[“file”][“tmp_name”] . “<br>”;

    if (file_exists("/images/" . $_FILES[“file”][“name”]))

      {

      echo $_FILES[“file”][“name”] . " already exists. ";

      }

    else

      {

      move_uploaded_file($_FILES[“file”][“tmp_name”],

      “/images/” . $_FILES[“file”][“name”]);

      echo "Stored in: " . “/images/” . $_FILES[“file”][“name”];

      }

    }

  }

else

  {

  echo “Invalid file - No content type”;

  }

?>
[/lua]

Hi, I am using the same code type to upload a .3gp audio file recorded on my android. I do not think the sound file is complete though because it will not play and trying to find out how to determine this. If you find an error with the uploading part on corona let me know please.

Warren

Most PHP installations want to use a special format called MultiPartFormData to do POST uploads.  Our network.upload() is doing a very simple upload and it is not doing MultiPartFormData.  So some REST API’s might take it.  PUT instead of post might get better luck.  There is a file in the community code that does the MultiPartFormData using network.request() and it does a base64 encoding on the data (since binary data and PUT/POST requests don’t get along all that well).  

The PHP scripts similar to what is listed doesn’t decode the base64 data, so your file on the server may actually be plain old Text and needs run through a decoder to get it back to it’s original binary.

We will be having something for this very soon…

Thanks for the info Rob. If you are doing a very simple upload that is not MultiPartFormData can you tell what it is doing? I am using a .NET web server control in a winforms program that seems to only accept uploads as multipartformdata so I have to change something on one end or another.

Go look in the community code for the MultiPartFormData helper class.  The sample there is pretty straight forward.  And assuming nothing major happens, this will be tomorrow’s Tuesday Tutorial.

Hi, I am using the same code type to upload a .3gp audio file recorded on my android. I do not think the sound file is complete though because it will not play and trying to find out how to determine this. If you find an error with the uploading part on corona let me know please.

Warren

Most PHP installations want to use a special format called MultiPartFormData to do POST uploads.  Our network.upload() is doing a very simple upload and it is not doing MultiPartFormData.  So some REST API’s might take it.  PUT instead of post might get better luck.  There is a file in the community code that does the MultiPartFormData using network.request() and it does a base64 encoding on the data (since binary data and PUT/POST requests don’t get along all that well).  

The PHP scripts similar to what is listed doesn’t decode the base64 data, so your file on the server may actually be plain old Text and needs run through a decoder to get it back to it’s original binary.

We will be having something for this very soon…

Thanks for the info Rob. If you are doing a very simple upload that is not MultiPartFormData can you tell what it is doing? I am using a .NET web server control in a winforms program that seems to only accept uploads as multipartformdata so I have to change something on one end or another.

Go look in the community code for the MultiPartFormData helper class.  The sample there is pretty straight forward.  And assuming nothing major happens, this will be tomorrow’s Tuesday Tutorial.

Hi,

I’m having the same problem, whether using the above example or the multipartformdata example.  

One of two things happen

  1. Server returns a 404 if any header with boundary information is sent

Or

  1. Server gets not data in any $_POST or $_FILES arrays

The php I’m using works for uploading images as I’m using it in a standalone IOS app which uses the same POST multiform/boundary line creation.

So I don’t think its the server per se and I’ve looked at the code in the multipartformdata and it looks the same as what I’m sending in my Xcode program, but for the love of <insert deity> I can’t get it to work.

Does anyone know of any php settings that might need to be tweaked?

Again, the server will accept uploads and the php code is good as it works under native IOS.

Thanks

Hi,

I’m having the same problem, whether using the above example or the multipartformdata example.  

One of two things happen

  1. Server returns a 404 if any header with boundary information is sent

Or

  1. Server gets not data in any $_POST or $_FILES arrays

The php I’m using works for uploading images as I’m using it in a standalone IOS app which uses the same POST multiform/boundary line creation.

So I don’t think its the server per se and I’ve looked at the code in the multipartformdata and it looks the same as what I’m sending in my Xcode program, but for the love of <insert deity> I can’t get it to work.

Does anyone know of any php settings that might need to be tweaked?

Again, the server will accept uploads and the php code is good as it works under native IOS.

Thanks