Category Archives: Coding & Programming

Solved: PHP move_uploaded_file and copy function both is not uploading the file in destination directory

Fixed: PHP move_uploaded_file() and copy() function is not uploading the file

In the programming field we usually need to upload the files in the folders.

In PHP, we have 2 functions for the purpose.

1st is move_uploaded_file()

2nd one is copy().

 

I think you know well, what the difference between these two copy() vs move_uploaded_file().

Well, if you not aware about the difference between the above 2 PHP functions that is used for file uploading.
Click to view the difference between PHP copy() and PHP move_uploaded_file() function.

 

But this is not my prupose for wrting this post. I want to aware you that sometimes the file uploaded error is something different.
First of all whenever you upload a file you just need to check weather you have used encryption type enctype=”multipart/form-data” and have to use POST method.

 

GET method can’t upload file in any case.
Cool, you have used that then first of all check weather the file has been passed by form and have uploaded to the temp directory.
Basically this is the functionality of file uploading, first during the form post action file uploaded to the temp directory and then we have to move or copy that file in our desired folder or directory.

 

How can you check the temp directory, in which file is going to upload, you can check the same using phpinfo().

upload-temp-directory

You can also change the same (default temp directory) that can be seen in the php.ini file.

If you uploaded a file, but file is not uploaded, first check whether the file ha been uploaded in temp directory or not.
If there has been some error occurred, you can check by print_r array of $_FILES variable.

 

$_FILES

If you get error that means files is not uploaded in temp directory.
It may have 2 reasons, whether the directory no exists or the directory hasn’t proper permission.

 

You can get rid by 2 solutions.

1) Change the path of temp directory in php.ini
2) Check the permission and folder existence.

 

I think, you will better fix the issue related to file upload.

Better Coding, Happy Coding!!

Save

PHP Magic Predefined Constants & usages

PHP Magic Predefined Constants & usages

In PHP, a constant is a name or an identifier for a simple value. A constant name starts with a letter or underscore can be followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.

 

You have to use define() function to put value in the constant and for retrieval the value simply specifying the name.
The constant() function can be used if you create the constant name dynamically and want to fetch the value of the constant.

 

[php]
<?php
define("SITENAME", ‘scriptarticle.com’);
echo SITENAME;
echo constant("SITENAME"); // returns the same as previous one
?>
[/php]

 

Along with the above PHP itself have a lot of predefined constants.

Some of the constants that are specials and can be called as magical are listed as below with example, these starts and ends with double underscore (__) and written in capital letters.

The predefined constants are very useful to access information about your code.

 

__LINE__

returns the line number in the code where the constant appears.

 

__DIR__

represents the path to the current file.

 

__CLASS__

returns current class name.

 

__FUNCTION__

returns current function name.

 

__METHOD__

represents the current method name.

 

__NAMESPACE__

returns the current namespace name.

 

__FILE__

represents the name of your file, includes its full path.

 

I have included all these in the below example, have a look.

[php]
<?php

// Set namespace (works only with PHP 5.3 and later)
namespace Scriptarticle;

// this echo current file’s full path and name
echo "This file full path and file name is ‘" . __FILE__;

// this echo file full path
echo "This file full path is ‘" . __DIR__;

// This echo current line number on file
echo "This is line number " . __LINE__;

function function_magic_constant() {
echo "This is from ‘" . __FUNCTION__ . "’ function.";
}

// echo function and used namespace
function_magic_constant();

class SAConstants {
// echo class name
public function printClassName() {
echo "This is " . __CLASS__ . " class";
}
// echo class and method name
public function printMethodName() {
echo "This is " . __METHOD__ . " method";
}
// echo function name
public function printFunction() {
echo "This is function ‘" . __FUNCTION__ . "’ inside class";
}
public function printNamespace() {
echo "Namespace name is ‘" . __NAMESPACE__ . "’";
}
}

// create a object of the class
$obj = new SAConstants;

$obj->printClassName();

$obj->printMethodName();

// this prints function name inside class and used namespace
// same as method name, but without class
$obj->printFunction();

// this prints namespace name
$obj->printNamespace();

?>
[/php]

 

If you want to view a complete list of PHP predefined constant then follow the below links.

PHP Config Predefined Constants
PHP Image Predefined Constants
PHP Date Predefined Constants
PHP Core Predefined Constants
PHP Magic constants

 

Hope you have enjoyed the post!!

 

A Quick view on PHP magic methods

Magic Methods in PHP

(PHP Magical Reserved Functions Starts with double underscore)

According to me you all have heard about the PHP Magic methods.
You have also used some of these like __autoload and __construct.

 

Let’s have a quick view on magic methods, as sometimes it looks like it will hard to use these function but reality is these are very simple.
First of all let me tell what magic method is and why these are called magical? Are these methods really showed some type of magic as the name imply.

 

PHP reserves all function names starting with two underscore prefix (__) as magical.

Magic methods provide hooks into special PHP behavior.

 

