How to read and write to file using PHP

This quick tutorial will show you how to read and write to a file on your server. There are many ways to do this so I’ll show you a few.

The first way is to simply read the whole file into a string, this is real easy.

You can also read a file in as an array by using the file() method.

Pretty easy right? You can also use a URL in place of a text file. The possibilities are endless.

Writing to a file is also pretty easy. Let’s say you want to append to a file.

There are so many ways to use files. Check out the PHP documentation for file_put_contents()

Failed to open stream: No such file or directory autoload.php

You ever having problem, when you trying to install Laravel onto web server (xampp/wamp) local server or installing new demo laravel app from github, with this error message : (require(C:………….bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in C:….laravelbootstrapautoload.php on line 17) . To fix this problem, you can follow this solutions :

No such file or directory autoload.php in laravel

If you have Git, that already installed on your computer, go a head to your directory where your placing your laravel app and then try to running command this php composer.phar install (or  just composer install or composer update ( if composer.json already there)) and if it still doesn’t work then php composer.phar dump-autoload. after that try to reload your app in a browser.

Delete an element value from an Array in PHP

Here is an easy way to delete an element from a PHP array, such that foreach ($array). You can use Unset Function (PHP 4, PHP 5)

Description : void unset ( mixed $var [, mixed $... ] )

unset() destroys the specified variables.

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

Example 1

Example 2

Put Element to the Beginning of an Array in PHP

Usually we use this $arr[] = $item; to add element into the end. But how to add an element to the beginning of array in PHP?

You can use array_unshift($array, $item);

array_unshift()prepends passed elements to the beginning of an array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.

How to Use it?

Use array_unshift($array, $item);

Example 1

will give you

Example 2

will give you

PHP Function – Memory Usage Information

By observing the memory usage of your scripts, you may be able optimize your code better.
PHP has a garbage collector and a pretty complex memory manager. The amount of memory being used by your script. can go up and down during the execution of a script. To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.

Reference : net nuts

PHP Function – Glob() to Find Files

Many PHP functions have long and descriptive names. However it may be hard to tell what a function named glob() does unless you are already familiar with that term from elsewhere.
Think of it like a more capable version of the scandir() function. It can let you search for files by using patterns.

You can fetch multiple file types like this:

Note that the files can actually be returned with a path, depending on your query:

If you want to get the full path to each file, you can just call the realpath() function on the returned values:

Resource: net tuts

Php For Beginners Video Tutorial 4

This episode of Php For Beginners – Video Tutorial series continues on how to pass multiple variables from page to page. You will also have a review for the “foreach” statement. You can get the source files here.

Multiple Variables and “foreach” Statement