I was working on some projects and suddenly I came up with this issue.
Consider having a file named requestWithFile.htm with the contents like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action='http://127.0.0.1/pv.php' enctype="multipart/form-data">
<input type="text" name="url" value="https://example.com/" /><br>
<input type="file" name="myfile" /><br>
<input type="submit">
</form>
</body>
</html>
and another file which in php named requestWithFile.php which does the same job:
<?php
$requesturl = "http://127.0.0.1/pv.php";
$file = realpath('touploadfile.jpg');
$ch = curl_init($requesturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"url" => "https://example.com/",
"myfile" => "@".$file
));
echo curl_exec($ch);
curl_close($ch);
?>
My post parameters in both situations are:
url => https://example.com/
myfile => a file named touploadfile.jpg
which touploadfile.jpg file is in the same directory with both the html and the php files.
The pv.php file contains codes to display the requests that we have made. Those codes are:
<?php
echo "The \$_POST :<br>\n\n";
print_r($_POST); print "\n\n<br><br>";
echo "The \$_GET :<br>\n\n";
print_r($_GET); print "\n\n<br><br>";
echo "The \$_REQUEST :<br>\n\n";
print_r($_REQUEST); print "\n\n<br><br>";
echo "The \$_FILES :<br>\n\n";
print_r($_FILES); print "\n\n<br><br>";
echo "The apache_request_headers: :<br>\n\n";
print_r(apache_request_headers()); print "\n\n<br><br>";
?>
NOW
If we make requests with the first file requestWithFile.htm, the browser echoes out:
The $_POST :
Array ( [url] => https://example.com/ )
The $_GET :
Array ( )
The $_REQUEST :
Array ( [url] => https://example.com/ )
The $_FILES :
Array ( [myfile] => Array ( [name] => touploadfile.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\phpB1B1.tmp [error] => 0 [size] => 69 ) )
The apache_request_headers: :
Array ( [Host] => 127.0.0.1 [User-Agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0 [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [Accept-Language] => en-US,en;q=0.5 [Accept-Encoding] => gzip, deflate [Referer] => http://127.0.0.1/requestWithFile.htm [Connection] => keep-alive [Content-Type] => multipart/form-data; boundary=---------------------------1848618309200 [Content-Length] => 377 )
but If we make the same request with requestWithFile.php file the result differs:
The $_POST :
Array ( [url] => https://example.com/ [myfile] => @C:\xampp\htdocs\touploadfile.jpg )
The $_GET :
Array ( )
The $_REQUEST :
Array ( [url] => https://example.com/ [myfile] => @C:\xampp\htdocs\touploadfile.jpg )
The $_FILES :
Array ( )
The apache_request_headers: :
Array ( [Host] => 127.0.0.1 [Accept] => */* [Content-Length] => 304 [Expect] => 100-continue [Content-Type] => multipart/form-data; boundary=------------------------122eddaaad7fe29d )
Except the difference between headers (which is because of sending from a web browser in the html file) the question that comes to mind is:
Why the value for $_FILES is empty for the php file?
Why the results for two identical requests differs ?
I myself cant figure it out.