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


Read Bytes of a File in a String

简单理论问题:

使用Php上传文件到服务器,我选择了这种机制(如果可能的话):

读取文件的字节,把它放在一个字符串作为简单文本,然后把它作为一个简单的文本消息($_REQUEST)发送到服务器,然后把这个"文本"写到服务器上的一个新文件,

我的问题是:

我怎么能读取字节的文件和存储在字符串/StringBuilder '因为他们是'作为简单的文本?

您可以使用file_get_contents,它是二进制安全的。

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

如果您在发送内容时遇到问题,请对其进行编码:

$binaryContent = base64_encode($binaryContent);

但是我会使用curl来上传文件:

From 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);