Upload text as file to another server with PHP - php

I'm working with a 3rd party API that allows me to upload a file to its database. This file has an specific format, but it's plain text.
What I want to do, is generate the text by myself with a PHP script on my own site and upload it to it's server like a file.
The catch is: I want to do this without have to save a temporary file on my own server and if possible, avoiding the use of cURL.
Thanks in advance.

file_put_contents('http://...', $your_text_file);
However, if the API has http basic auth on it, you'll ned to us a stream context to set all that up.

Related

Can you force file download with the Dropbox API?

I'm trying to build a basic web application using the Dropbox API. I have the file upload/folder listing etc. working but cannot find in the documentation how to force the file to download to the user's browser. Is this possible?
If it is can someone point me in the right direction? I'm using the standard PHP SDK.
Dropbox.com: How do I force a file to download from the web
Force a file or folder to download
To cause the browser to download a file or folder rather than display
it, you can use dl=1 as a query parameter in your URL. For example:
https://www.dropbox.com/s/qmocfrco2t0d28o/Fluffbeast.docx?dl=1 Note
that the original share link URL may contain query string parameters
already (e.g. dl=0), so app developers should make sure to properly
parse the URL and add or modify parameters as needed.
And if that doesn't suffice you can check Wikihow: How to Force a File to Download from the Web on Dropbox, with nice screenshots.
If this is not what you had in mind you have clarified that in your question. You still can do that now.

Is there away to upload a file through RestFUL API PHP?

I am trying to develop a RESTFUL API call in PHP , where someone will send me a file through the URL to upload
something like:
script.php?file_name=text.txt
is there away I can take text.txt and upload it in PHP?
To clarify:
lets put it this way , what are the ways that a end user could send a file to a PHP program?
The problem with this is that the REST server is not aware of the end user's machine in any way. So, say for instance that your end user is at yoursite.com/upload where they fill out a form with the upload credentials which posts to api.yoursite.com/uploads/do or whatever. As far as the api is concerned, yoursite.com is making the request, not the end user.
So, no. In my opinion there is no safe way to do this. The best alternative would be to upload the file and then HTTP POST the contents to the rest server. That can be tricky if the file is much larger than a few kilobytes, and you would want to do all sorts of security checking before writing the file to the server. The other option would be to use yoursite.com to upload the file to a temporary location and then send some information to the rest server with details on out to CURL the file contents from the first server. Also, can be insecure.
What problem are you trying to solve? What language framework? Can you give more details please?

is it possible to upload an image with only jquery with out using any php

i am trying to upload an image with Ajax/jquery without refreshing and also without using any php in back-end.
i searched alot but all are using php at back-end, my requirement is not to use php.
http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html
http://dondedeportes.es/uploader-previewer/
if not possible without php . please suggest me some alternatives .
No, it's not possible to upload an image without some kind of backend to process the upload.
You can't upload the image to the server without server-side processing.
If your goal is just to show the image in the page, without uploading it to the server (perhaps as a preview), that's possible on newer browsers via a combination of the File API and a data: URI. The File API gives you client-side access to the file a user identifies, including the ability to read its contents; and a data: URI lets you put an image on the page using the data from that image file.
What kind of server are you using? If it supports Webdav, you could use a Javascript Webdav-Client. But for this, the server has to be configured.

PHP: Stream PDF (make PDF accessible only from webpage)

I have a site built in PHP and we have some PHP pages that require logging in that produce links to PDFs. The problem is that those PDFs (which were copied out to Amazon Cloudfront) were indexed and available for searching.
What is the easiest way to stream an existing PDF to the browser, making it so that the user has to login to be able to see the document? I'm hoping that there is some simple Header code or something where I can say "load this file on the server that's not accessible on the web and output it as PDF".
Any thoughts/suggestions?
Thanks!
You can use htaccess (or similar) to redirect any requests for a .pdf document to a PHP script, passing the requested file name. The script can then validate the log-in credentials, and if the user is logged in it can then send PDF headers, fetch the PDF document (file_get_contents) and output the code.
You can either block access to files (as mentioned in the other answer) or (more cleverly, IMHO) you can pass the file through to the browser after checking credentials (or doing pretty much anything) in PHP. There are code examples and a discussion here: http://bytes.com/topic/php/answers/159354-pass-through-any-file

Automatically attaching and emailing a zipped file to PHP with no User input

I have a program I wrote that when it has an error, it saves infomation of the error in a zip file in their TEMP directory, and then opens their browser to my PHP file.
I want the PHP file to automatically go to a specific location (their temp zip) that will be passed via HTTP POST arguments and attach the zip folder to an email to myself. It should be noted that my mail() command is connected to an google SMTP server.
Can this be done?
If not, what do you suggest as an alternative. I suppose I could pass the binary data as a HTTP Post and then have PHP recreate the zip? All ideas are welcomed.
I'm not sure you can do that via the browser - no browser can upload a file without users' consent - that's insecure and hence it is not allowed.
You could rather try making a connection to the server, not open the web page on your server, from your software and upload the file that way - cURL would be the method of choice I think - it has plenty of implementations in most programming languages so it shouldn't be a problem. Just try searching for "upload a file with curl" on google.

Categories