Sunday 15 September 2013

Tuesday 12 March 2013

Batch file to execute MySQL scripts


@echo off
ECHO %USERNAME% started the File Execution Process at %TIME%  >status.txt

for %%f in (*.sql) do (
C:\wamp\bin\mysql\mysql5.5.20\bin\mysql -u root --delimiter=// < %%f >>status.txt
pause; 

)



Can someone extend this to let the files be executed in accordance of their modified time stamps

Monday 11 March 2013

Search and Replace Tools

For multi line search and replace within a set of files,

I use on windows:
Text Crawler
ToolBuckets Multi Line Find and Replace Feature in Notepad++

On Ubuntu I simply use sed alongwith grep

What do you use?
Any cool tricks to simplify multi line search and replace


Friday 1 March 2013

How to store a file and make it downloadable?

How to store a file and make it downloadable?


I often come across this question and new comers often find it difficult to understand. Let me put this thing in simple way.

What do we need to do this?

To make this possible, we need to do following simple things:
  1. Store file at some permanent location
  2. Store data about file
  3. Serve the file whenever user request for it.

If we simply move uploaded file to a particular location, we may end up overwriting a file which was previously uploaded and had same name. To cope with this issue, we may rename the file and try to generate a unique name to avoid collision. 

I usually follow following approach:


//! Generate a unique file name
 $uniqueFileName == md5(uniqid('',true));
//! Generate a upload path
$uploadfileTo =  UPLOAD_PATH. $uniqueFileName ;
//! Move to permanent location
move_uploaded_file($file['tmp_name'], $uploadfileTo);

uniqid() generates a random string. First argument is prefix for the random string. If we pass second argument as true, a string with more entropy will be generated , i.e. more random numbers. We are hashing the generated string using md5 algorithm. You can also use the hash of combination of timestamp and output of uniqid().

Now if we have store the file, we will need information of file in our database.  I would recommend storing at least this details about file:

  • File's original name
  • File's extension
  • File's Content-Type / MIME type
  • File Size
  • Storage location
  • File's generated name
Now when you've store this information in database, you can create a download page. This can be achieved in two ways.
  1. Redirect user to the URL of actual stored file.
  2. Create a proxy page which will serve the content of file.
Problem with first approach is that, actual storage URL is known and you have no control over who is accessing it.

I will recommend using second approach. To do so, we have to tell browser that the php page is actually an original file with particular content type. Browser identify the document based on the HTTP headers. So, we can achieve this by simply passing proper HTTP headers. PHP allows us to send HTTP headers using function header() .

On a download page, you can simply retrieve details of original files. Suppose we have those details in self explaining variables below, we can serve the file using following code snippet:

//! pass content type header for the file
header("Content-type: {$sContentType}");

//! force browser to download the content
header("Content-Disposition:attachment;filename='{$sFileName}'");

//! load the content of the file
readfile($filePath);

And that's it. Your file will be served to user with original name and content type. And user will actually never know the real location.

Common issue people face with this is, file is downloaded properly but when they open it, they realized that the file is corrupted. This happens because you are sending some other output to the browser also as payload along with content of file. You may have some code which is echoing something.  You can also use output buffering to stop sending anything to browser except the actual content. Use output buffering, before you readfile flush the output buffer and then read the file.

Tuesday 26 February 2013

Good curation sites?

Curation is big....helps save time and provides big buckets of content to use when required...

Some sites I use for curating -

spundge - random collections

scoop.it - health, startup and code articles

diigo -  links and textual content

Please add the sites you use to curate  and specify what you use them for... 

Friday 15 February 2013

Chromium Browser Automation(CBA)

CBA and most of the Automation browser identify the Ids of the Text box, button, Text area to process automation procedure.
Within Some of the Pages of software Same ids are given to multiple button example in LimsDx Management Page
because of which CBA identify first id within the page and plays the script but because of wrong button clicked the script is stopped

Is their a kind of technique where the script can be played with respect to Values of buttons ?


Thursday 14 February 2013

Notepad++ Plugins

Plugins are supported by NPP+ to allow one to extend the feature set...

Some useful ones used  by me are as follows:


JSON Viewer -> https://sourceforge.net/projects/nppjsonviewer/

Compare -> http://sourceforge.net/projects/npp-plugins/

XML Tools -> http://sourceforge.net/projects/npp-plugins/files/XML%20Tools/



Others can please update this post to add the plugins they have used and found useful...

Wednesday 13 February 2013

Some Nice Packages for Sublime Text 2

I have recently found a collection of nice Sublime Text 2 packages on Github.

