PHP File Upload

PHP allows users to upload images on server as shown below.

<?php
if(isset($_REQUEST['upload'])) {
$filename    =    $_FILES['filename']['tmp_name'];
if (file_exists($_SERVER['DOCUMENT_ROOT'].$_FILES["filename"]["name"]))
{
echo $_FILES["filename"]["name"] . " Already Exists. ";
}
else {
$path    =    $_SERVER['DOCUMENT_ROOT'].$_FILES['filename']['name'];
move_uploaded_file($filename,$path);
}
}
?>

<strong>Create HTML form shown as below.</strong>
<form enctype="multipart/form-data" method="post" action="">
<input type=file size=6 name=filename id=filename>
<input type="submit" name="upload" value="upload" />
</form>

Find duplicate records in mysql

When you need to fetch duplicate records from mysql database , run below query in your phpmyadmin interface.


SELECT count('fieldname') AS c FROM 'tablename' GROUP BY 'fieldname1','fieldname1'
HAVING c>1

Create an array : Javascript

You can create your own JavaScript array as shown below.


<script type="text/javascript">

var i;
 var languageArray = new Array();
 languageArray[0]    =    "English";
 languageArray[1]    =    "Gujrati";
 languageArray[2]    =    "Hindi";
 languageArray[3]    =    "Punjabi";

for(i=0; i<languageArray.length; i++) {
 document.write(languageArray[i] + "<br>");
 }
 </script>

Output : -

English
Gujrati
Hindi
Punjabi

Create Your Own Objects : JavaScript

You can create your own JavaScript object given by below.


<script type="text/javascript">
 objPerson    =    new Object();
 objPerson.fname    =    "Milap";
 objPerson.lname    =    "Patel";
 objPerson.age    =    "24";
 document.write(objPerson.age);
 </script>

objectname = new Object();
 will create JavaScript object named "objectname".

fname and lname are methods and we can assign values to object,
 objPerson.age    =    "24";

htaccess 301 redirect

When you need to redirect your page based on query string from URL, you can put below code in your .htaccess file !


RewriteEngine on
 RewriteCond %{REQUEST_URI}  ^/array\.php$
 RewriteCond %{QUERY_STRING} ^id=([0-9]*)&name=([a-z]*)$
 RewriteRule ^(.*)$ http://mywebsite.com/index.php? [R=302,L]

REQUEST_URI will return name of the executed file.
QUERY_STRING will return query string variables in URL.

After you add above code in htaccess , all array.php pages with query string id & name will redirect to http://mywebsite.com/index.php.

Resetting Magento Admin Password

There are many cases when you forget magento admin password .

For security reasons magento stores password inform of MD5 format in database , so you cant get password directly through phpmyadmin interface.

You can reset password using 2 methods :-

1) First find under which user you want to change password using below query:-


"SELECT * FROM `admin_user`;"

Now, to reset password run below query,


UPDATE `admin_user` SET `password`= MD5("newpassword") WHERE `username`='admin';

2) Go to phpmyadmin.Browse admin_user table . Edit specific record you want to update . While you change password in `password` field, select MD5 from dropdown under Function section & enter your password , it will store your password in MD5 format.

See below Screenshot for better idea :-

Resetting Admin Password

You are Done !!

Now try to login with new password !!

Setting up your favicon.ico

Sometimes it looks simple to Setup favicon icon for your website, but it is also quite useful when we start to set favicon as i do.

First , upload favicon.ico file in your root directory (most of times in public_html).

Just put below tag in your index file OR whichever common file for your project.


<link rel=”FaviconIcon” href=”favicon.ico” type=”image/x-icon” />

You are done..
This will display favicon icon in the starting of your Address Bar as well as in your current browser tab.
Although new browsers support GIF and PNG images , but , I would suggest always saving the image as favicon.ico.

PHP array_walk() Function

array_walk is one of the powerful array function , which is working based on user defined function as an argument. Here is the small example below.


<?php
 function add_string($val,$key,$par) {
 echo $val." ".$par. " <br />";
 }
 echo "Hello, Good Morning <br />";
 $a    =    array("Milap","Jay","Parthiv","Sandip","Smit");
 array_walk($a,'add_string','Loves PHP');
 ?>

Output :-

Hello, Good Morning
Milap Loves PHP
Jay Loves PHP
Parthiv Loves PHP
Sandip Loves PHP
Smit Loves PHP

Small Explanation :-

add_string function is passed as an argument in array_walk function, it means, the logic of add_string will apply to each element of array. 3rd parameter is optional.

How to create an image rotator with jquery and css

Whenever you need to display random images (more than 1 image) ,
you can do it using simple jquery cycle plugin.

Here is the small explanation :-


<script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery.cycle.all.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
 $('#<em><strong>imagediv</strong></em>').cycle({
 fx:    'fade',
 speed:  2500
 });
 });
 </script>

<div id="<em><strong>imagediv</strong></em>">
 <img src="image1.jpg"  alt="image1" />
 <img src="image2.jpg" alt="image2" />
 <img src="image3.jpg alt="image3" />
 <img src="image4.jpg  alt="image4" />
 <img src="image5.jpg alt="image5" />
 </div>

  1. Download jquery.js from http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
  2. Download jquery.cycle.all.js from http://malsup.github.com/jquery.cycle.all.js
  3. Include both .js files in header .
  4. add following CSS
    .pics {
    height: 117px;
    margin: 0;
    overflow: hidden;
    padding: 0;
    width: 354px;
    }
    

There are many options to set time for fading effect & all..

You can refer http://jquery.malsup.com/cycle for more option & customization jQuery cycle plugin.

Thats it ..

Done.. Enjoy !!!

List of deprecated functions php

The following is a list of deprecated functions in PHP 5.3.x.

Follow

Get every new post delivered to your Inbox.