Hi All,
I need your help to fix below code to upload file to my web server.
[lua]http = require(“socket.http”)
ltn12 = require(“ltn12”)
local filename
local response_body
filename = “pic1.png”;
– upload file path
local path = system.pathForFile( filename, system.DocumentsDirectory );
print(“Path=”…path);
– Get file size from local document directory
local path = system.pathForFile( filename, system.DocumentsDirectory )
local fh = io.open(path)
local len = fh:seek( “end”, 0 )
io.close(fh)
print(“len=” … len)
response_body = {}
http.request{
url = “http://www.mydomain.com/web_service/upload.php”,
method = “POST”,
headers = {
[“Content-Type”] = “multipart/form-data”,
[“Content-Length”] = len
},
source = ltn12.source.file(io.open(path)),
sink = ltn12.sink.table(response_body)
}
print(response_body[1]);[/lua]
<?php <br> // Configuration - Your Options
$allowed\_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
$max\_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload\_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $\_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in\_array($ext,$allowed\_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($\_FILES['userfile']['tmp\_name']) \> $max\_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is\_writable($upload\_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
// Upload the file to your specified path.
if(move\_uploaded\_file($\_FILES['userfile']['tmp\_name'],$upload\_path . $filename))
echo 'upload success'; // It worked.
else
echo 'upload error'; // It failed :(.
?\>
Thank you in advanced.
Admin6 [import]uid: 45018 topic_id: 9473 reply_id: 309473[/import]