There are several useful packages (plugins). To check, please go to : https://github.com/SublimeText

To install any plugins,


  1. Download the desired package
  2. Go to Preference -> Browse Packages in Sublime Text
  3. Put your downloaded package there
  4. Just restart the Sublime Text 2 to activate the package.

Monday 11 February 2013

How to deploy our git code to server automatically?

How to deploy our git code to server automatically?


My current project has two separate installation on our US Server and updating project each time to both installation is quite time consuming. So I search for a tool which can help us automatically deploy our files.

I found a pretty nice utility named git-ftp which can basically upload your git tracked file and upload affected files to FTP server(s). You can checkout it's git repository at Github: https://github.com/resmo/git-ftp

How to Install? 

Installing and integrating with out Git is easy. If you want to set it up for your repositories, follow instructions as below:

Open you Git bash and write following command:


$ cd ~
$ git clone https://github.com/resmo/git-ftp git-ftp.git
$ cd git-ftp.git && chmod +x git-ftp
$ cp ~/git-ftp.git/git-ftp /bin/git-ftp
This will setup git-ftp on your machine. Now using it is a piece of cake.

How to Use?

To use it with your existing repositories, go to your Git repository. For example,
$ cd c:/wamp/www/DocControl
To upload code to our server, you can use :

$ git ftp push
You will need to use required parameter. Just like git, you will need to initialize the git ftp. So for the first time when you are pushing changes to your ftp server, run following command:

$ git ftp init -u <user> -p - ftp://host.example.com/public_html
For our US server, command will look like below :

$ git ftp init -u <user> -p - ftp://118.139.xxx.yyy/Directory
It will ask password for FTP server. After putting it, it will upload all git files to specified location of remote server and initialize the required things. After that only affected files from last push to ftp server will be uploaded, hence it won't use bandwidth unnecessarily. Next time, you will only have to run :


$ git ftp push -u <user> -p - ftp://118.139.xxx.yyy/Directory


There are many more options available like ignore files, scopes, etc. Check them at https://github.com/resmo/git-ftp/blob/develop/README.md

If you don't want to repeat this command everytime, you can simple put it inside a shell file and run it whenever you want.  You can also add a scope, which is similar to remote in git.

Be careful with config files if you are working with multiple installation. You should ignore those files which differs for each installation, just like in my case, they are database configs.

Monday 21 January 2013

How to solve the "Fatal error: Cannot use string offset as an array"?

How to solve the "Fatal error: Cannot use string offset as an array"?

I create one function that provide the data by passing id to it.

I used this function in for loop and pass the ID's one by one.

From this function I got all data properly. But when I take this data into the variable in same for loop
I got "Fatal error: Cannot use string offset as an array".

To resolve this problem, I used another for loop.

But can I solve this problem by using only one for loop or any other option?


Wednesday 16 January 2013

After Upgrading phpmyadmin to newer version the client connection are not accepted
It displays

Forbidden

You don't have permission to access / on this server.

What can be done to resolve this?

Sunday 13 January 2013

How can we used date picker for every records(input box) that create when we clone.

How can we used date picker for every records(input box) that create when we clone.

For more explanation I give an example.

I have create one date picker that will create a calendar and through that we can select the date.
To use that date picker I have to assign it with text box id or class in ready function of jquery.

But when I cloned, It doesn't reflect the cloning text box.

How can I solve this problem?

Thursday 10 January 2013

What is the Best Approach to adopt OOP in PHP?

What is the Best Approach to adopt OOP in PHP?

What is the best practice for using OOP paradigm in PHP? To what extent, OOP paradigm should be forced? PHP supports OOP. But if we use close to a strict OOP paradigm, we end up compromising on performance and memory.

For example, we have classes named User and UserGroup. UserGroup will have properties like details about Group and contains its members. Best OOP approach is that members of group should be array of instance of User class. Each User has several properties. If we follow OOP approach, we will end up filling all the details of all users in that UserGroup. This will make a lot of queries to database degrading the performance and consume a lot more memory. In face we will not need the details of all users, but couple of them. Or only couple of properties at a time.

So what is the best practice to adapt OOP paradigm in PHP? To what extent, it should be forced?

Sunday 6 January 2013

How does one order results for a MySQL Query by how well they matched a given query.

How does one order results for a MySQL Query by how well they matched a given query.



Use the following ORDER rule


ORDER 
BY case when `ColumnName` LIKE 'FirstRelevance' then 1 else 0 end
+ case when `ColumnName` LIKE 'SecondRelevance' then 1 else 0 end
+ case when `ColumnName` LIKE 'ThirdRelevance' then 1 else 0 end
...
DESC