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");
}
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
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