PHP does not provide the definitions of the magic functions and the programmers have to write/code that what these functions will do. Magic functions will never directly be called by the programmer but PHP will call the function ‘behind the scenes’. That’s why they are called ‘magic’ functions because these can not be directly called and they allow the programmer to do some powerful things by the coding. I think it’s enough; you will be clearer by examples.

 

__autoload()
This is not a magic method exactly but it is very useful. The __autoload() function is automatically called when a class is instantiated and file will get include in the code. This is useful since you don’t always want to load every class again and again and add just in case you need it to add.

 

__construct()
This magic methods is called when user create object (instances of your class) of the class. Usually this is used for creating constructor in php5.

 

__destruct()
As the name implies, the __destruct() method is called when the object is destroyed by PHP’s garbage collector. It accepts no arguments, and it is usually used to perform memory clean-up operations such as closing a database connection or closing any file.

[php]
<?php

class House{

public function __construct() {
$this->created = time();
$this->logfile = fopen(‘/tmp/log.txt’, ‘w’);
}

public function __destruct() {
fclose($this->logfile);
}
}
$home = new House;
echo $home->created;
?>
[/php]

 

__get
This method is called when your object try attempt to read property or variable of the class which is unavailable or inaccessible.

 

__set
This method called when object of your class attempts to set value of the property which is inaccessible or unavailable in your class.

 

__call
This magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable.

 

__callstatic
This is same as __call executes when inaccessible or unavailable method in static context.

[php]
<?php

class House
{
function __get($name)
{
echo "__get executed as $name is unavailable";
}
function __set($name , $value)
{
echo "__set executed as $name not exists or inaccessible";
}
function __call($name , $parameter)
{
$a = print_r($parameter , true); //taking recursive array in string
echo "__call executed with name $name , parameter $a";

}
static function __callStatic($name , $parameter)
{
$a = print_r($parameter , true); //taking recursive array in string
echo "__callStatic executed with name $name , parameter $a";

}
}
$a = new House();
$a->abc = 3;//__set executed
$app = $a->myvar;//__get triggerd
$a->getMyinfo(‘mahesh’ , ‘scriptarticle’, ‘blog’);//__call willl executed
House::xyz(‘1’ , ‘sca’ , ‘help’);//__callstatic will executed

?>
[/php]

 

__isset
This magic methods called when isset() function is applied on a property of the class which is inaccessible or unavailable.

 

__unset
This is just opposite of isset method as it called when unset() function called on inaccessible or unavailable property of the class.

[php]
<?php

class House
{
function __isset($name)
{
echo "__isset is called for $name is unavailable";
}
function __unset($name)
{
echo "__unset is called for $name";
}
}
$a = new House();
isset($a->myvar);
unset($a->yourvar);

?>
[/php]

 

__sleep
This method will trigger when you are going to serialize your class’s object.

__wakeup
This will execute when you are un-serializing any class object.

[php]
<?php

class House {
public $name;
public $width;
public $data = array(); // stores misc. data in an array
public $connection; // holds some connection resource

public function __sleep() {
// list the properties to save
return array(‘name’, ’56’, ‘data’);
}

public function __wakeup() {
// reconnect to the house
$this->connect();
}
}
?>
[/php]

 

__toString
This executes when you are using echo on your object.

__invoke
This will trigger when you are using object of your class as function.

[php]
<?php

class House
{
public $myvar;

public function __construct($myvar)
{
$this->myvar = $myvar;
}

public function __toString()
{
return $this->myvar;
}

public function __invoke($x)
{
var_dump($x);
}
}

$object = new House(‘Hello’);
echo $object; // __toString will called
$object(8); // __invoke called
?>
[/php]

 

I have still left some magic methods as __clone(),__set_state() I’ll post a new article for these very soon.

Hope the above one helps you a lot.

 

what is difference between array_merge and array_combine in php

Difference between array_merge and array_combine in php

 

Array Merge

PHP: array_merge
Merge one or more arrays

 

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one or if the arrays contain numeric keys, then the later value will not overwrite the original value and will be appended.

 

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

 

Have a look on example below

 

Example:
<?php
$array1 = array(“color” => “red”, 2, 4);
$array2 = array(“a”, “b”, “color” => “green”, “shape” => “trapezoid”, 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

 

Output will:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)

 

Array Combine

PHP: array_combine
Creates an array by using one array for keys and another for its values

 

Example:
<?php
$array1 = array(“green”,”red”,”yellow”);
$array2 = array(“avocado”, “apple”, “banana”);
$result = array_combine($array1, $array2);
print_r($result);
?>

 

Output will:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)

 

Quickly Deactivate or Activate All WordPress Plugins via Database or FTP

WordPress plugin activate deactivate all at once and Quickly

