Creating a basic php file upload with mysql

I don’t usually think deep in making programs. Mostly, I just follow the basics and find the commands and compile it to make the program.

  1. Google the Create the MYSQL command function to connect to the database

http://www.w3schools.com/php/php_mysql_connect.asp


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

2. Google the upload command function and compile it with the mysql connect function

http://www.w3schools.com/php/php_file_upload.asp


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>
<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>


From that we created a simple mysql connect with php file upload.

OOP class pagination PHP

<?
/*
* Developer: Rex Adrivan
* Description: OOP pagination class
*/

class Pagination{
function Paginate($values,$per_page){
$total_values = count($values);

if(isset($_GET['page'])){
$current_page = $_GET['page'];
}else{
$current_page = 1;
}
$counts = ceil($total_values / $per_page);
$param1 = ($current_page - 1) * $per_page;
$this->data = array_slice($values,$param1,$per_page);

for($x=1; $x<= $counts; $x++){
$numbers[] = $x;
}
return $numbers;
}
function fetchResult(){ 
$resultsValues = $this->data;
return $resultsValues;
}
}


// Sample Usage

$pag = new Pagination();
$data = array("Hello","Rex","Prosper","Adrivan","Hehe");
$numbers = $pag->Paginate($data,2);
$result = $pag->fetchResult();
foreach($result as $r){
echo '<div>'.$r.'</div>';

}

foreach($numbers as $num){ 
echo '<a href="classpagination.php?page='.$num.'">'.$num.'</a>';
}



?>


Facebook Auto tag and upload Photo

$args = array( ‘message’ => ‘Message’, ‘image’ => ‘@’ . realpath($path_to_image), ‘tags’ => array( array( ‘tag_uid’=> $friend1_uid, ‘x’ => $x1, ‘y’ => $y1, ), array( ‘tag_uid’ => $friend2_uid, ‘x’ => $x2, ‘y’ => $y2, ),/*…*/ ), ); $data = $facebook->api(‘/me/photos’, ‘post’, $args);
Where $facebook is initialized Facebook PHP SDK object. $data[‘id’] is id of uploaded photo.

Notes: 1. fileUpload option has to be set when initializing Facebook object:

$facebook = new Facebook(array( /*..*/ ‘fileUpload’ => true, ));