How to receive a file(ie. a text file) from a http post in php?
http, httpwebrequest, php
Solution
Yes, `$_POST` will be empty, you should check `$_FILES` variable for uploaded files: Here is quick snippet:
<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
echo "The file has been uploaded successfully";
}
else
{
echo "There was an error uploading the file";
}
?>
The contents of $_FILES from above script is as follows.
$_FILES['file']['name'] The original name of the file on the client machine.
$_FILES['file']['type'] The mime type of the file, if the browser provided this information. An example would be “image/gif”.
$_FILES['file']['size'] The size, in bytes, of the uploaded file.
$_FILES['file']['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES['file']['error'] Since PHP 4.2.0, PHP returns an appropriate following error code along with the file array
- UPLOAD_ERR_OK – Value: 0; There is no error, the file uploaded with success.
- UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
- UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
- UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded.
- UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.
Uploaded Files will by default be stored in the server’s default temporary directory. Variable $_FILES['file']['tmp_name'] will hold the info about where it is stored. The move_uploaded_file function needs to be used to store the uploaded file to the correct location
Problem
I am sending a file from `client(C#)` using `webclient` or `HttpWebRequest`. I like to know to how to receive the file sent from client in `PHP(Server)` . I have checked `$_POST`, its empty. Client code (c#) : ``` using (WebClient client = new WebClient()) { client.UploadFile("http://path/file.php","POST",@"Data.txt"); } ```