使用file_get_contents的PHP post文件不适用于某些文件类型


php post file using file_get_contents does not work for some file types

下面的代码工作得很好,但是如果我将文件更改为使用myFile.pdf或myFile.xlsx,则文件将以0字节创建。工作版本和非工作版本的唯一变化是文件类型。为什么我得到0字节与excel文件和pdf ?

 $file = file_get_contents('myFile.xml');

 $url = 'destination.php';

 $post_data = array(
     "file" => $file,
     "fileName" => "myFile.xml"
 );
$stream_options = array(
'http' => array(
   'method'  => 'POST',
   'header'  => 'Content-type: application/x-www-form-urlencoded',
   'content' => http_build_query($post_data),
),
 );
 $context  = stream_context_create($stream_options);
 $response = file_get_contents($url, null, $context);

 echo $response;

然后我的目标文件保存文件如下:

 file_put_contents($_POST["fileName"], $_POST["file"]);

?>

在我的实例中,问题实际上是服务器上的文件大小限制,文件类型只是巧合。检查post_max_size和upload_max_filesize