Monday, 25 May 2015

How can I upload file asynchronously in php?

How can I upload file asynchronously in php?

Following are simple 4 Steps to upload file asynchronously  in PHP.

Step #1
Add jQuery javascript file in your page - Need for Ajax call.


<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>


Step #2
Add Following Html, where you want to show the button to upload the file.






Step #3:
Add following javascript code which is responsible for uploading the file.
URL: /folder/upload-file.php
Description: Here you need to write the send the request to server.
$( document ).ready(function() {
    $(':button').click(function(){
        var formData = new FormData($('form')[0]);
        $.ajax({
            url: '/folder/upload-file.php', //Location of upload file 
            type: 'POST',
            xhr: function() {  // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                return myXhr;
            },

            beforeSend: function(data){},
            success: function(data){},
            error: function(data){},        
            data: formData,        
            cache: false,
            contentType: false,
            processData: false
        });
    });
});


Step #4
Add Following code in "/folder/upload-file.php" which will upload the file in Server.
$targetFile="/path/to/upload/file";//Location of srver
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
        echo "success";
    } else {
        echo "failure";
    }





Friday, 22 May 2015

Shared Server Security Risk open_basedir disable_functions disable_classes

How to secure website on shared server


There are a variety of security issues that arise when using shared hosting solutions. There are three php.ini directives that remain important in a shared hosting

open_basedir : The open_basedir directive provides the ability to limit the files that PHP can open
to a specified directory tree. When PHP tries to open a file with, for example, fopen()
or include, it checks the the location of the file. If it exists within the directory tree
specified by open_basedir, then it will succeed; otherwise, it will fail to open the file.

disable_functions :  You can disable function like exec, passthru, shell_exec, system etc for security purpose.

disable_classes : You can disable class like DirectoryIterator, Directory for security purpose.


You may set the open_basedir directive in php.ini OR on a per-virtual-host basis in httpd.conf. In the following httpd.conf virtual host example, PHP scripts may only open files located in the /home/user/www and /usr/local/lib/php directories.

<VirtualHost *:80>
    DocumentRoot 'C:/wamp/www/zf1_11/public_html'
    ServerName zf11.localhost
    <Directory 'C:/wamp/www/zf1_11/public_html'>
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>