I have been trying to upload a file to a webserver using LUA.
My problem is that I want to upload a file using LUA to webserver which mimics uploading a file like from a browser.
I was successfully able to upload file using server, where I can do file_get_contents('php://input'), where i get contents and mime_decode on it and save that file.
But, i want to achieve where i can do like $_FILES['file_name'], on server side using lua.
so does anybody have idea how to do this in LUA?
Regards.
You can use the HTTP sockets library for sending HTTP requests and ltn12 library for filters (file input). Both should be provided with Lua.
http = require("socket.http")
ltn12 = require("ltn12")
http.request{
url = "url://to.server/upload/script.php",
method = "POST",
headers = {
["Content-Type"] = "multipart/form-data",
["Content-Length"] = sizeOfFile
},
source = ltn12.source.file(io.open(pathToLocalFile)),
sink = ltn12.sink.table(response_body)
}
print(response_body[1]) --response to request
Related
I'm new to the Google Drive API and I've been using it in a sort of an unorthodox manner. Right now I have just an API key to authenticate and I download files using the https://www.googleapis.com/drive/v3/files/file-id?alt=media&key=api-key scheme. This can successfully download files, except that their name is actually just the file ID without any extension. I've tried a few things so far including setting headers in my PHP code that define content-type, content-disposition, and even content-length attributes, but that used my server's bandwidth even if used properly. I've also tried that HTML download attribute for a tags, but I found it only works with blobs or local files. I've also tried some Javascript to no avail. Here is part of my code right now, which uses blobs to download and rename the file, however I would like to not have to go this route and seek a better option:
<?php
$id = "SOME-ID-HERE";
$key = "API-KEY-HERE"; // Google Drive API Key
$pre = "https://www.googleapis.com/drive/v3/files/";
$dlink = $pre.$id."?alt=media&key=".$key;
?>
<script type="text/javascript">
window.URL = window.URL || window.webkitURL;
var xhr = new XMLHttpRequest(),
a = document.createElement('a'), file;
xhr.open('GET', '<?php echo $dlink ?>', true);
xhr.responseType = 'blob';
xhr.onload = function () {
file = new Blob([xhr.response], { type : 'application/octet-stream' });
a.href = window.URL.createObjectURL(file);
a.download = 'filename.ext';
a.click();
};
xhr.send();
</script>
As you can see, I'm not even using the Drive API PHP libraries, I'm just using their link which either returns the file contents or the metadata of the file. I suspect that this is why the file being downloaded is renamed to just the file ID, but I don't know for sure. Since I don't want to use blobs unless I have to, my main question is: Can I use the Drive API (properly by including the PHP libraries for it) to download files from Google Drive, but have them be named exactly as how they are already named in Google Drive? Secondly, if this is possible, does it use my server's bandwidth for serving the files or is it directly from Google's servers? I want to try to avoid using my server's bandwidth if possible to accomplish this.
UPDATE: I have a server here that works for this but it's a hacky method of getting it to work. I would still feel safer using the API because it's future proof.
https://drivedl.cf/file-id
I wouldn't recommend using it yet though, it works but there's some potential bugs I need to weed out
UPDATE 2: https://drivedl.cf/file-id is currently unusable because I'm working on it right now, trying to use the API but It's giving me issues
I was wondering if any one can help with following issue.
I have an Api with node/express for image uploads
server1
var upload = multer({ dest: 'uploads/'})
app.post('/api/upload', upload.single('file'), function(req, res, next) {
if (req.file && req.file.path) {
//used to have imgur upload
//imgur.uploadFile(req.file.path)
// .then(function (json) {
//});
}
});
server2
I have PHP Api for image uploads as well which I have set up for self-hosting using pictshare. I want to redirect file uploads to new API by redirecting uploads to PHP Api server.
Have tried multer, multiparty, needle, request and various other methods... but somehow couldn't figure out.
Is there way to direct multer to new destination?
File is being saved in uploads/ folder, maybe better would be to upload/direct that file to new server and return new url from server2 ?
Looked around for pipelining upload images, with not much luck..
note: server1 api is being used by mobile app so wouldn't want to release app update if it can be handled from server side.
To upload a file (image or whatever) you need to have it in your drive first (or in memory, but I will tackle the drive case first). Then you can read it and send it.
The APIs that receive files use to receive forms. Usually they expect to receive a form where the attachment comes as a file field.
Here you can see a working example uploading an existing file in my drive to an external API by using the 'superagent' library for Node.
const
fs = require('fs'),
agent = require('superagent');
const stream = fs.createReadStream('path/to/downloaded/file');
agent.post(`urlOfApi/uploadFileEndpoint`)
.type('form')
.attach('file', stream.path);
I have a few questions regarding uploading files to a HTTp request (from an applet). This is what I am following:
I am taking a screenshot of the screen using Applet's robot and storing the same on the local file system. The file I have saved on the local file system needs to be uploaded to Apache, eventually to be accessed by PHP.
public void sendBufferedImage(File file, BufferedImage screenImage, String urlPath) throws Exception
{
ImageIO.write(screenImage, "GIF", file); // This is to store the file in
a temp location on the local file system.
url = new URL(urlPath);
urlcon = (HttpURLConnection) url.openConnection();
urlcon.setRequestMethod("POST");
urlcon.setDoOutput(true);
urlcon.connect();
out = new DataOutputStream(urlcon.getOutputStream());
out.write(file.getPath().getBytes());
}
Does this complete writing the file to the HTTP request?
If so, how should reading the HTTP request work to get the file handle out of the byte array written to the HTTP request?
Can a file be written to the HTTP request only via byte array?
A sample of how the php code would look like while getting the file handle out of the HTTP request would help a great deal.
Thanks,
Use Case: I am working on an image uploader which uses ajax upload function.I want to upload images to a subdomain user creates on the website.For example,when the user creates a domain on the website I copy a php script for uploading images to the new domain viz image-cropping.php.I want to send a request to this file when the user uploads any image to his domain.
Issue:When I try to upload an image I get Error: Permission denied to access property 'readyState'.My calling js file is on xyz.google.com and the upload php script is on abc.google.com.
Research
After doing some googling and research I learnt javascript won't allow to send request cross domain and it needs a http proxy to handle this.Here is the code I have tried.The script to run the ajax uploader.In action I have the path to file on other domain(the path is built dynamically).
new AjaxUpload(btnUpload, {
action: 'includes/modules/domain_creation/proxy.php',
name: 'image',
onSubmit: function(file, ext){
if (! (ext && /^(jpg|png|jpeg|gif|JPG|JPEG|PNG|GIF)$/.test(ext))){
alert('Only JPG, PNG or GIF files are allowed');
return false;
}
$('#thumbImg').html('<img src="http://localhost/gobiggi_VS_2_2/images/appImages/imageLoader.png" />');
},
1) Am I doing it in the right way(working on it for first time)?Is the proxy actually needed?2)How and where can I set the proxy so that the error permission can be negotiated?3)What security issues does it open up(Is it safe?If not what's an alternative)?Any pointers or suggestions would be helpful for me.Thank you for your time.
Update:
I am using this proxy script for uploading the image.Part of the code is
$domainUsername = $_SESSION['domainUsername'];
$domainNameWeb = $_SESSION['domainName'];
//$fileParameterProxy = $_FILES['image'];
//Destination URL: Where this proxy leads to the upload script
$destinationURL = 'http://www.'.$domainNameWeb.'/'.$domainUsername.'/upload.php';
//The only domain from which requests are authorized.
$RequestDomain = 'abc.net';
Now I don't get the Error for permission but I am not able to get the image on to the server.When I try to do print_r($_FILES) I get a blank array on my upload script.
I believe I am missing something!!Can someone please correct?
Thank you for your time!
1 and 2) You have to set your proxy as action, because that is the place where you are allowed to upload the files. The proxy then will do the request to the other domain, where it can send the files to.
3) Depends on your proxy implementation. You should avoid to store the files locally or execute/include anything from user input, like always when writing php scripts. Directly send the tmp file to your destination server, this will also be the fastest implementation.
On my website I have a .php script to which our customers can post orders.
$destname = CreateUniqueOrderFileName();
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $destname))
echo "OK";
else
echo "ERROR: move";
}
So for my clients I wrote an app in C# and if I upload an order I do something like this:
var client = new WebClient();
byte[] response = client.UploadFile("http://mywebsite/order.php", "POST", orderFile);
But now I have a customer who uses Oracle and wants to post an order using PL/SQL.
I try to tell him he has to do a file uploaded via HTTP POST and use multipart/form-data but he does not understand me. Maybe I'm using the wrong vocabulary or not using standards.
So does someone have an example of how to post a file to a .php script in PL/SQL or a suggestion where to find more information uploading a file to a .php webpage with an Oracle client.
I think you can use PL/SQL package UTL_HTTP to do a HTTP POST
http://awads.net/wp/2005/11/30/http-post-from-inside-oracle/
Another option might be to use a Java Stored Procedure
We could find a way to do a POST request using the Content-Type multipart/form-data according to RFC2388. So I changed the .php script to something like this.
$fp = fopen('php://input','r');
$content = stream_get_contents($fp);
$destname = CreateUniqueOrderFileName();
$isWritten = #file_put_contents($destname, $content);
if($isWritten===false)
{
echo "ERROR: could not write file: ".$destname;
return;
}
Using this we where able to post the order using the UTL_HTTP lib to do a HTTP POST.