javascript - php://stdin reading extra characters -


i having weird problem system making. attempting create system upload files server. not have direct file write privileges, , http post multipart/form-data file uploads not work. unfortunately not have direct access server.

my solution use jquery's $.ajax function upload file via http put, read in stdin in php on server, , upload contents different location via ftp.

everything appears work, somewhere along line, bytes being added. on client, windows claims file (i testing website's favicon.ico file) 7358 bytes. once read in file using standard javascript filereader object, appears 7311 bytes. then, put php script on server, claims read 10890 bytes, filezilla agrees when check dump folder on ftp.

here javascript code upload (jquery 1.11.3 included, , file_input <input type="file" id="file_input"/>:

function upload(){ console.log("upload"); var f = file_input.files[0]; var r = new filereader(); r.onload = function(event){     var d = r.result;     console.log("data loaded");     console.log(d.length);     $.ajax({         url: "upload.php?name="+encodeuricomponent(f.name)+"&mime="+encodeuricomponent(f.mime),         context: document.body,         method: "put",         contenttype: "application/octet-stream",         data: d         }).done(function(data){             console.log("done");             console.log(data);             }); }; r.onerror = function(event){     alert("error reading file!\ncode " + event.target.error.code); }; r.readastext(f); console.log("initiated"); } 

as can see, name , mime type included via query string.

here php accepts upload (part of upload.php):

if(strtoupper($_server['request_method'])=="put"){     echo "uploading\n";     echo "connected\n";     var_dump($_post);     echo "x";     $file=fopen("php://input","r");     var_dump($file);     $x="";     while($byte=fgetc($file)!==false){         $x.=$byte;     }     echo strlen($x);     echo "\n";     $ftp=fopen('ftp://user:pass@example.com/admin/fs/upload/'.uniqid('',true),'w');     var_dump($ftp);     fwrite($ftp,$x);     fclose($ftp);     echo "done\n"; } 

i examined files in notepad, , majority of file appeared unchanged (there weren't http headers or anything), new file refused open in image viewer (the original would). based on differences in files - parts changes, , changed appear random non-visual characters - guess has character encoding discrepancy. ideas appreciated.

thanks,

magikm18

edit

my server apache/2.4.6 , running on x86_64-redhat-linux-gnu (according phpinfo()).

also, tested text-based file (a php script) , worked fine - must file's binary content.

for binary files (i.e. ain't text), can try appending b flags of fopen:

fopen("php://input","rb"); /* ... */ $ftp=fopen('ftp://user:pass@example.com/admin/fs/upload/'.uniqid('',true),'wb'); 

edit


you have send files binary too, in addition receiving stream. see link in comments.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -