Showing posts with label file handling. Show all posts
Showing posts with label file handling. Show all posts

Sunday, September 13, 2015

file upload in php

<?php
if(isset($_POST['submit'])){
$file = $_FILES['file'];
$name = $file['name'];
    $type = $file['type'];
$tmp_name = $file['tmp_name'];
$error = $file['error'];
$extension = explode('.', $name);
    $extension = end($extension);
    $extension = strtolower($extension);
    $allowed = ['txt', 'php', 'html'];
    $directory = 'assignment/';
    if(in_array($extension, $allowed)){
        $location = $directory . $name;
        move_uploaded_file($tmp_name, $location);
    }
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" id="">
<input type="submit" name="submit" id="">
</form>
</body>
</html>

Saturday, September 12, 2015

storing form data to a file (working with file)

<?php
if(isset($_POST['name'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $string = $name . " \t "  . $email . " \t " . $contact . "\n";
    $pointer = fopen('myfile.txt', 'a+');
    fwrite($pointer, $string);
}


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1>Please contact with us</h1>
    <form method="post">
       <div class="form-group">
           <label for="name">Name</label>
           <input type="text" name="name" id="name" class="form-control">
       </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" class="form-control">
        </div>
        <div class="form-group">
            <label for="contact">Contact</label>
            <input type="text" name="contact" id="contact" class="form-control">
        </div>
        <div class="form-group">
            <input type="submit" name="submit" id="contact" class="btn btn-lg btn-success">
        </div>

    </form>
</div>

</body>
</html>

Sunday, August 30, 2015

findings frequency number from a file by using own explode code and frequency code - modularity approach

in functions.php
===========================

<?php
//for exploding a string to array
function myExplode($string)
{
    $length = strlen($string);
    $word = "";
    $words = array();
    for ($i = 0; $i < $length; $i++) {
        if (ord($string[$i]) == 13) {
            continue;
        }
        if ($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n") {

            if ($i == $length - 1) {
                $word .= $string[$i];
            }
            if(!empty($word)){
                $words[] = $word;
            }
            $word = "";
        } else {
            $word .= $string[$i];
        }
    }
    return $words;
}




//for determine frequency
function frequency($words){
    $frequency = array();

    foreach ($words as $word) {
        if(array_key_exists($word, $frequency)){
            $frequency[$word] ++;
        } else {
            $frequency[$word] = 1;
        }
    }

    return $frequency;
}


in file.php
================

<?php
require 'functions.php';
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$words = myExplode($string);
$frequency = frequency($words);
print_r($frequency);

findings frequency number from a file by using own explode code and frequency code

<?php
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$length = strlen($string);
$word = "";
$words = array();

for($i = 0; $i < $length; $i++){
    if(ord($string[$i]) == 13){
            continue;
        }
    if($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n"){

        if($i == $length - 1){
            $word .= $string[$i];
        }
        if(!empty($word)){
            $words[] = $word;
        }
        $word = "";
    }else{
        $word .= $string[$i];
    }
}
//for determine frequency
$frequency = array();

foreach ($words as $word) {
    if(array_key_exists($word, $frequency)){
        $frequency[$word] ++;
    } else {
        $frequency[$word] = 1;
    }
}

foreach($frequency as $key => $value){
    echo "$key => $value <br>";
}

file writing and file appending || file handling in php

<?php
/*
 *file reading and file appending
 * */
$pointer = fopen('hello.txt', 'w');//mode is w means file open for writing purpose
fwrite($pointer, "Hello world\n"); //that will replace all existing sentence or word and write hello world in hello.txt file
fclose($pointer);

$pointer = fopen('hello.txt', 'a');//mode is a means file open for appending some line / word
fwrite($pointer, 'Hello world in another line');

file reading || file handling in php

<?php
/*
 *for reading only
 * */
$pointer = fopen('php.txt', 'r'); //first argument is file name, 2nd parameter is mode some modes are r, w, a, a+
//fgets() return one line from file keep cursor to end of the line
echo fgets($pointer) ;
echo '<br>';
echo fgets($pointer) ;
echo '<br>';
fclose($pointer);



$pointer2 = fopen('php.txt', 'r');
//fgets() return one line from file keep cursor to end of the line
echo fgets($pointer2) ;
echo '<br>';
echo fread($pointer2, filesize('php.txt'));//this line echoing everything (all word) from php.txt
echo '<br>';
fclose($pointer2);


$pointer3 = fopen('php.txt', 'r');
while(!feof($pointer3)){
    echo fgetc($pointer3) . '<br>';
}
fclose($pointer3);
//feof() is file-end-of-file, fgetc() is file-get-character();

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }