Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, August 18, 2017

Install Laravel Framework on Ubuntu

Step 1 – Install LAMP

You will need to make sure your LAMP meets the following requirements:
  • PHP >= 5.6.4
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension
$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php5.6 php5.6-mcrypt php5.6-gd
$ sudo apt-get install apache2 libapache2-mod-php5
$ sudo apt-get install mysql-server php5.6-mysql

Step 2 – Install Composer

Install Composer which is a tool for dependency management in PHP.

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer

Step 3 – Install Laravel

Now create a directory where Laravel will be downloaded.
$ mkdir /var/www/html/your_website

Now enter the newly created directory and download the latest Laravel version.
$ cd /var/www/html/your_website
$ git clone https://github.com/laravel/laravel.git

Start the Laravel installation using Composer:
$ composer install

Dependencies installation will take some time. After than set proper permissions on files.
$ chown -R www-data.www-data /var/www/laravel
$ chmod -R 777 /var/www/laravel/storage

Step 4 – Set Encryption Key

# php artisan key:generate
You will notice the below error when running the command.

[ErrorException]
  file_get_contents(/var/www/html/your_website/.env): failed to open stream: No such file or directory

To solve this you should rename the .env.example file into .env:
$ mv .env.example  .env

Generate the encryption key again:
# php artisan key:generate

You should get the following output:
Application key [base64:ULQsledeS17HxCAsssA/06qN+aQGbXBPPpXVeZvdRWE=] set successfully.

Now edit the app.php file and configure the encryption key. Open the file with your favorite text editor. 

# vim config/app.php

Locate the ‘key’ => env(‘APP_KEY’ line and add the key next to it. After you are done the directive should look like this:


'key' => env('APP_KEY', 'base64:7fGASOGOSASA%^AYUFSA9TxZu8M2NwBWVEQsjPGRiasbdasYITIUG$%^$CSSA='),

    'cipher' => 'AES-256-CBC',

Save and close the file.

Now open your favorite web browser and navigate to  http://localhost/your_website  where you will be welcomed by a page as shown in the image below:
Laravel First Page

Wednesday, June 14, 2017

How can I downgrade / Upgrade from PHP 7.0 to PHP 5.6 / PHP 5.6 to PHP 7.0 / PHP 8 on Ubuntu

Ubuntu 16.04 comes with php7.0, and some php web applications might fail to run  with php7.0. So, you test your web app with both php7.0 and php5.0  version. 

You can do following changes with Ubuntu sudo update-alternatives --config php
 
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update

root@pc1# apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0 php5.6-curl php5.6-gd php5.6-mcrypt php5.6-xml php5.6-xmlrpc

sudo apt-get install php8.1 php8.1-fpm
sudo apt-get install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl
sudo apt-get install php7.4 php7.4-fpm
sudo apt-get install php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl


Installing both php5.6 & php7.0 & php8.0 was clean in my case: no complain of issues, etc.

To switch from php7.0 to php5.6 or other:

# For php in web apps
sudo a2dismod php7.0 && sudo a2enmod php5.6 && sudo service apache2 restart
OR sudo update-alternatives --config php
# For php-cli in the command line
sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php
or from  php5.6 to php7:
# For php in web apps
sudo a2dismod php5.6 && sudo a2enmod php7.0 && sudo service apache2 restart
# For php-cli in the command line
sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php
Now Check php version : root@pc1# php --version or php -v
Remove : sudo apt-get remove php5.6-* 

Sunday, June 11, 2017

Problems faced during migration php/mysql code Windows to Linux

1.  Slashes in file names when migrating PHP project to Windows server from Linux server.
Solution : Need to remove back slash & forward slash with underscore (" _ ")

2. Case Sensitive : Mostly in windows both "A" and "a" treat same. But in Linux thinks are different. So need to take care of this. This problems faces both in PHP and MySql files and table respectively. 

You can configuring MySQL Tables to be Case Insensitive by editing the MySQL configuration file, which is generally found at /etc/mysql/my.cnf/etc/my.cnf or ~/my.cnf

Add the following line to the my.cnf configuration file:
lower_case_table_names=1
Then restart MySQL (or reboot):
/etc/init.d/mysql restart (Debian/Ubuntu/SuSE)
/etc/init.d/mysqld restart (Red Hat/CentOS/Fedora)

Today me facing problem the CodeIgniter Web Framework  code. In case of windows folder name was in cap mean in Upper case and in php coding same folder in lower case. It working fine with window server (XAMPP). But when I migrate same into Linux server (ubuntu LAMP) case sensitivity matters and creating problem to load files. You need to take care this otherwise php code does not work. 

Wednesday, January 13, 2016

Create and Embed Barcodes using PHP

Requirements: 
PHP GD Library 
In case of Debian Linux just run command :

#sudo apt-get install php5-gd

Barcode script : 
View the code here: https://github.com/davidscotttufts/php-barcode/

That Scripts generates barcodes in four barcode formats including Code 128, Code 39, Code 2of5, and Codabar. With a little over 100 lines of code you have the options of “vertical” or “horizontal” display, varying barcode heights, and one of four barcode formats.

How to embed in HTML code:
Example 1:
<img alt="testing" src="/yourfilelocation/barcode.php" />
Example 2 (with text data):
<img alt="testing" src="/yourfilelocation/barcode.php?text=testing" />
Example 3 (with codetype, size, text)
<img alt="TESTING" src="/yourfilelocation/barcode.php?codetype=Code39&size=40&text=TESTING" />

Wednesday, May 28, 2014

Validation uploaded File in php


if(isset($_FILES['uploaded_file'])) {
    $errors     = array();
    $maxsize    = 1000000;
    $acceptable = array(
        'image/jpg',
        'image/gif',
        'image/png',
        'application/pdf',
        'image/jpeg',
        
    );

    if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 1MB.';
    }

    if(!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"])))
    {
    $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
    }

if(count($errors) === 0) {
    move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
} else {
    foreach($errors as $error) {
        echo '<script>alert("'.$error.'");</script>';
    }

    die(); //Ensure no more processing is done
}
}

Friday, October 26, 2012

Backup MySQL : Execute database query using PHP file.


Below is an example of using SELECT INTO OUTFILE query for creating table backup 

<?php
$dbhost = '';
$dbuser = ' ';
$dbpass = ' ';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'db_name';
mysql_select_db($dbname);
$tableName  = 'table_name';
$backupFile = '/backup/file_name.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query) or die();
mysql_close($conn);
}
?>

