views:

329

answers:

2

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_dir = './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'].'"&gt;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.

A: 

moving files with unix intern move command isnt realy clever. Use intead the php upload function. To your encoding problem you could first serialize the content of the uploaded file with serialize(); and if the user request it unserialize it. This is the best way to transport files along the internet.

streetparade
how do i do that. Can you give me a code segment.
Ankur Chauhan
A: 

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

 ?>
symcbean