RTF file upload in PHP and problems with UTF - php

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 'download file ';
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');
?>

Related

How to convert php file into html file

I'm a frontend developer and I'm facing with a problem.
Whenever I'm building a website, I'm using PHP to include the template files, so I get a redundant code.
But when I want to generate this file into an HTML file I open up the PHP file in the browser to copy/paste the code to an HTML file.
How can I make this process to be way much faster, or how could I avoid to do these things manually? Maybe there is a program to do this or something?
You can use a recursive wget.
Say your webserver runs on your localhost, you can run:
wget -r -k localhost
Be careful: wgetdoes not perform a search on which pages are available, it simply looks at links (the <a> tags) and will capture these as well. As long as everything is reachable from the index page (not necessarily on the index page), it will be downloaded.
wget is a linux program, but I guess there is a Windows application with the same name/options as well... As #rkbvkleef points out, it's part of the MinGW package.
Basically your php file (which runs on server, could be local server) contains or generates your HTML code to present on browser. You can simply write HTML code out of tags in a php file and it will work. Or if you want to generate some HTML based on some conditions you are checking inside php or using some variables in php then you can use echo function. It will display whatever string you echo on your webpage.
<?php
$name = "Murtaza";
echo("<h1>Hello ".$name."</h1>");
?>

determine if PDF file is openable and not corrupt

I am wandering if anybody has a reliable way of determine whether a PDF document is actually a PDF document, and that it isn't corrupted.
I generate reports on my system and I want to be certain that the data returned by another system contains an openable PDF document (and that the data is not corrupt).
At the moment, I am basically looking at string length (the PDF gets stored into a variable, not a physical file).
Any recommendations to do this in PHP would be great.
If you just want to make sure the file is a PDF file, without checking that it is a completely intact pdf file with no issues, you can read the first 5 bytes of the file and for a PDF file they will be exactly equal to the string "%PDF-"
This is how the file program in linux identifies PDF files.
But if you want to make absolutely sure there are no errors anywhere in the file, you can run a program that processes the entire file, and see if that program returns success.
In linux you can use ghostscript ("gs") to render the PDF document to any format.
Or you can install acrobat reader, and run acroread as a command line program to convert it to postscript:
acroread -print -toPostScript [your_file.pdf]
To do either of these you will need to use the system PHP function. To check of the program ran successfully, you need to pass a variable in the second parameter to system that will receive the return status.
You can use pdfinfo, centos installation command:
yum install poppler-utils
... and use pdfinfo command. The PHP code is as follows:
if(!exec("pdfinfo test.pdf")){
echo "file is corrupted"
}

php code turned gibberish?

I went to edit this PHP file - it's supposed to generate those captcha security images on contact forms - the images werent working, so I was going to see if there was a broken path or something i could fix simply.
But when I opened the file it looked like this:
http://mydomainsample.com/explosion/screenshots/Screen%20Shot%202012-05-17%20at%209.34.14%20AM.png
complete gibberish.
Is it possible this happened somehow while downloading the file from the server? I did not have ftp access to the site originally - we got control of the domain and transferred it from one host to another.
I used site sucker to backup the site before transferring, but it downloads php files as html files. you end up with filename.php.html.
in the past this has never turned the php into incomprehensible gibberish, so i dont understand why it did now.
The problem is, you cannot use programs like "site sucker" to get PHP files. This is because when you get a PHP file from a URL, the file is executed, and you're getting the output of the script. That's why you get .php.html.
It doesn't "turn the php into incomprehensible gibberish", the server runs the script and you're getting the output. Most of the time the output is HTML, which you can open as text. In this case, the script's output is a PNG file, thus why you see "gibberish". Rename the file to .png, then you'll see the image.
You need to get FTP access in order to get the PHP source.
That's a PNG image, not PHP source code.
The file actually looks like a PNG image, maybe you just downloaded output the PHP script has generated?

Take file information?

So I know how to upload a file to a web-server with PHP. Instead of uploading it though, I just want to read the data from the file and use it, WITHOUT the upload part. Could someone link me up or give me an example plz?
from HTML, the file is always uploaded to the server, to a temp directory. if from PHP you don't move it to another directory, it will be deleted later, but you can still use it and read it on the script that handles the upload, as shown in the example of is_uploaded_file()
The only way to do that is to send the data of the file via POST and work with it via something like
$postData = file_get_contents( 'php://input' );
PHP is a server-side language, which means it either needs a server-side copy of the file (since it can't access the client) or you need to send parts of the file via common HTTP request methods that PHP can work with (POST or even GET)
There may be a way using JavaScript, but I can't think of any
a javascript possible solution:
https://developer.mozilla.org/en/DOM/FileReader
not cross-browser, works only in firefox and webkit html5 api compatible versions

PHP run code from txt file

If i had a text file on my web server, which contains full PHP code, all properly formatted, could i use PHP fopen to read the text file and echo the output of the PHP to the browser. Ie. Run PHP code that is held in a text file rather than hard coded?
Thanks
It's perfectly possible to do this (it's just another file after all), although I'd be tempted to directly include it rather that messing around with fopen/eval, etc. (N.B.: The file would of course have to be "fully formed" and begin with "<?php", etc. for the include to work.)
However, I'd be very wary of naming the file with a .txt extension as this will mean that it'll be possible to browse the contents of this file directly from the browser if it exists in the public HTTP docs area. As such, why not simply write the data into a .php file - this will be no more difficult than a .txt file and offers the advantage that it always be parsed by the web server if someone attempts to access it.
You can read in the file as you suggested using fopen you can then execute the string that is read in using eval. I wouldn't recommend this. Try another solution to what ever the actual problem is.

Categories