Early73 Early73 - 5 days ago 8
PHP Question

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

The following code works fine, however if I change the files to use myFile.pdf or myFile.xlsx the file gets created with 0 bytes. only thing that changes for the working version and the non working version is the file type. Why am I getting 0 bytes with excel files and pdfs?

$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;


then my destination file saves the file like this:

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


?>

Answer

In My instance the problem was actually with file size restrictions on the server and the file types were just coincidental. check post_max_size and upload_max_filesize.

Comments