Just another WordPress.com site

Archive for the ‘software engineer’ Category

Force a PDF to download

I recently needed to force a PDF to download using Apache. The default behaviour for most browsers is to try to open the PDF inside the browser itself. This is fine for a small PDF or for powerful machines – but a large PDF on even a modest machine can often lock the browser up. This needed fixing!

Little R&D of the Apache documents, you can get FilesMatch option which takes Regular Expressions. Initially I used something like this…

<files *.pdf=””></files><br />
<files *.pdf=””> ForceType application/pdf</files><br />
<files *.pdf=””> Header set Content-Disposition attachment</files><br />

This worked PERFECTLY – except some files had upper-case extensions and some had lower and I could see situations in the future where combinations of upper and lower case would be used too – just to piss me off! Because of this, not even this would work…

<filesmatch \.(pdf|pdf)=””></filesmatch>
<filesmatch \.(pdf|pdf)=””> ForceType application/pdf</filesmatch><filesmatch \.(pdf|pdf)=””> Header set Content-Disposition attachment</filesmatch>

That would match perfectly – as long as it was an EXACT match on upper OR lower case.
I was reaching the end of my patience – that is until I read the Using Character Classes on PerlDoc.
This showed me that I could force the RegEx (short for Regular Expressions) to match in a case-insensitive manner. This lead me to the following…

<filesmatch \.(?i:pdf)$=””></filesmatch>
<filesmatch \.(?i:pdf)$=””> ForceType application/pdf</filesmatch><filesmatch \.(?i:pdf)$=””> Header set Content-Disposition attachment</filesmatch>

However this only worked in proper browsers – and the bulk of the world are sadistic enough  to use Internet Explorer based ones. For some reason, if Internet Explorer see’s the content type “Application/PDF” it will simply open it up in the reader. The solution? Why not pretend its a bog standard Octet Stream, just like a Zip file? After all, that’s basically all it is; a binary file… A steam of bytes.

<FilesMatch “\.(?i:pdf)$”>
ForceType application/octetstream
Header set ContentDisposition attachment

And there you have it… A perfectly working modification to force all PDF files to download – this will work for any file extensions you chose to put into the FilesMatch argument!

Impotant Note :

You can put this code in either the htaccess or the vhost configuration for your server.
You can read more about FilesMatch at the Apache Document page.

linux commands for every web developer/Software engineer

scp – Linux command line tool to copy files over ssh

scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]

scp  abc.TXT root@

Is the name or IP of the host where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command
Is the user which have the right to access the file and directory that is supposed to be copied in the cas of the from-host and the user who has the rights to write in the to-host
Is the file or files that are going to be copied to the destination host, it can be a directory but in that case you need to specify the -r option to copy the contents of the directory
Is the name that the copied file is going to take in the to-host, if none is given all copied files are going to maintain its names

Copy Multiple fies

scp root@$dir/\{abc.txt,def.txt,ghi.txt\} root@

Restart MySql Ubuntu

To restart Apache2 on Ubuntu, we enter the following command in a terminal such as bash:

restart mysql

service mysql start

Restart Apache2 Ubuntu

To restart Apache2 on Ubuntu, we enter the following command in a terminal such as bash:

/etc/init.d/apache2 restart

Of course, you most likely will need to be the superuser to do that so you might need to enter the following command and then your password when requested:

sudo /etc/init.d/apache2 restart


use pointer to acees the variable within a variable directly like

 echo ${!name}
Result : bar

grep command: 

grep command usually interpet “|” as “AND” sign but you can use the same “|” for “OR” sign

for example

zgrep “^2012-06-05” /var/path/to/file/abc12346-12-06* | grep -E ‘3003300356|3003301763|3008203898|3008202542|3008226266|3003404159’

zgrep = grep for gzipped files

%d bloggers like this: