How do I create a PHP page which can accept file uploads? - php

I want to upload a file to my server from some application. How can I code a PHP page to accept this file?

The application should just include the file in an HTTP POST request as would an HTML form with an input of type file. In the PHP code, the posted file contents would be available in the $_FILES array.
Lots more information here.
Keep in mind that the concept of a "file" doesn't mean the same thing over HTTP that it does on a local computer or on the target server. In HTTP, the "file" is just a stream of data wrapped in an HTTP request or response with a given content type and various other headers, no different from a web page or an image or any other request/response.

I would suggest reading the PHP manual chapter on handling file uploads

Related

curl file upload details

I have a custom system running on a server which expects a certain file type.
when uploading a file via posts, the $_FILES variable in PHP contains information such as [type] = image/png. Where does this information come from?
When i manually try to curl to my website via SSH, and i add -F "file=#/a/b/c/d.pqx" the type is set to "application/octet-stream".
Is the curl command translating the uploaded file and suppling this information or is the server/php parsing the file and providing this information?
Is it possible to make a POST request to my website and have the filetype show up as "application/pqx" (pqx is a custom file extension) ? I am more interested in a way to make the right POST request rather than changing the PHP code. Can i customize the curl request to the website and supply the filetype myself?
thanks for all your help!

Return JSON before streaming file or HTTP headers from ifram?

I have a form that uses a hidden iframe to submit a file to a script that changes the file and then returns the changed file. I found that I don't actually have to save the file anywhere if I just do something along the lines of echo file_get_contents(tmp);, where tmp is the path of the file uploaded to the apache tmp directory.
The script also does: header("Content-type: application/octet-stream") so that when the iframe loads, the user is prompted for download. I would like to know, on the client side, if everything went alright with the server. It's not an XMLHttpRequest, so I can't check the headers, and the only thing returned is the file itself.
Is there some way to return some json before streaming the file? Or, is there a way to check the headers of an iframe?
Currently, I've been setting a cookie with the server and checking every half a second with javascript to see if the cookie was set. I would prefer a less hacky solution than this though.
If you stream JSON before the file, the MIME header will be invalid and the browser will not be able to download the file.
In terms of checking "if everything went alright with the server", why wouldn't you be determining this on the server, rather than going round-trip to the client and back again? Allowing it to error-out on the client smells of bad design.

Retrieve the complete details of a remote file without downloading it

How could I extract the METADATA / METAINFO / everything that would describe the REMOTE FILE without downloading it in the first place?
do you have some snippets in PHP?
how about in cURL?
or any language?
thanks
You can't get all the metadata from a file without downloading the entire file, as you have no way of knowing how much of the file you need before you've captured its metadata block, some file formats store metadata at the end of the file (meaning you'll need the entire file anyway), some files don't have any metadata embedded in them at all, and so on.
If you do a HEAD request, you will usually be able to get some basic metadata on the file in question in the form of the content-type and content-length headers returned by the server, but this is typically limited to the MIME type and the file size (and if you're dealing with a script that serves the file and that script doesn't set the necessary headers, you might not even get that).

Take file information?

So I know how to upload a file to a web-server with PHP. Instead of uploading it though, I just want to read the data from the file and use it, WITHOUT the upload part. Could someone link me up or give me an example plz?
from HTML, the file is always uploaded to the server, to a temp directory. if from PHP you don't move it to another directory, it will be deleted later, but you can still use it and read it on the script that handles the upload, as shown in the example of is_uploaded_file()
The only way to do that is to send the data of the file via POST and work with it via something like
$postData = file_get_contents( 'php://input' );
PHP is a server-side language, which means it either needs a server-side copy of the file (since it can't access the client) or you need to send parts of the file via common HTTP request methods that PHP can work with (POST or even GET)
There may be a way using JavaScript, but I can't think of any
a javascript possible solution:
https://developer.mozilla.org/en/DOM/FileReader
not cross-browser, works only in firefox and webkit html5 api compatible versions

PHP - File upload - What is happening internally?

For image upload we use FILE html controller.
How this html controller able to browse in the local system?
After selecting a file , it will be copied and moved to server location.
If the php is ale to copy the local file and move to server , will it be able to do any other manipulations of that file ? like delete!
What is happening actually on file upload?
The HTML control is provided by the browser. The browser is a local application and has access to the user's file system. The file's contents are sent to the receiving script by the browser using standard methods.
PHP has no access to the user's file system at any point, just the copy provided by the browser. Deleting or even reading files on the user's file system is not possible.
Actually php is not accessing local system. After you choose a file and click upload at upload form. The whole file(not location) is sent via POST request. And php just recieves that POST request with the whole file, and stores at server.

Categories