Sometimes during troubleshooting, it’s recommend to deactivate all plugins and activate them one by one and suppose at that time you will not able to login in WordPress admin (wp-admin) or white screen of death. At the same point we need to disable all the plugin or some plugin then in that case, How will you disabled/deactivate all or some plugin manually by the database or using FTP. Some time we need it to do very quickly.
I am sharing with you the technique by which you can quickly disable and (re)enable your entire set of WordPress plugins.

 

Deactivate All Plugins Using FTP

 

Navigate to the “/wp-content/” folder. You will see a folder called plugins. Rename the plugins folder to anything like  “plugins-deactivate”. Once you do this, all of your plugins will be deactivated automatically.

 

There may be due to some issue with plugin and it locked out your admin panel. If the issue was with your plugin, you should be able login to your “/wp-admin/” after deactivating the plugin. Once you do that, go back to your /wp-content/ directory and rename “plugins-deactivate” back to plugins. You can troubleshoot the problem by activating plugin one by one until your site breaks again.

 

 

Deactivate all plugins using phpMyAdmin

 

Login to your database using with phpMyAdmin and then have a backup.
I always suggest you to take Backup before doing any updates in table directly, so please in first step take backup of your database.
If you have backup of your database, you can quickly restore that and can revert your changes, so in this case(if there will some accident) you will always in safe hands.

 

Now, search a row in “wp_options” table that have “option_name” as “active_plugins”. Use the following SQL query (update your Database table prefix “wp_” if needed) for doing the same job.

 

SELECT * FROM wp_options WHERE option_name = ‘active_plugins’;

 

Once you found active_plugins column, click to edit it. You will see something like the below, depends on the number and type of plugins you have installed:

 

a:13:{i:0;s:19:”akismet/akismet.php”;i:1;s:23:”all_in_one_seo_pack.php”;i:2;s:28:”breadcrumb-navigation-xt.php”;i:3;s:37:”contact-coldform/contact_coldform.php”;i:4;s:13:”feedcount.php”;i:5;s:36:”google-sitemap-generator/sitemap.php”;i:6;s:13:”gravatars.php”;i:7;s:30:”nofollow-free/nofollowfree.php”;i:8;s:17:”ol_feedburner.php”;i:9;s:26:”simple_recent_comments.php”;i:10;s:18:”simple_twitter.php”;i:11;s:9:”top10.php”;i:12;s:16:”wp-db-backup.php”;}

 

This is a serialized array, you can unserialize this array by simply using the tool “http://www.functions-online.com/unserialize.html“. Copy the above string and click on run button you will get an array that represents every activated plugin on your site.

 

Thus, to quickly disable all plugins without using the wp-admin is cut the serialize array string as we have seen above and paste it into a safe notepad (text) file. After removing the code, click the button to save your changes and that’s it. All WordPress plugins are now deactivated (although you have all the plugins files in the same place “wp-content/plugins” and you can activate the plugin whenever you want by wp-admin area- if found working). This above method is obviously handy during, when the situation is critical and time-sensitive. Once you are ready to re-enable your entire set of plugins, simply copy & paste the preserved code(serialized array string) by notepad file and put it in the “active_plugins” field. Click save and done.

 

If you want to activate only some of those, unserialize the array by the above tool and make the array of plugins you want to activate then serialize that and put the string in the “activate_plugins” columns.

 

You can also go with a very simple SQL query to disable all plugins in one second.

UPDATE wp_options SET option_value = ” WHERE option_name = ‘active_plugins’;

 

 

This method works only for WordPress older versions(2.9 or older) for later use the below.

UPDATE wp_options SET option_value = ‘a:0:{}’ WHERE option_name = ‘active_plugins’;

Search Engine Spider and User Agent Identification with “Ultimate User Agent Blacklist”

Search Engine Spider and User Agent Identification with “Ultimate User Agent Blacklist”

 

A user-agent is software (a software agent) that is acting on behalf of a user.
In many cases, a user-agent acts as a client in a network protocol used in communications within a client–server distributed computing system.

 

For more information view wiki
http://en.wikipedia.org/wiki/User_agent

 

Automated Agents is called as Bots.
http://user-agents.org has a complete list of all latest spiders/bots/user agents.
There are some more and those are anonymous (not known and have very different names).

 

If you develop a website and want to make the site accessible by some specific user agent or bots of a country, then you can update the information in your root .htaccess file.

 

As, Bot blocking blacklists are useless in some way as some rogue spiders just generate random user-agent strings so we will never have them in our list to start with, but We have tried list out as much as we can in the below zip file.

 

Ultimate User Agent Blacklist

 

What you have to done is, unzip the file and paste the code in your root .htaccess file, it will protect your website from unwanted crawling/indexing by anonymous bots.

 

If your website uses WordPress open-source, then can also use “Better WP Security” plug-in.
Just need to download and install the plug-in and go to “Better WP Security – Ban Users” Tab and enable “Enable Default Banned List”, you can also update the list according to your needs.

 

But please be careful before doing same, as it may affect your website’s core files and plug-in, So before doing this backup your website’s files and database.