读取字符串中文件的字节数

时间:2013-03-12 13:07:42

标签: php android

简单的理论问题:

使用Php将文件上传到服务器,我选择此机制(如果可能):

读取文件的字节,将其作为简单文本放入字符串,然后将其作为简单文本消息发布到服务器($ _ REQUEST),然后将此“文本”写入服务器上的新文件,

所以我的问题是:

如何读取文件的字节并将其存储在String / StringBuilder中'因为它们是'简单文本?

1 个答案:

答案 0 :(得分:1)

你可以使用file_get_contents二进制安全。

$binaryContent = file_get_contents('path/to/file');

如果您在发送内容时遇到问题而不是编码:

$binaryContent = base64_encode($binaryContent);

但我会使用curl上传文件:

来自php.net http://www.php.net/manual/de/function.curl-setopt.php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);