To restore the backup you just need to run LOAD DATA INFILE query like this :

<?
 $tableName  = 'table_name';
$backupFile = 'file.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);
?>

Facing Problem :

when you try to create output file on server may be faced 
Error : Can't create/write to file '/var/www/test/backup/mypets.sql' (Errcode: 13)

Solution :

Recent Ubuntu Server Editions (such as 10.04) ship with AppArmor and MySQL's profile might be in enforcing mode by default. You can check this by executing sudo aa-status like so:

# sudo aa-status
5 profiles are loaded.
5 profiles are in enforce mode.
   /usr/lib/connman/scripts/dhclient-script
   /sbin/dhclient3
   /usr/sbin/tcpdump
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/sbin/mysqld
0 profiles are in complain mode.
1 processes have profiles defined.
1 processes are in enforce mode :
   /usr/sbin/mysqld (1089)
0 processes are in complain mode.
If mysqld is included in enforce mode, then it is the one probably denying the write. Entries would also be written in /var/log/messages when AppArmor blocks the writes/accesses. What you can do is edit /etc/apparmor.d/usr.sbin.mysqld and add /backup/ or /data/* near the bottom like so:

...
/usr/sbin/mysqld {
    ...
    /var/log/mysql/ r,
    /var/log/mysql/* rw,
    /var/run/mysqld/mysqld.pid w,
    /var/run/mysqld/mysqld.sock w,
    /data/ r,
    /data/* rw,
   /backup/* rw,
}

And then make AppArmor reload the profiles.

# sudo /etc/init.d/apparmor reload
WARNING: the change above will allow MySQL to read and write to the /data directory. We hope you've already considered the security implications of this.



Saturday, May 19, 2012

PHP : mysql_real_escape_string() function


The mysql_real_escape_string() function escapes special characters in a string for use in an SQL statement
The following characters are affected
Syntax : mysql_real_escape_string(string,connection)
string : Required. Specifies the string to be escaped
connection  : Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

Example :
<?php
// Connect$link mysql_connect('mysql_host''mysql_user''mysql_password')    OR die(mysql_error());
// Query$query sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),      
           mysql_real_escape_string($password));
?>