Send files via cURL post PHP. XML or plain text? - php

So I've been working for around two months with cURL post requests via php and when it's about sending files I've been using the .txt extension files and my questions are:
Is it better (or even possible) to send the data as a .xml file?
And if yes, curl_file_create() function also works with .xml files or is there another way to accomplish this?

Is it better to send the data as a .xml file?
Yes. No. Maybe.
It depends on what the server expects you to send.
It depends on what format is most suitable for expressing the information you are sending.
JPEG might be the best format!
curl_file_create() function also works with .xml files
It deals with files. Nowhere does it say it only deals with particular types of file.

Related

Uploading a file from a .NET client to PHP on a server

I'd like to use .NET to upload a picture file from the local disk to a web server where it will be received by a PHP script and saved to the server. I'm not sure of the preferred way to transfer the data, now that I've realised it isn't as simple as I'd hoped.
The problem is that PHP's file upload mechanism only accepts data encoded as multipart/form-data, and I can't see a way to get WebClient to upload the file this way without doing the encoding myself into a byte array and uploading that.
Which would be the neater solution: should I go through the hassle of doing this encoding on the client? Or, if I just use WebClient.UploadFile, should I be able to receive it from php://input and, if so, will it need decoding?
I have found several examples of doing the encoding, on this and other sites, so I don't need help with that. I'd just like an opinion on whether such client-side encoding is sensible or necessary, or whether I can do the work on the server instead by not using $_FILES and receiving the data in a more 'manual' way.
In the longer term I will be seeking to better understand the HTTP protocol.
I've worked out my own answer, thanks to Janoszen's comment. Just the kind of simplicity I wanted.
Here's an extract from the .NET client (example in Visual Basic):
Dim wc As New WebClient()
wc.UploadFile(url, "PUT", filename)
And from the PHP on the server:
file_put_contents(filename, file_get_contents('php://input'));
Thanks Janoszen!

How to create a WebService that receives Files in PHP sent from Delphi?

I have a WebService that works that way:
I send a file through ftp into a specific folder in my remote server;
I call a web service in that same server so that the file name and path can be recorded in the data base. Only this way the system will know of the file existence.
I do it that way because I do not know how to make a web service that can receive a file by HTTP POST directly.
The server works under PHP & MySQL.
The client that sends the file and register it is made in Delphi. I use Indy now.
In the future I intend to make it full web but my client is still addicted to windows.
So, can I make a rest web service that receives a pdf file and also receive data about this file, like "from", "to", "description"...? How?
In order to upload a file, you must perform a POST call to your WebService. (You can also perform a PUT operation, but that's not covered in this answer).
The php part:
In php, you use the $_FILES associative array, which contains the items uploaded to the script using the mentioned POST method.
Once you accept the file, you use the move_uploaded_file function to save the file in your server disk, and then you can read the file from there and post it to a database or any other operation you want to perform.
I recommend you to read the referenced articles in the handling file uploads php manual entry.
You can also find a working example on the php file upload w3schools article.
The Delphi part
There are various libraries that allow you to perform HTTP operations from Delphi. My preferred one is Indy Sockets, and you'll find a great number of questions (and answers) here on StackOverflow, for example: Http Post with indy which includes a flie attachment.

How to read a client-side file header from a webpage?

I am making an online tool for identifying certain file types. I need to access some byte values from the file header to do this.
The user selects the file on the client machine. Somehow, I need to get the key byte values from the file, and then these are looked up in a server side database to categorize the file.
How can I read bytes from a client-side file?
I know I could have the user upload the file to the server, but these files are very large, and I only need a few bytes, so it would be slow and wasteful to upload the whole file.
Could I somehow upload part of the file? It seems it is difficult to cancel a html form upload and the file-part is not available after cancel. Is this correct?
Is it possible to read a file in javascript? I have googled this, but the answer is unclear. I have read that it is possible with a java applet, but only if the applet is signed.
Is there some other way?
You can use html5, but will need to fallback on flash or some other non-javascript method for older browsers.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
So. as Said above you must use non-javascript methodds. But each of this methods has some minus.
FLASH - bad work with proxy. Really bad. Of course you can use flash obly for get base64 code of file and give it to js. In this case this will be work greate.
Java Applet - greate work but not many users have JVM or versions of JVM may not be sasme (but if you will use JDK1.4 or 1.5 thi is no problem).
ActiveX - work only in IE and on Windows
HTML5 File Api - not cross browsers solution. Will be work only on last browsers and not in all.
of course much better use server side - in php for example getmimetype and other functions.
But I can manually change headers of my file. For example i can add to php file headers from jpeg or png - and your script will be think that is image.
So this is bad solution : use headers. For check filetype maybe simple use mimetype of file of trust to user and generate icon through file extension

Using uploaded file without saving to server disk

I have created a PHP script to upload a file, unfortunately I don't have permission to save files on the disk. I have to upload an excel file (using phpexcel), then I have to read all the rows in the file and save to disk, Is there any way for me to process this file without saving to disk, I tried to read $_FILES['file1']['tmp_name'] but it doesn't work.
could u please suggest a method to process this file
Thank you for the consideration
By "save to disk" you mean to send it back to the user for him to download it?
Usually, you shall have write access to (at least) the PHP temporary directory. Have you tried whether the form and script work in a local environment? Maybe there is something elso wrong with the upload?!
Finally: Why so you not have the persmission to save files? Are you allowed to create a subdirectory below you PHP file (via FTP) and give that one full permissions?
I tried to read $_FILES['file1']['tmp_name']
most probably you have just encountered an error.
that happens to beginner programmers very often
you have to repair that error instead of looking for odd workarounds.
Start from checking $_FILES['file1']['error']
what does
var_dump($_FILES['file1']['error']);
say?
Instead of sending your files with a form (multidata over HTTP POST), you can send your files with a little bit of Javascript with the HTTP PUT method to your server.
This scenario is described in the official documentation of PHP -> PUT method support.
Due some restrictions described in the documentation you have to do some workarounds to be able to work it properly.
You can read the direct input stream from your Webserver. The data will be piped from your Webserver to your PHP programm and will be only saved in memory.
To do a PUT Ajax call with jQuery was answered here. You can use a jQuery upload plugin like Uploadify.

Does GXml work with KMZ files?

Can Google Map API GXml parse .kmz files directly? if not how is the best way to convert .kmz file to .kml? The .kmz file is stored on database and PHP code is used to retrieve it.
I'm pretty sure that there's no way to unpack zipped data with Javascript, which you'd have to do before passing the data to GXml.parse.
GGeoXml can handle KMZ files. It does it by passing the URL to a Google server which unzips the data and parses it there, then returns the individual overlay objects to the Javascript client.
Since you're reading the data with PHP, you might consider unzipping it in your PHP script, and serving the unzipped data to the client. You may need to do some work to your PHP configuration in order to enable the PHP Zip File functions.

Categories