Hi all,
I know this question has been asked before but there was no clear answer. I am trying to upload images my server and many of them are coming out corrupted (the bottom parts of the image are not coming out correctly). I am using a multi-part form and build 1076.
Lua file
[lua]
function mysqlDB:uploadImage (inspecData, inspectionID) – Function to upload images
local multipart = MultipartFormData.new()
local URL =basicURL … “uploadImage.php/”
local image = inspecData[#inspecData].Entry
print(image)
multipart:addFile(“myfile”, system.pathForFile( image, system.DocumentsDirectory ), “image/jpeg”, image)
local params = {}
params.body = multipart:getBody() – Must call getBody() first!
params.headers = multipart:getHeaders() – Headers not valid until getBody() is called.
local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
print(“imageUploaded”)
if #inspecData > 1 then
inspecData[#inspecData] = nil
self:uploadImage (inspecData, inspectionID) – iterates though and calls itself.
else
self:updateDBDetails (inspectionID)
print(“All Pictures uploaded”)
end
print ( "RESPONSE: " … event.response )
end
end
network.request( URL , “POST”, networkListener, params)
end
[/lua]
uploadImage.php
[lua]
<?php
$myparam = $_POST[‘myFieldName’];
echo $myparam;
$target_path = “./images/”;
$target_path = $target_path . basename( $_FILES[‘myfile’][‘name’]);
if(move_uploaded_file($_FILES[‘myfile’][‘tmp_name’], $target_path)) {
$src = $target_path;
// $dst = ‘/tmp/decoded_’.basename( $_FILES[‘myfile’][‘name’]);
$dst = $target_path;
base64file_decode( $src, $dst );
echo "The file “. basename( $_FILES[‘myfile’][‘name’]). " has been uploaded”;
} else{
echo “There was an error uploading the file, please try again!”;
}
function base64file_decode( $inputfile, $outputfile ) {
/* read data (binary) */
$ifp = fopen( $inputfile, “rb” );
$srcData = fread( $ifp, filesize( $inputfile ) );
fclose( $ifp );
/* encode & write data (binary) */
$ifp = fopen( $outputfile, “wb” );
fwrite( $ifp, base64_decode( $srcData ) );
fclose( $ifp );
/* return output filename */
return( $outputfile );
}
/*
$target_path = “files/”;
$target_path = $target_path . basename( $_FILES[‘file’][‘name’]);
if(move_uploaded_file($_FILES[‘file’][‘tmp_name’], $target_path)) {
echo "The file ". basename( $_FILES[‘file’][‘name’]).
" has been uploaded";
} else{
echo “There was an error uploading<br> the file, please try again!”;
}
print_r($_FILES);
foreach (getallheaders() as $name => $value) {
printf("\n$name: $value\n");
}*/
?>
[/lua]