Social Icons

Wednesday, June 19, 2013

PHP file upload

In a website, it is a common requirement to upload a file or photo (image). Here, I am providing a solution as how can do it in PHP. It is not advisable to store the file in database directly. Instead, you should store the file in your project folder and in your database, you should store only the path of the file. So, we'll follow the same approach only.

First we design a form containing a file field. HTML code for the form containing a file field is :

<html>
<head>
<title>File Upload Form</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
File: <input type="file" name="myfile" id="myfile"/>
<br/>
<input type="submit" name="submit" id="submit" value="Upload File"/>
</form>
</body>
</html>

Above is just the html code for displaying a file field in the web page. Now, on clicking  of submit button, it will redirect you to upload.php as it is defined in form action. So, we'll write the code for upload.php now.

upload.php

<?php

if ( $_FILES['myfile']['error'] > 0)
{
  echo "Error: ".$_FILES['myfile']['error'];
}
else
{
  if (file_exists("upload/" . $_FILES["myfile"]["name"]))          //upload/file is the path where file                                                                                                                 gets stored
  {
      echo $_FILES["myfile"]["name"] . " already exists. ";
  }
  else
  {
      move_uploaded_file($_FILES["myfile"]["tmp_name"],
      "upload/" . $_FILES["myfile"]["name"]);
      echo "File Stored in: " . "upload/" . $_FILES["myfile"]["name"];
   }}
?>


Thats it. Now, your file upload script is ready. In the above code, "upload/" . $_FILES["myfile"]["name"] is the path where file gets uploaded or stored.  Make sure, you have 'upload' directory in your project folder path. Now as your file gets stored, if you want to save its path in database, then you can run a sql query in your upload.php script as its path after uploading the file. In that case, anytime you can echo the file with the help of the path.

Thursday, June 6, 2013

Find 2nd highest or nth highest salary of employee in sql

It is one of the common questions for developers and also asked in interviews frequently to find 2nd highest salary or nth highest salary of an employee from the employee table. Here, I am providing solution for both.

To find 2nd highest (or maximum) salary of employee, you can write your query like this in MySql:

Select max(salary) as salary 
From employees 
Where salary<(Select max(salary) From employees);

The above sql query will give you the 2nd highest salary of employee. Here, 'employees' is the name of the table in the database.

But, what if you have to find 3rd highest or 4th highest or nth highest salary of employee. If you will follow the above approach then you will have to write the subquery for n times. That doesn't seem good. Here, I am providing a generalized solution to find nth highest salary.  Look at the below sql query:


SELECT DISTINCT(salary) 
FROM employees 
ORDER BY salary DESC LIMIT (n-1) , 1


Here, n is the highest term for which you have to find out highest salary. The above query will give you nth highest salary.

Tuesday, June 4, 2013

Difference between single quote and double quote in PHP

In PHP, if yu have to display or echo something (say a string), then you can do it with the help of single quote and double quote both. But there is a little difference between them as :

A single quoted string doesn't have variables within it interpreted but a double quoted string does. It can be better understood with the following example:

<?php
$a = 'prem';
echo 'My name is $a';
echo "My name is $a";
?>

For the first echo statement having single quote, output will be
My name is $a

For the second echo statement having double quoted string, output will be
My name is prem

So, if you have to define a constant string then better use with single quote and if you have variable too in the string then go with double quotes.

Thursday, May 30, 2013

Installing and Setup Yii framework - PHP

Yii framework is one of the popular PHP MVC framework these days. Here, I am providing a guide to install yii framework. Just make sure your web server is capable of supporting PHP 5.1 orr higher. I assume that you have already installed web server and PHP or softwares like wamp, xampp or lamp. So, here we go.

First, download the yii framework from http://www.yiiframework.com/download/ . Unpack the downloaded file to a web accessible folder. For example, if you have xampp installed then place it inside xampp/htdocs/ , if wamp then you can place it in wamp/www/ . Now, you can check if your server satisfies all requirements for using yii and the installation was a success. I assume you have installed xampp server and put the downloaded yii folder inside xampp/htdocs/ . Now, open the url in your browser as http://localhost/yii/requirements/index.php You will see the results there. Few fields may show warnings like for PDOSQLite extension or memcache extension. But that is ok. You can proceed. Now, lets talk about creating a new application in yii.

Creating a new application in yii

We will use a command line tool 'yiic' that comes packaged with he yii framework. It is not mandatory to use this toll but it saves time and helps in creating a new yii application easily. Now, open your command terminal and change to your webroot folder i.e, till xampp/htdocs.

cd xampp/htdocs

If you have installed wamp then you have to reach to wamp/www. Here, I assumed you have xampp installed.

Now, as you put yii downloaded folder inside htdocs as xampp/htdocs/yii, run the command like this...

yii/framework/yiic webapp demo

Here, 'demo' is your project folder name.

Note: If you get the error here as : php.exe is not recognized as an internal or external command then look here for the solution: http://awesomesirjee.blogspot.in/2013/05/phpexe-is-not-recognized-as-internal-or.html

On running the above command, it will give you message as :

Create a Web application under '/xampp/htdocs/demo'? [Yes|No]
Yes

It will create you application successfully. You can check a new folder named 'demo' is there in xampp/htdocs/ . You see how easily you created a new brand yii application with just one command. Now, got your browser and type in url: http://localhost/demo/index.php

You will find your new application running with a welcome page. Thats it.

php.exe is not recognized as an internal or external command - yiic - yii framework

This is a common error that you can come across while creating a new yii application using 'yiic' command tool. If you get this error, follow the below steps and your error will be fixed.

You just need to add complete path of your php.exe in yii/framework/yiic.bat
yiic.bat is the MS-DOS batch file.
For example in my case, it will be

if "%PHP_COMMAND%" == "" set PHP_COMMAND=C:\xampp\php\php.exe

This solution will work only for yii.

If you want to make it for all i.e make it possible to run php.exe in any command line instance, then you will have to add path of php.exe in your PATH environment variables.

Monday, May 27, 2013

PHP extension "curl" must be loaded - Magento

While installing magento, you can get this error on configuration part.

But you can resolve it easily. Magento installation wizard now checks for curl on setup. Just make sure that curl extension is enabled in your php.ini

You can uncomment the line referring to php_curl.dll and restart your web server. It should fix the error

Friday, May 24, 2013

Include all PHP files from a directory


Suppose you have a directory (or say folder) consisting of some PHP files and you want to include all of them in your code. You need not to write include for each and every file. Then, you can do it in one line as follows:

<?php
foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}
?>

Total Pageviews