How to write into a file in PHP?

The fwrite function is used in PHP to write to a file. It is overloaded, which means that it accepts various data types, such as string, integer, floating point,s, and more. In our example, we will show you how to use this function to write into a file in PHP. Writing information into a file on the server can be done by using the fwrite function. 

file_put_contents php json file

By Waved AlbatrossWaved Albatross on Nov 10, 2019
$data[] = $_POST['data'];

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData);

Add Comment

2

stristr php

By Joyous JaguarJoyous Jaguar on Apr 06, 2020
<?php
  $string = 'Hello World!';
  if(stristr($string, 'terre') === FALSE) {
   echo '"terre" non trouvé dans la chaîne de caractères';
  }
// affiche : "terre" non trouvé dans la chaîne de caractères
?>

Add Comment

1

php superglobals

By JSMDJSMD on May 13, 2020
#Superglobal

$_SERVER Superglobal
Superglobals were introduced in PHP 4.1.0, and are built-in variables 
  that are always available in all scopes. Basically system variables.
https://www.w3schools.com/php/php_superglobals.asp

Note: $_SERVER Superglobal  -- tells a little about the server and 
  the client

==============
#Example index.php
<?php include 'server-info.php';?>
<!DOCTYPE html>
<html>
<head>
    <title>System Info</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class ="container">
    <h1>Server & File Info</h1>
    <?php if($server): ?>
    <ul class="list-group">
    <?php foreach($server as $key => $value): ?>
        <li class="list-group-item">
            <strong><?php echo $key; ?>: </strong>
            <?php echo $value; ?>
        </li>
    <?php endforeach; ?>
    </ul>

    <?php endif; ?>

    <h1>Client Info</h1>
    <?php if($client): ?>
    <ul class="list-group">
    <?php foreach($client as $key1 => $value1): ?>
        <li class="list-group-item">
            <strong><?php echo $key1; ?>: </strong>
            <?php echo $value1; ?>
        </li>
    <?php endforeach; ?>
    </ul>

    <?php endif; ?>

</body>
</html>
==================
#Example server-info.php
<?php
    # $_SERVER SUPERGLOBAL

    //Create Server Array
    $server =[
        'Host Server Name' => $_SERVER['SERVER_NAME'],
        'Http Host' => $_SERVER['HTTP_HOST'],
        'Server Software' => $_SERVER['SERVER_SOFTWARE'],
        'Document Root' => $_SERVER['DOCUMENT_ROOT'],
        'Current Page' =>  $_SERVER['PHP_SELF'],
        'Script Name' =>  $_SERVER['SCRIPT_NAME'],
        'Absloute Path' =>  $_SERVER['SCRIPT_FILENAME']

    ];

    echo $server['Host Server Name'];
    echo $server['Http Host'];
    echo $server['Server Software'];
    echo $server['Document Root'];
    echo $server['Current Page'];
    echo $server['Script Name'];
    echo '<br>';
    //also can show it all
    print_r($server);

    //Creat Client Array
    $client = [
        'Client System Info' => $_SERVER['HTTP_USER_AGENT'],
        'Client IP' => $_SERVER['REMOTE_ADDR'],
        'Remote Port' => $_SERVER['REMOTE_PORT']

    ];
    echo '<br>';
    echo '<br>';
    print_r($client);

?>

Add Comment

2

php file_put_contents

By XediriXediri on Jun 11, 2020
file_put_contents ($filename, $data, $flags = 0, $context = null): int

Add Comment

3

The function will accept three arguments - filename, data to write, and length of said data, it can return a value of true or false depending on whether it was successful or not.

PHP answers related to "php file_put_contents"

View All PHP queries

PHP queries related to "php file_put_contents"

php file_put_contents ?? ' ' operator in php laravel php artisan app:name in laravel 6 php artisan make:auth laravel 8 not working php artisan ui:auth laravel 7 php cache clear in laravel php extensions for laravel php laravel dump php laravel return json response php laravel rount price to 99 php run server laravel php unit testing laravel remove index.php laravel in linux server cheque print in php codeigniter install php ubuntu 20.04 php enable module php display errors php console log php for loop php sha256 php in array php uppercase each word php uppercase first letter php is numeric php now simple localhost php php include once inside a function? php if else how to convert array to string with commas in php curl php example php string to date php string functions Line : 83 -- syntax error, unexpected end of file php pagination in php php print_r php check if type is mysqli_result saber value ao escolher combobox php Best Code Beautifier tools for php Uncaught TypeError: Argument 1 passed to NotORM_Result::update() php slim array shift php php PDO database connection set default php version in ubuntu php time() spread operator php Check if string starts with php radio button in php form increase PHP Memory login.php php class https//:localhost/phpmyadmin/index.php php get session variable from table Convert string to lowercase in php parseint php Php array length date to string php php reverse shell Mkdir permission denied php Php += Move uploaded file in php Php mysqli fetch assoc Php trying to get property of non-object Php strtolower date format in php array to object in php Send email php smtp hostinger Show all errors in php binemon to php password and confirm password validation in php php int to strin php timeout Php localhost str_pad_left in php php htmlspecialchars php redirect to page

Browse Other Code Languages

CodeProZone