开发者

RTF file upload in PHP and problems with UTF

I want to create a web form which will allow the user to upload text/rtf files in UTF-8 having foreign language content using PHP and then execute a series of commands on it via the exec() function. After this processing I would be giving it back to the user as a download.

I made rudimentary form in html with a file input form and submit button. and the PHP side has the following contents.

$base_di开发者_高级运维r = './uploads';
$cmd = "mkdir -p ".$base_dir.' ; mv -v '.$_FILES['file']['tmp_name'].'$_/'.$_FILES['file']['name'].' ; /var/www/cgi-bin/test.awk'.'/var/www/html/uploads/'.$_FILES['file']['name'].'>'.'/var/www/html/uploads/'.$_FILES['file']['name'];
exec($cmd);
print '<a href="http://oceanfizz.usc.edu/uploads/'.$_FILES['file']['name'].'">download file </a>';

But the problem is that the uploaded rtf files seem to have text changes mainly like

so "é became \'8e abd so forth.

I think it is a problem with the encoding

Can someone suggest a fast and easy way to upload files to a server and get back processed files via the browser at the same time preserving the encoding and contents.


Moving files with unix internal move command isnt realy clever. Use instead the php upload function.

To your encoding problem you could first serialize the content of the uploaded file with serialize(); and if the user requests it unserialize it. This is the best way to transport files along the internet.


We'd need to see the code you are using in the form to post the data to say for sure. As streetparade says - you should use move_uploaded_file() to move the file - not mv.

However once it has got to the server, particularly as its a Unix server, there's no need nor way for the system to change it.

When you say it has changed, why do you think that? I think its far more likely that you are accesing the file via a non-utf8 editor, or you have downloaded it with the wrong encoding type.

Try something like:

 <?php 
 move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/temp/test.txt');
 header('Content-type: text/plain; charset=utf-8');

 print file_get_contents('uploads/test.txt');

 ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