0

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 ?

Nikita
  • 189
  • 1
  • 4
  • 16
  • Did you want to find the size of file ??? – Deep123 Mar 12 '13 at 13:09
  • you ca send content via ajax call to the server, but with javascript, it will be difficult to access local file – MatRt Mar 12 '13 at 13:09
  • What is actual meaning of bytes ?? – Deep123 Mar 12 '13 at 13:09
  • oops sorry now get your meaning ! but your question is little bit confusing – Deep123 Mar 12 '13 at 13:10
  • it's not good idea. send file location. then upload it. first do a search on how to upload a file from android. – Akshay Deep Giri Mar 12 '13 at 13:13
  • Thanks all for your Answer , Bytes mean something like this "jivgfuor_ç'_è"è_-é"ç_àè$$$$"'(-"à(çé")à(_éç_'àç" , i want to Read the Bytes , Store in a String , Send this String to the Server as Simple Text Message , then Write into a New File – Nikita Mar 12 '13 at 13:19
  • Sorry if its a little Confusing , its like when you Open an Audio File for Example with NotePad , Copy its Content to a New File then Rename as .mp3 or .wav , if its Technically Possible ? – Nikita Mar 12 '13 at 13:21
  • Can SomeOne UpVote My Post in the Same Way(UnFair) somes Peoples DownVOted My Post ? – Nikita Mar 15 '13 at 10:15

1 Answers1

2

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);
bitWorking
  • 12,485
  • 1
  • 32
  • 38
  • Sorry , i didnt mentionned i am looking to send it from Android to the Server – Nikita Mar 12 '13 at 13:24
  • ok perhaps this post is helping you http://stackoverflow.com/questions/7476280/reading-a-binary-file-into-a-string – bitWorking Mar 12 '13 at 13:27
  • i looked into this Post : http://stackoverflow.com/questions/3204476/android-file-uploader-with-server-side-php but i want to add an Extra like an Id(Text Name) dos.writeBytes(twoHyphens + boundary + lineEnd); dos .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename=" + exsistingFileName + "" + lineEnd); dos.writeBytes(lineEnd); and get it with $_REQUEST on Php Side – Nikita Mar 12 '13 at 13:28
  • yes this is Exactly what i was looking for , Thanks you , but following the Post here http://stackoverflow.com/questions/3204476/android-file-uploader-with-server-side-php How can i add a String as Extra – Nikita Mar 12 '13 at 13:30
  • dos.writeBytes("Custom Message"); and Php Side $_REQUEST to get the "Custom Message" ? – Nikita Mar 12 '13 at 13:32
  • Can SomeOne UpVote My Post in the Same Way(UnFair) somes Peoples DownVOted My Post ? – Nikita Mar 15 '13 at 10:16