determine if PDF file is openable and not corrupt - php

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"
}

Related

php - What will happen if I overwrite the file itself when it is executing (using ZipArchive)

So as described in question itself, I want to replace the file from which a zip archive is opened and then which is overwriting files with new version.
If still my question is not clear then the thing I want to do is I want to get a zip file from a server and then unzip using CLASS "ZipArchive" and then over write everyfile which is in Zip to destination location, the problem will be the php file by which this thing is happening will gonna be overwritten.
So will the php generate the error or the process will go whatever we want?
On Linux files are not usually locked (see https://unix.stackexchange.com/questions/147392/what-is-advisory-locking-on-files-that-unix-systems-typically-employs) so you can do whatever you want with that file. PHP works with that file in memory so you can overwrite it during it's execution.
But if you will run the script multiple times while the first one is in progress it might load incomplete version and then it will throw some error so it might be wise to make sure that won't happen (using locks) or try to do some more atomic approach.
Windows locks files so I assume you won't be able to extract files the same way there.

PHP - Parse file to executable and get returned file

I was curious on how I would parse a file in PHP as an executable argument and then get the returned file.
I need to do this, but I'm sure sure whether I use system or exec.
User Uploads File
>
File is sanitized?
>
File is parsed to an executable with arguments
>
File is returned from the executable
>
User downloads file
Any help would be appreciated.
Edit:
Sorry that I wasn't clear enough. What is happening is the user uploads any file, and then this file is parsed to another executable with some arguments on what the executable is then to do with the file. Once it's done that, it returns a file which PHP then needs to be able to retrieve so the user can download it.
If I start a batch file that parses String arguments to the file, it only returns what's within the batch file, not what the program is returning - so I know that it is communicating with the file correctly.
Please elaborate on what you want to achieve.
You want to pass a valid PHP file as an argument, execute the PHP and give back a resulting html page?
Is that what you are saying? From where would the "argument" file come? Is it on the server? If so, it can be executed. If any file can be uploaded for execution, you would still need to store it on the server prior to execution - how would you want to make this secure so that it can't be misused? It would be an open door for any nasty script.

How to create a shortcut to run PHP code in Windows?

I wrote some PHP to generate a single PDF file with multiple pages based on input from a Comma Seperated Values (.csv) file.
Someone who is not familiar with programming will be running the PHP very often to generate the PDF file. That person will be responsible for changing the CSV file before generating it.
I need to know how I can provide him/her a shortcut in Windows so that when he/she click it, the PHP should execute and produce the PDF file in a hardcoded path.
You can create an shortcut on file itself, or executive php bin. Check Command Line PHP on Microsoft Windows.
Just create shortcut with C:\PHP5\php.exe -f "C:\PHP Scripts\script.php" -- -arg1 -arg2 -arg3, but it will be depended on locations. So you could create association with php files and just create shortcut to your script.
Or you could launch web-server (php web-server) and put link to your script to favorites in browser.
Just create a shortcut to the page, e.g.
http://www.example.com/mypdf.php
You can even open the link with a specific browser by typing one of these as the shortcut location:
chrome http://www.example.com/mypdf.php
firefox http://www.example.com/mypdf.php
iexplore http://www.example.com/mypdf.php
Alternatively, if you're indeed running PHP as CLI-only, see #sectus' answer.

Extracting self-extracting exe archive via php

I have a problem. I have the service which is giving me .exe file which they claim is in fact zip archive. A self-extracting archive.
Problem is that I am downloading that with my app (php) to server and need to extract it there witout downloading to local computer.
I have tried download .exe file to local computer - it is self extracting on windows to /temp dir and than self launching FLASH player.
$zip = zip_open($myfile); gives in print_r($zip): 1
zip->open gives no results either.
change .exe to .zip doesn't let winzip or other kind of un-packer on windows to open it - .exe cannot be opened by winzip too.
Now I have no idea how to deal with it. If anybody can advise please.
Try to execute the program as an executable with the system command
Executing files from an external source you don't trust 100% is never a good idea.
The info-zip version of zip allows you to remove the SFX stub from a self-extracting zip file (with the -J flag) converting it back into a normal zip file.
Source code is freely available.
Making a self-extracting zip file is a matter of prepending a zip file with the SFX binary code, then appending the size of the binary stub to the resulting file - but I'm not sure how the data is represented - but a bit of reverse-engineering the available code should make this clear.
Well... if your PHP server is Windows you shouldn't have a problem doing it as a system command. Otherwise, it's a little more tricky. I hear that the unzip system command will unzip self-extracting zip files, but I don't have access to a Linux box at the moment to try it out.
If you're on shared hosting, chances are you can't do it.
Well if you think after executing the exe file, it will extract its content, then you can use exec function to run the .exe files like the one below:
exec("d:\\example\\php\_exe\\1436.exe");
and also you can use system function to run external programs as well.
And also if you wonder what's the difference:
PHP - exec() vs system() vs passthru()

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

Categories