Nikita
Nikita

Reputation: 199

Read Bytes of a File in a String

Simple Theorical Question :

To Upload a File to the Server Using Php , i choosen this Mechanism(if Possible) :

Read Bytes of the File , Put it in a String as Simple Text , then Post it to the Server as a Simple Text Message($_REQUEST) , Then write this 'Text' to a New File on the Server ,

So my Question is :

How can i read Bytes of a File and Store it in a String/StringBuilder 'As they are' as Simple Text ?

Upvotes: 0

Views: 1563

Answers (1)

bitWorking
bitWorking

Reputation: 12665

you can use file_get_contents it's binary safe.

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

If you get problems sending the content than encode it:

$binaryContent = base64_encode($binaryContent);

But I would use curl to upload a file:

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

Upvotes: 2

Related Questions