30 Jan

Upload Image to MySQL using PHP

Upload Image to MySQL using PHP

As a new-bie to php/mysql, I tried different stuffs. So here I’m with my php code for Image Upload to MySQL. Its a quite simple code with two php files one to display and one to upload.
For Image Upload code, I’ve added code download link upload-image-mysql-demo.zip at the end of the page.

As a new-bie to php/mysql, I tried different stuffs. So here I’m with my php code for Image Upload to MySQL. Its a quite simple code with two php files one to display and one to upload.

For Image Upload code, I’ve added code download link upload-image-mysql-demo.zip at the end of the page.

CREATE TABLE `pix` (

`pic_id` int(11) NOT NULL auto_increment,

`pic_name` varchar(100) NOT NULL,

`pic_data` longblob NOT NULL,

PRIMARY KEY  (`pic_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Get 2 php files:- Image.php, showImage.php.

Set Connection parameters accordingly.

/* Image.php*/

<?php

$con = mysql_connect(“127.0.0.1:3306″,”root”,””);

if (!$con)

{

die(“Could not connect: ” . mysql_error());

}

$DB = mysql_select_db(“test”, $con);

move_uploaded_file($_FILES[“uploadedfile”][“tmp_name”],”latest.img”);

$instr = fopen(“latest.img”,”rb”);

$image = addslashes(fread(fopen(“latest.img”,”r”),filesize(“latest.img”)));

mysql_query (“insert into pix (pic_name, pic_data) values (“myImage”, “‘.$image.'”);”);

?>

<html>

<form enctype=”multipart/form-data” method=”POST”>

<img src=showImage.php?gim=1 width=500 height=150 alt=”hell”>

<br><hr>

<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000″ />

Choose a file to upload(<100 KB): <input name=”uploadedfile” type=”file” /><br/>

<input type=”submit” value=”submit” name=”submit” />

</form>

<html>

/*showImage.php*/

<?php

$con = mysql_connect(“127.0.0.1:3306″,”root”,””);

if (!$con)

{

die(“Could not connect: ” . mysql_error());

}

$DB = mysql_select_db(“test”, $con);

$res = @mysql_query(“select * from pix order by pic_id desc limit 1″);

if ($row = @mysql_fetch_assoc($res))

{

$title = htmlspecialchars($row[pic_name]);

$bytes = $row[pic_data];

}

header(“Content-type: image/jpg”);

print $bytes;

mysql_close();

?>

Click here to download zip file for above demo of Uploading image to mysql using php.

11 Jan

Simple Shell Script to Monitoring Disk Space on a Linux Machine

This article explain a way to get a mail as soon as the disk usage reaches to its critical level to avoid issues later. To set a simple monitor on Linux / Unix, I have two simple scripts:

  • DSAlert.sh : Shell script for retrieving disk space percentages and put in to a cron job.
  • DiskSpace_Alert.pl : Perl script for sending an email.

Following is the disk monitor shell script which will execute the perl file for sending alert email.

#DSAlert.sh
#!/bin/sh
#Retrive disk space info
df=`df -Pl  | grep "^/dev" | awk '{print $5, $6}' | sed "s/%//"`

#Reference: df
echo “$df” | while read percent fs
do
#If Disk Usage increases above 90% send an email.
if [ $percent -ge 90 ] ; then
#Calling a perl file with parameters for sending an email.
`/opt/lampp/bin/perl DiskSpace_Alert.pl $fs is $percent percent full`
fi
done

Perl Script for sending an email:

# DiskSpace_Alert.pl
#!perl
use MIME::Lite;

#Reference : Mime-Lite

# set up email
$to = “my-mail-id\@domain.com”;
$from = “Diskmonitor\@ServerName.com”;
$subject = “Disk_Alert”;
$message = “Disk Space issue.\nActions Required:\n”.”@ARGV”;
# send email
email($to, $from, $subject, $message, $file);

# email function
sub email
{
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;
# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message);
# send the email

MIME::Lite->send(‘smtp’, ‘localhost’, Timeout => 60);
$msg->send();
}
Setting up a crontab for monitoring:

Crontab –e
* * * * * sh  full/path/to/DSAlert.sh

And we’re done with the Linux/Unix Disk Monitoring alerts.

Instructions for setting up timing for crontab:

+----------------> minute (0 - 59)
|  +-------------> hour (0 - 23)
|  |  +----------> day of month (1 - 31)
|  |  |  +-------> month (1 - 12)
|  |  |  |  +----> day of week (0 - 6) (Sunday=0 or 7)
|  |  |  |  |
*  *  *  *  *

10 Jan

Linux Shell Commands – quick how-tos

Following are linux commands, tricks for doing regular tasks.

Rename files: replace space by _ in linux:

for files in *; do mv "$files" `echo $files | tr ' ' '_'`; done

Change case of multiple file names:

for files in *; do mv $files `echo $files | tr '[:upper:]' '[:lower:]'`; done

Find files having large size:

ls -hlRtr | awk '{print $5 " " $6 "-" $7 " "$9}' | grep '[0-9]G ' 

– Variants: grep ‘[0-9][0-9][0-9]M’ will give you files having sizes xxxMBs.

Find and replace text from multiple files :

find ./ -type f | xargs perl -pi -w -e 's/SEARCH-TERM/REPLACE-TERM/g;'

Find text in file:

find . -name "*.php" | xargs grep " order by l.ID "
find . -name "*.pl" | xargs grep "virtualTourUrl"
grep *.* "Pattern"

Find and Delete file within last one hour:

find /some/dir -type f -mmin +60 | xargs rm -f
find . -cmin +60 -exec rm -f {} \;

Find files last modified / accessed – copy to other directory:

find . -mtime +1 -exec cp {} TARGET_FOLDER \;

– atime..last access
– mtime..last modify
– ctime..last status change

Replace content of a file:

perl -pi -e "s/kedar/radek/g" a.txt
perl -pi -e "s/OLDTEXT=OLDVALUE/NewText=NewValue/g" *.*

Find a.txt and Replace content by string comparison and save in to other file:

find ./ -name "a.txt" -exec sed 's/radek/kedar/g' {} \; > b.txt

Find files modified in last n days:

find *.sql -mtime +n -exec ls -l --sort=time {} \;

Create and Extract a Tar file:

tar -cvf /path/to/file.tar /path/to/source/ archived
tar -xvf /path/to/file.tar

A quick sftp – secure file transfer program to transfer files between linux/unix machines:

sftp usename@hostname/ipaddress
password: ****
sftp> ls #executed on remote machine
sftp> lls #executed on local machine
sftp> get *.* #download all files from remote machine to local machine
sftp> put *.* #upload files to remote machine

- Create directory in sftp: mkdir [For local: lmkdir]
- Change directory in sftp: cd [For local: lcd]
- get / mget : download files to local linux machine
- put / mput : upload files to remote linux machie
04 Jan

Difference MyISAM and InnoDB Storage Engines Mysql

MyISAM

InnoDB

Default since version 3.23

Relatively newer

Files storage:

FRM: table definition,

MYD: Actual Data

MYI: Index File

FRM: table definition

.ibd file (concept of table space) or single huge .ibdataX

No transation, Foreign key

ACID Transactions, Foreign key, Rollback

Low disk and memory utilisation

Relatively high storage requirements (Almost 3 times disk space)

Non clustered indexing

Clustered indexing for Primary key

Table level locking

Row level locking

Fulltext Indexes, Merge tables, Compressed tables.

Not available

GIS, RTREE indexes

Not available

Not avalable

HASH lookups

04 Jan

Selected Oscar Wilde Quotes

Oscar Fingal O’Flahertie Wills Wilde!! Oscar Wilde is well known for his biting wit. And he has an extraordinary way of representing the things.

Here are some collection of famous quotes of Oscar Wilde:

  1. A cynic is a man who knows the price of everything but the value of nothing.
  2. A dreamer is one who can only find his way by moonlight, and his punishment is that he sees the dawn before the rest of the world.
  3. A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal.
  4. A man can be happy with any woman, as long as he does not love her.
  5. A man who does not think for himself does not think at all.
  6. A thing is not necessarily true because a man dies for it.
  7. A true friend stabs you in the front.
  8. All bad poetry springs from genuine feeling. To be natural is to be obvious, and to be obvious is to be inartistic.
  9. All women become like their mothers. That is their tragedy. No man does. That’s his.
  10. Always forgive your enemies – nothing annoys them so much.
  11. America is the only country that went from barbarism to decadence without civilization in between.
  12. An excellent man; he has no enemies; and none of his friends like him.
  13. An idea that is not dangerous is unworthy of being called an idea at all.
  14. Anybody can be good in the country. There are no temptations there.
  15. Arguments are to be avoided: they are always vulgar and often convincing.
  16. Between men and women there is no friendship possible. There is passion, enmity, worship, love, but no friendship.
  17. Charity creates a multitude of sins.
  18. Children begin by loving their parents; after a time they judge them; rarely, if ever, do they forgive them.
  19. Consistency is the last refuge of the unimaginative.
  20. Democracy means simply the bludgeoning of the people by the people for the people.
  21. Do you really think it is weakness that yields to temptation? I tell you that there are terrible temptations which it requires strength, strength and courage to yield to.
  22. Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught.
  23. Every saint has a past and every sinner has a future.
  24. Everybody who is incapable of learning has taken to teaching.
  25. Experience is simply the name we give our mistakes.
  26. He has no enemies, but is intensely disliked by his friends.
  27. He lives the poetry that he cannot write. The others write the poetry that they dare not realise.
  28. He must have a truly romantic nature, for he weeps when there is nothing at all to weep about.
  29. He was always late on principle, his principle being that punctuality is the thief of time.
  30. How marriage ruins a man! It is as demoralizing as cigarettes, and far more expensive.
  31. I always pass on good advice. It is the only thing to do with it. It is never of any use to oneself.
  32. I am not young enough to know everything.
  33. I am so clever that sometimes I don’t understand a single word of what I am saying.
  34. I am the only person in the world I should like to know thoroughly.
  35. I can resist everything except temptation.
  36. I choose my friends for their good looks, my acquaintances for their good characters, and my enemies for their intellects. A man cannot be too careful in the choice of his enemies.
  37. I have nothing to declare except my genuis.
  38. I have the simplest tastes. I am always satisfied with the best.
  39. I like persons better than principles, and I like persons with no principles better than anything else in the world.
  40. I never travel without my diary. One should always have something sensational to read in the train.
  41. I put all my genius into my life; I put only my talent into my works.
  42. If there was less sympathy in the world, there would be less trouble in the world.
  43. If you pretend to be good, the world takes you very seriously. If you pretend to be bad, it doesn’t. Such is the astounding stupidity of optimism.
  44. [ad#ad-2-300×250]

  45. Illusion is the first of all pleasures.
  46. In America the young are always ready to give to those who are older than themselves the full benefits of their inexperience.
  47. It is a very sad thing that nowadays there is so little useless information.
  48. It is absurd to divide people into good and bad. People are either charming or tedious.
  49. It is better to be beautiful than to be good. But… it is better to be good than to be ugly.
  50. It is only by not paying one’s bills that one can hope to live in the memory of the commercial classes.
  51. It is only the modern that ever becomes old-fashioned.
  52. It is perfectly monstrous the way people go about, nowadays, saying things against one behind one’s back that are absolutely and entirely true.
  53. It is what you read when you don’t have to that determines what you will be when you can’t help it.
  54. Laughter is not at all a bad beginning for a friendship, and it is far the best ending for one.
  55. Life is far too important a thing ever to talk seriously about.
  56. Life is never fair, and perhaps it is a good thing for most of us that it is not.
  57. Life is too important to be taken seriously.
  58. Man is least himself when he talks in his own person. Give him a mask, and he will tell you the truth.
  59. Men always want to be a woman’s first love – women like to be a man’s last romance.
  60. Men marry because they are tired; women, because they are curious; both are disappointed.
  61. Moderation is a fatal thing. Nothing succeeds like excess.
  62. Morality is simply the attitude we adopt towards people whom we personally dislike.
  63. Most people are other people. Their thoughts are someone else’s opinions, their lives a mimicry, their passions a quotation.
  64. Most modern calendars mar the sweet simplicity of our lives by reminding us that each day that passes is the anniversary of some perfectly uninteresting event.
  65. No object is so beautiful that, under certain conditions, it will not look ugly.
  66. Of course I have played outdoor games. I once played dominoes in an open air cafe in Paris.
  67. One of the many lessons that one learns in prison is, that things are what they are and will be what they will be.
  68. One should always be in love. That is the reason one should never marry.
  69. One should always play fairly when one has the winning cards.
  70. One’s real life is so often the life that one does not lead.
  71. Only the shallow know themselves.
  72. Pessimist: One who, when he has the choice of two evils, chooses both.
  73. Please do not shoot the pianist. He is doing his best.
  74. Questions are never indiscreet, answers sometimes are.
  75. Quotation is a serviceable substitute for wit.
  76. Ridicule is the tribute paid to the genius by the mediocrities.
  77. Romance should never begin with sentiment. It should begin with science and end with a settlement.
  78. She wore far too much rouge last night and not quite enough clothes. That is always a sign of despair in a woman.
  79. Society exists only as a mental concept; in the real world there are only individuals.
  80. Some cause happiness wherever they go; others whenever they go.
  81. Success is a science; if you have the conditions, you get the result.
  82. The advantage of the emotions is that they lead us astray.
  83. The basis of optimism is sheer terror.
  84. The books that the world calls immoral are books that show the world its own shame.
  85. The difference between literature and journalism is that journalism is unreadable and literature is not read.
  86. The good ended happily, and the bad unhappily. That is what fiction means.
  87. The old believe everything, the middle-aged suspect everything, the young know everything.
  88. The one charm about marriage is that it makes a life of deception absolutely necessary for both parties.
  89. The only thing to do with good advice is to pass it on. It is never of any use to oneself.
  90. The only way to get rid of temptation is to yield to it… I can resist everything but temptation.
  91. The public is wonderfully tolerant. It forgives everything except genius.
  92. The pure and simple truth is rarely pure and never simple.
  93. The true mystery of the world is the visible, not the invisible.
  94. The world is a stage, but the play is badly cast.
  95. There are many things that we would throw away if we were not afraid that others might pick them up.
  96. The world is divided into two classes, those who believe the incredible, and those who do the improbable.
  97. There are only two kinds of people who are really fascinating – people who know absolutely everything, and people who know absolutely nothing.
  98. There are only two tragedies in life: one is not getting what one wants, and the other is getting it.
  99. There is always something infinitely mean about other people’s tragedies.
  100. Women love us for our defects. If we have enough of them, they will forgive us everything, even our gigantic intellects.
  101. Women are made to be loved, not understood.
  102. Woman begins by resisting a man’s advances and ends by blocking his retreat.
  103. Whenever people agree with me I always feel I must be wrong.
  104. When the gods wish to punish us they answer our prayers.
  105. When I was young I thought that money was the most important thing in life; now that I am old I know that it is.
  106. When a man has once loved a woman he will do anything for her except continue to love her.
  107. There is always something ridiculous about the emotions of people whom one has ceased to love.
  108. We are all in the gutter, but some of us are looking at the stars.
  109. True friends stab you in the front.
  110. To love oneself is the beginning of a lifelong romance.
  111. There is no such thing as a moral or an immoral book. Books are well written, or badly written.
  112. Those whom the gods love grow young.
  113. There is nothing in the world like the devotion of a married woman. It is a thing no married man knows anything about.
  114. There is only one class in the community that thinks more about money than the rich, and that is the poor. The poor can think of nothing else.
  115. There is something terribly morbid in the modern sympathy with pain. One should sympathise with the colour, the beauty, the joy of life. The less said about life’s sores the better.
  116. There is only one thing in life worse than being talked about, and that is not being talked about.
  117. Selected Oscar Wilde Quotes
  118. Oscar Fingal O’Flahertie Wills Wilde!! Oscar Wilde is well known for his biting wit. And he has an extraordinary way of representing the things.
  119. Here are some collection of famous quotes of Oscar Wilde:
  120. A cynic is a man who knows the price of everything but the value of nothing.
  121. A dreamer is one who can only find his way by moonlight, and his punishment is that he sees the dawn before the rest of the world.
  122. A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal.
  123. A man can be happy with any woman, as long as he does not love her.
  124. A man who does not think for himself does not think at all.
  125. A thing is not necessarily true because a man dies for it.
  126. A true friend stabs you in the front.
  127. All bad poetry springs from genuine feeling. To be natural is to be obvious, and to be obvious is to be inartistic.
  128. All women become like their mothers. That is their tragedy. No man does. That’s his.
  129. Always forgive your enemies – nothing annoys them so much.
  130. America is the only country that went from barbarism to decadence without civilization in between.
  131. An excellent man; he has no enemies; and none of his friends like him.
  132. An idea that is not dangerous is unworthy of being called an idea at all.
  133. Anybody can be good in the country. There are no temptations there.
  134. Arguments are to be avoided: they are always vulgar and often convincing.
  135. Between men and women there is no friendship possible. There is passion, enmity, worship, love, but no friendship.
  136. Charity creates a multitude of sins.
  137. Children begin by loving their parents; after a time they judge them; rarely, if ever, do they forgive them.
  138. Consistency is the last refuge of the unimaginative.
  139. Democracy means simply the bludgeoning of the people by the people for the people.
  140. Do you really think it is weakness that yields to temptation? I tell you that there are terrible temptations which it requires strength, strength and courage to yield to.
  141. Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught.
  142. Every saint has a past and every sinner has a future.
  143. Everybody who is incapable of learning has taken to teaching.
  144. Experience is simply the name we give our mistakes.
  145. He has no enemies, but is intensely disliked by his friends.
  146. He lives the poetry that he cannot write. The others write the poetry that they dare not realise.
  147. He must have a truly romantic nature, for he weeps when there is nothing at all to weep about.
  148. He was always late on principle, his principle being that punctuality is the thief of time.
  149. How marriage ruins a man! It is as demoralizing as cigarettes, and far more expensive.
  150. I always pass on good advice. It is the only thing to do with it. It is never of any use to oneself.
  151. I am not young enough to know everything.
  152. I am so clever that sometimes I don’t understand a single word of what I am saying.
  153. I am the only person in the world I should like to know thoroughly.
  154. I can resist everything except temptation.
  155. I choose my friends for their good looks, my acquaintances for their good characters, and my enemies for their intellects. A man cannot be too careful in the choice of his enemies.
  156. I have nothing to declare except my genuis.
  157. I have the simplest tastes. I am always satisfied with the best.
  158. I like persons better than principles, and I like persons with no principles better than anything else in the world.
  159. I never travel without my diary. One should always have something sensational to read in the train.
  160. I put all my genius into my life; I put only my talent into my works.
  161. If there was less sympathy in the world, there would be less trouble in the world.
  162. If you pretend to be good, the world takes you very seriously. If you pretend to be bad, it doesn’t. Such is the astounding stupidity of optimism.
  163. Illusion is the first of all pleasures.
  164. In America the young are always ready to give to those who are older than themselves the full benefits of their inexperience.
  165. It is a very sad thing that nowadays there is so little useless information.
  166. It is absurd to divide people into good and bad. People are either charming or tedious.
  167. It is better to be beautiful than to be good. But… it is better to be good than to be ugly.
  168. It is only by not paying one’s bills that one can hope to live in the memory of the commercial classes.
  169. It is only the modern that ever becomes old-fashioned.
  170. It is perfectly monstrous the way people go about, nowadays, saying things against one behind one’s back that are absolutely and entirely true.
  171. It is what you read when you don’t have to that determines what you will be when you can’t help it.
  172. Laughter is not at all a bad beginning for a friendship, and it is far the best ending for one.
  173. Life is far too important a thing ever to talk seriously about.
  174. Life is never fair, and perhaps it is a good thing for most of us that it is not.
  175. Life is too important to be taken seriously.
  176. Man is least himself when he talks in his own person. Give him a mask, and he will tell you the truth.
  177. Men always want to be a woman’s first love – women like to be a man’s last romance.
  178. Men marry because they are tired; women, because they are curious; both are disappointed.
  179. Moderation is a fatal thing. Nothing succeeds like excess.
  180. Morality is simply the attitude we adopt towards people whom we personally dislike.
  181. Most people are other people. Their thoughts are someone else’s opinions, their lives a mimicry, their passions a quotation.
  182. Most modern calendars mar the sweet simplicity of our lives by reminding us that each day that passes is the anniversary of some perfectly uninteresting event.
  183. No object is so beautiful that, under certain conditions, it will not look ugly.
  184. Of course I have played outdoor games. I once played dominoes in an open air cafe in Paris.
  185. One of the many lessons that one learns in prison is, that things are what they are and will be what they will be.
  186. One should always be in love. That is the reason one should never marry.
  187. One should always play fairly when one has the winning cards.
  188. One’s real life is so often the life that one does not lead.
  189. Only the shallow know themselves.
  190. Pessimist: One who, when he has the choice of two evils, chooses both.
  191. Please do not shoot the pianist. He is doing his best.
  192. Questions are never indiscreet, answers sometimes are.
  193. Quotation is a serviceable substitute for wit.
  194. Ridicule is the tribute paid to the genius by the mediocrities.
  195. Romance should never begin with sentiment. It should begin with science and end with a settlement.
  196. She wore far too much rouge last night and not quite enough clothes. That is always a sign of despair in a woman.
  197. Society exists only as a mental concept; in the real world there are only individuals.
  198. Some cause happiness wherever they go; others whenever they go.
  199. Success is a science; if you have the conditions, you get the result.
  200. The advantage of the emotions is that they lead us astray.
  201. The basis of optimism is sheer terror.
  202. The books that the world calls immoral are books that show the world its own shame.
  203. The difference between literature and journalism is that journalism is unreadable and literature is not read.
  204. The good ended happily, and the bad unhappily. That is what fiction means.
  205. The old believe everything, the middle-aged suspect everything, the young know everything.
  206. The one charm about marriage is that it makes a life of deception absolutely necessary for both parties.
  207. The only thing to do with good advice is to pass it on. It is never of any use to oneself.
  208. The only way to get rid of temptation is to yield to it… I can resist everything but temptation.
  209. The public is wonderfully tolerant. It forgives everything except genius.
  210. The pure and simple truth is rarely pure and never simple.
  211. The true mystery of the world is the visible, not the invisible.
  212. The world is a stage, but the play is badly cast.
  213. There are many things that we would throw away if we were not afraid that others might pick them up.
  214. The world is divided into two classes, those who believe the incredible, and those who do the improbable.
  215. There are only two kinds of people who are really fascinating – people who know absolutely everything, and people who know absolutely nothing.
  216. There are only two tragedies in life: one is not getting what one wants, and the other is getting it.
  217. There is always something infinitely mean about other people’s tragedies.
  218. Women love us for our defects. If we have enough of them, they will forgive us everything, even our gigantic intellects.
  219. Women are made to be loved, not understood.
  220. Woman begins by resisting a man’s advances and ends by blocking his retreat.
  221. Whenever people agree with me I always feel I must be wrong.
  222. When the gods wish to punish us they answer our prayers.
  223. When I was young I thought that money was the most important thing in life; now that I am old I know that it is.
  224. When a man has once loved a woman he will do anything for her except continue to love her.
  225. There is always something ridiculous about the emotions of people whom one has ceased to love.
  226. We are all in the gutter, but some of us are looking at the stars.
  227. True friends stab you in the front.
  228. To love oneself is the beginning of a lifelong romance.
  229. There is no such thing as a moral or an immoral book. Books are well written, or badly written.
  230. Those whom the gods love grow young.
  231. There is nothing in the world like the devotion of a married woman. It is a thing no married man knows anything about.
  232. There is only one class in the community that thinks more about money than the rich, and that is the poor. The poor can think of nothing else.
  233. There is something terribly morbid in the modern sympathy with pain. One should sympathise with the colour, the beauty, the joy of life. The less said about life’s sores the better.
  234. There is only one thing in life worse than being talked about, and that is not being talked about.
-- Kedar Vaijanapurkar --