Showing posts with label PHP FOPEN. Show all posts
Showing posts with label PHP FOPEN. Show all posts

Friday, 2 August 2019

PHP function used to read CSV files

The below function can be used to read CSV files and returning an array with all values obtained from the CSV file
USAGE:
Copy the below function to your PHP script
function readCSV($csvFile){
 
 
        $file_handle = fopen($csvFile, 'r');
 
        while (!feof($file_handle) ) {
 
                $line_of_text[] = fgetcsv($file_handle, 1024);
 
        }

Friday, 5 October 2018

PHP SNIPPET TO WRITE TO A FILE

Monday, 3 September 2018

PHP allow_url_fopen is enabled but does not work

I've a problem with a fetch in my server.

I'm trying to get all content from a url, and save it in a variable.
But, this return fopen die message.
This is my code:
//Guardo la url pasada por get
$url = $_GET["url"];

if(preg_match('#^http://www.filmaffinity.com.*#s', trim($url))){
  //Funciona
} else{
$data = array('msg' => 'bad url');
   echo json_encode($data);
   return false;
}
//Tomo el código y lo meto en una variable
$fo= fopen($url,"r") or die ("No se encuentra la pagina.");
   while (!feof($fo)) {
   $cadena .= fgets($fo, 4096);
}
fclose ($fo);

This is part of my code, when i execute this, return 'No se encuentra la pagina.'
allow_url_fopen is ON in my server.
How can fix this problem? Can help me?
Thanks.

You are just looking for a certain name in the url?
This will work better.
if(stripos('http://www.filmaffinity.com',$url)){}

you may want to try:
file_get_contents($url)

You may want to try encoding the url:
$url = urlencode($url)

or
$url = rawurlencode($url)

curl will get you more answers to your problem:
with curl you can control the the timeout, see the request (curl_getinfo) and response (CURLINFO_HEADER_OUT, true) headers, the HTTP Status (['http_code']), it can follow 30x redirects, etc.
There is an issue:
$data = file_get_contents('http://www.filmaffinity.com');

Returned an error:
 file_get_contents(http://www.filmaffinity.com) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error

With curl:
HTTP CODE: aarray (
  'url' => 'http://www.filmaffinity.com/',
  'content_type' => 'text/html',
  'http_code' => 302,
  'header_size' => 223,
  'request_size' => 170,
  'filetime' => -1,
  'ssl_verify_result' => 0,
  'redirect_count' => 0,
  'total_time' => 0.321491,
  'namelookup_time' => 0.040338,
  'connect_time' => 0.180309,
  'pretransfer_time' => 0.180365,
  'size_upload' => 0,
  'size_download' => 20,
  'speed_download' => 62,
  'speed_upload' => 0,
  'download_content_length' => 20,
  'upload_content_length' => -1,
  'starttransfer_time' => 0.321415,
  'redirect_time' => 0,
  'certinfo' =>
  array (
  ),
  'redirect_url' => 'http://www.filmaffinity.com/en/main.html',
  'request_header' => 'GET / HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/32.0
Host: www.filmaffinity.com
Accept: */*
Accept-Encoding: deflate, gzip

With curl it works, but you must change this:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

To true:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Tested Working Code:

<?php
header('Content-Type: text/plain; charset=utf-8');
echo "start\n";
$url = 'http://www.filmaffinity.com';
 $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_ENCODING,"");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/32.0");
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT,100);
  curl_setopt($ch, CURLOPT_FAILONERROR,true);
  $data = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
  }
  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
    $responseHeader = substr($data,0,$skip);
    $data= substr($data,$skip);
    $info = curl_getinfo($ch);
    if ($info['http_code'] != '200')
    $info = var_export($info,true);
   }
  if ($info['http_code'] != '200'){echo 'HTTP CODE: ' .$info['http_code'];}

preg_match_all('#<a href="([^"]*)#m',$data,$matches);
foreach($matches[1] as $val){
  $url = 'http://www.filmaffinity.com' . $val;
  echo "$url\n";
}
?>

This is the Result

 http://www.filmaffinity.com/en/main.html
    http://www.filmaffinity.com/en/advsearch.php
    http://www.filmaffinity.com/en/login.php
    http://www.filmaffinity.com/en/register.php
    http://www.filmaffinity.com/en/main.html
    http://www.filmaffinity.com/en/awards.php?award_id=berlin&year=2015
    http://www.filmaffinity.com/en/awards.php?award_id=academy_awards&year=2015
    http://www.filmaffinity.com/en/cat_new_th_us.html
    http://www.filmaffinity.com/en/boxoffice.php
    http://www.filmaffinity.com/en/imlost.php
    http://www.filmaffinity.com/en/all_awards.php
    http://www.filmaffinity.com/en/best_2014.php
    http://www.filmaffinity.com/en/oscar_data.php
    http://www.filmaffinity.com/en/topgen.php?nodoc=1
    http://www.filmaffinity.comhttp://www.filmaffinity.com/es/main.html
    http://www.filmaffinity.com/en/cookies_info.php
    http://www.filmaffinity.com/en/tours.php
    http://www.filmaffinity.com/en/tour.php?idtour=55
    http://www.filmaffinity.com/en/tour.php?idtour=54
    http://www.filmaffinity.com/en/tour.php?idtour=29
    http://www.filmaffinity.com/en/tour.php?idtour=9
    http://www.filmaffinity.com/en/tour.php?idtour=24
    http://www.filmaffinity.com/en/tours.php
    http://www.filmaffinity.com/en/trailers.php
    http://www.filmaffinity.com/en/bestrated.php
    http://www.filmaffinity.com/en/film489970.html
    http://www.filmaffinity.com/en/film221477.html
    http://www.filmaffinity.com/en/film730528.html
    http://www.filmaffinity.com/en/film809297.html
    http://www.filmaffinity.com/en/film399474.html
    http://www.filmaffinity.com/en/film795770.html
    http://www.filmaffinity.com/en/film536488.html
    http://www.filmaffinity.com/en/film695552.html
    http://www.filmaffinity.com/en/bestrated.php
    http://www.filmaffinity.com/en/mostrated.php
    http://www.filmaffinity.com/en/film575568.html
    http://www.filmaffinity.com/en/film670216.html
    http://www.filmaffinity.com/en/film124904.html
    http://www.filmaffinity.com/en/film636539.html
    http://www.filmaffinity.com/en/film206955.html
    http://www.filmaffinity.com/en/film779937.html
    http://www.filmaffinity.com/en/film617730.html
    http://www.filmaffinity.com/en/film423489.html
    http://www.filmaffinity.com/en/mostrated.php
    http://www.filmaffinity.com/en/worstrated.php
    http://www.filmaffinity.com/en/film189979.html
    http://www.filmaffinity.com/en/film612348.html
    http://www.filmaffinity.com/en/film968394.html
    http://www.filmaffinity.com/en/film651247.html
    http://www.filmaffinity.com/en/film281762.html
    http://www.filmaffinity.com/en/film886013.html
    http://www.filmaffinity.com/en/film591128.html
    http://www.filmaffinity.com/en/film381051.html
    http://www.filmaffinity.com/en/worstrated.php
    http://www.filmaffinity.com/en/mostvisited.php
    http://www.filmaffinity.com/en/film124904.html
    http://www.filmaffinity.com/en/film994565.html
    http://www.filmaffinity.com/en/film941942.html
    http://www.filmaffinity.com/en/film575568.html
    http://www.filmaffinity.com/en/film670216.html
    http://www.filmaffinity.com/en/film423489.html
    http://www.filmaffinity.com/en/film809035.html
    http://www.filmaffinity.com/en/film391687.html
    http://www.filmaffinity.com/en/film934433.html
    http://www.filmaffinity.com/en/mostvisited.php
    http://www.filmaffinity.com/en/cat_new_th_us.html
    http://www.filmaffinity.com/en/film124904.html
    http://www.filmaffinity.com/en/film376816.html
    http://www.filmaffinity.com/en/film898006.html
    http://www.filmaffinity.com/en/film562434.html
    http://www.filmaffinity.com/en/film510733.html
    http://www.filmaffinity.com/en/film272576.html
    http://www.filmaffinity.com/en/film493854.html
    http://www.filmaffinity.com/en/film792317.html
    http://www.filmaffinity.com/en/film784978.html
    http://www.filmaffinity.com/en/cat_new_th_us.html
    http://www.filmaffinity.com/en/cat_upc_th_us.html
    http://www.filmaffinity.com/en/film526524.html
    http://www.filmaffinity.com/en/film405261.html
    http://www.filmaffinity.com/en/film543207.html
    http://www.filmaffinity.com/en/film809035.html
    http://www.filmaffinity.com/en/film402986.html
    http://www.filmaffinity.com/en/film956269.html
    http://www.filmaffinity.com/en/film759419.html
    http://www.filmaffinity.com/en/film699453.html
    http://www.filmaffinity.com/en/film701069.html
    http://www.filmaffinity.com/en/cat_upc_th_us.html
    http://www.filmaffinity.comhttp://www.facebook.com/FilmAffinity
    http://www.filmaffinity.comhttp://twitter.com/Filmaffinity
    http://www.filmaffinity.com/en/faq.php
    http://www.filmaffinity.com/en/private.php

Then Add (replace the foreach loop above):
foreach($matches[1] as $key => $val){
  if (!strpos($val,'/film')){continue;}
  $url = 'http://www.filmaffinity.com' . $val;
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_ENCODING,"");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/32.0");
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT,100);
  curl_setopt($ch, CURLOPT_FAILONERROR,true);
  $data = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
  }
  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
    $responseHeader = substr($data,0,$skip);
    $data= substr($data,$skip);
    $info = curl_getinfo($ch);
    if ($info['http_code'] != '200')
    $info = var_export($info,true);
   }
  if ($info['http_code'] != '200'){echo 'HTTP CODE: ' .$info['http_code'];}
  $fp = fopen("html$key.txt",'w');
  fwrite($fp,$data);
  fclose($fp);
}

Thursday, 30 August 2018

delete a new line from the txt file after inserting the value

I create a txt file from a folder in that way:

$fp = fopen("mylist.txt","rw+");
foreach(glob("folder/*.*") as $value){
fwrite($fp,$value."\n");
}

and it create inside the txt each items, one per line, BUT it also add a newline at the end of the file.
This is the content of mylist.txt:
foldelr/file1.mp3
folder/file2.mp3
(BLANK NEWLINE)

I tried to remove the blank newline at the end of the txt file, in this way:
$filetxt = fopen("mylist.txt","r");
$rtrim = rtrim($filetxt, "\n");
$newfile = file_put_contents("newfile.txt", $rtrim);

But it doesn't work, because in the "newfile.txt", i still have the newline blank after last file name.
I tried to convert file in array, use "array_diff()" to remove the last line, but it fail. I tried also all the suggestions in this thread: remove new line characters from txt file using php but not fix my problem.
Does anyone can please help me to understand what i'm missing or mistaken?
Kind Regards
Brus

Use file_get_contents method it works fine for me
    $fp = fopen("mylist.txt","rw+");
    foreach (glob("test/*.*") as $value) {
        fwrite($fp, $value . "\n");
    }

    $filetxt = file_get_contents("mylist.txt");
    $rtrim =  rtrim($filetxt, "\n");
    $newfile = file_put_contents("mylist.txt", $rtrim);

Or You can also remove the last newline by using the array count.
$fp = fopen("mylist.txt","rw+");
$i =0;
foreach(glob("folder/*.*") as $value){
  $i++;
  if($i < count(glob("folder/*.*")))
    fwrite($fp,$value."\n");
  else
    fwrite($fp,$value);
}

PHP - Create an associative array from the .txt file


This question already has an answer here:


  • CSV to Associative Array 5 answers
I have .txt file formatted as such:
    "id","dealer_id","vin","stockno",
    "1","2","3","4",
    "5","6","7","8",
    "9","10","11",12"

My goal is push this into an associative array like such:
    "id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"

My question is, the data loops through for every 4 entries. In the example above, I should have 3 arrays created like such:
    "id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"
    "id"=>"5", "dealer_id"=>"6", "vin"=>"7", "stockno"=>"8"
    "id"=>"9", "dealer_id"=>"10", "vin"=>"11", "stockno"=>"12"

How can I make this happen within PHP -- if possible at all?

Here's one approach:
<?php
$file = fopen('data.txt', 'r');
$headers = fgetcsv($file);
$result = array();
while ($row = fgetcsv($file)) {
        if (!$row[0]) continue;
        $nextItem = array();
        for ($i = 0; $i < 4; ++$i) {
                $nextItem[$headers[$i]] = $row[$i];
        }
        $result[] = $nextItem;
}
fclose($file);
var_dump($result);

This uses fgetcsv to read each delimited row of the file. The first row is used as the header names. Then, for each other row, we create a new entry and match the indexes to the header names. I set it to 4 because you have a trailing delimiter that creates an empty entry at the end, if you removed those from the input you could use the length of $headers for the inner loop.
Here is the result of the var_dump for your data:
array(3) {
  [0]=>
  array(4) {
    ["id"]=>
    string(1) "1"
    ["dealer_id"]=>
    string(1) "2"
    ["vin"]=>
    string(1) "3"
    ["stockno"]=>
    string(1) "4"
  }
  [1]=>
  array(4) {
    ["id"]=>
    string(1) "5"
    ["dealer_id"]=>
    string(1) "6"
    ["vin"]=>
    string(1) "7"
    ["stockno"]=>
    string(1) "8"
  }
  [2]=>
  array(4) {
    ["id"]=>
    string(1) "9"
    ["dealer_id"]=>
    string(2) "10"
    ["vin"]=>
    string(2) "11"
    ["stockno"]=>
    string(3) "12""
  }
}

PHP CSV to Associative Array with headers


I have a CSV file with headings in both the columns and rows that i need as an associative array and the examples i have tried on SO haven't worked for me.


The closest example i found was CSV to Associative Array but i couldn't print the data, so i must be missing something.
$all_rows = array();

$header = 1;//null; // has header
if (($handle = fopen($csv_file, "r")) !== FALSE) {
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if ($header === null) {
            $header = $row;
            continue;
        }
        $all_rows[] = array_combine($header, $row);
    }

    fclose($handle);
}

// CSV Example
// [FOOD][IN_Stock][On_Order]
// [Apples][1302][2304]
// [Oranges][4356][335]

foreach ($all_rows as $key => $value) {

    // This prints ok based on the row number
    echo $all_rows[$key]['Instock'];

    // But i need to be able to call data based on the Food Type
    echo $all_rows['Apples']['In_Stock'];
    echo $all_rows['Apples']['On_order'];
    echo $all_rows['Oranges']['In_Stock'];
    echo $all_rows['Oranges']['On_order'];

}

I can print the data by calling the row number, but the data will not always be the same and therefore need to print based on the headers in the first column.
Added the printed Array as Requested:
Array
(
[0] => Array
    (
        [Food] => Apples
        [In_Stock] => 6329
        [On_Order] => 375
    )

[1] => Array
    (
        [Food] => Pears
        [In_Stock] => 3329
        [On_Order] => 275
    )

[2] => Array
    (
        [Food] => Oranges
        [In_Stock] => 629
        [On_Order] => 170
    )
  )

Encase anyone else needs it, this is what i used:
$csv = array();
foreach ($all_rows as $row) {
if($row) {
    foreach($row as $key => $val) {

        $csv[$val]['In_Stock'] = $row["In_Stock"];
        $csv[$val]['On_order'] = $row["On_order"];

    }
}
}


You need a second foreach to achieve what you want.
Something like this:
foreach ($all_rows as $row) {
    if($row) {
        foreach($row as $key => $val) {
            if($key == "Food" && $val == "Apples") {
                echo "Apples: " . "<br>";
                echo $row["In_Stock"] . "<br>";
                echo $row["On_order"] . "<br>";
            }
            else if($key == "Food" && $val == "Oranges") {
                echo "Oranges: " . "<br>";
                echo $row["In_Stock"] . "<br>";
                echo $row["On_order"] . "<br>";
            }
        }
    }
}

If you just want to print the values on your page, this could do it.
But I since you've mentioned to just call the values based on food type, you can create a function out of this.

Friday, 5 June 2015

PHP: Requires a file called counter1.dat in the same folder as the script

Requires a file called counter1.dat in the same folder as the script
CHMOD the counter file to 755
<?php
//simple counter example v1.0
//open the counter file in read only mode
$counterfile = "counter1.dat";
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");
//read the value stored in the file
$thecount = (int) fread($fp, 20);
//close the file
fclose($fp);
//increment the count
$thecount++;
//display the count
echo "visitor no : $thecount";
//open the file again in write mode and store
// the new count
$fp = fopen($counterfile, "w");
fwrite($fp , $thecount);
//close the file
fclose($fp);
?>

PHP: Display stock quotes from a CSV file, in this case Microsoft

<?php
//stock quote script
//this is the url for Microsoft's stock quote , we are opening it for reading
$fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s=msft&f=sl1d1t1c1ohgv&e=.csv","r");
//this uses the fgetcsv function to store the quote info in the array $data
$data = fgetcsv ($fp, 1000, ",")
?>
<!-- this is our table which displays the stock info -->
<!-- we access the individual items by using $data[0]-->
<table>
<tr><td>description</td><td>latest figure</td><tr>
<tr><td>symbol</td><td><?php echo $data[0] ?></td></tr>
<tr><td>last price</td><td><?php echo $data[1] ?></td></tr>
<tr><td>date</td><td><?php echo $data[2] ?></td></tr>
<tr><td>time</td><td><?php echo $data[3] ?></td></tr>
<tr><td>change</td><td><?php echo $data[4] ?></td></tr>
<tr><td>open</td><td><?php echo $data[5] ?></td></tr>
<tr><td>high</td><td><?php echo $data[6] ?></td></tr>
<tr><td>low</td><td><?php echo $data[7] ?></td></tr>
<tr><td>volume</td><td><?php echo $data[8] ?></td></tr>
</table>
<?php
//close the filehandle $fp
fclose ($fp);
?>

Tuesday, 2 June 2015

PHP: Image to base64 string

<title>Image to Base64 String</title>
<fieldset>
    
<legend>Image to Base64 String</legend>
        
<center>
        
<form name="select_all">
                
 <?php 
                
/** 
 *   This code will help you to learn how we can convert an image into a base64 string!! 
 */ 
                    
echo"<h3><p>Image</p></h3>"; 
                    
//$file = File Image yang ingin di encode  
                    //Filetype: JPEG,PNG,GIF 
                    
$file "encode.jpg"; 
                    if(
$fp fopen($file,"rb"0)) 
                    { 
                    
$gambar fread($fp,filesize($file)); 
                    
fclose($fp); 

                     
                    
$base64 chunk_split(base64_encode($gambar)); 
                    
//Result 
                    
$encode '<img src="data:image/jpg/png/gif;base64,' $base64 .'" >'; 
                    echo 
$encode; 
                    }     
                
?> 

                
<br><textarea name="text_area" rows="20" cols="70">
 <? echo $encode?> </textarea>
                
<p><input type="button" value="Select All Code" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();"></p>
        
</form>
        
</center>
    
</fieldset>

Monday, 23 February 2015

PHP: Generate CSV file from a PHP array

Here is a simple but efficient function to generate a .csv file from a PHP array. The function accept 3 parameters: the data, the csv delimeter (default is a comma) and the csv enclosure (default is a double quote).
<?php
function generateCsv($data, $delimiter = ',', $enclosure = '"') { 
$handle = fopen('php://temp', 'r+'); 
foreach ($data as $line) { 
fputcsv($handle, $line, $delimiter, $enclosure);
 } 
rewind($handle); 
while (!feof($handle)) { 
$contents .= fread($handle, 8192); 
}
 fclose($handle); 
return $contents; 
}
?>

Saturday, 4 October 2014

fwrite in PHP

fwrite — Binary-safe file write

Syntax:

int fwrite ( resource $handle , string $string [, int $length ] )
fwrite() writes the contents of string to the file stream pointed to by handle.

Parameters:

handle
A file system pointer resource that is typically created using fopen().

string
The string that is to be written.

length
If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

Note that if the length argument is given, then the magic_quotes_runtime configuration option will be ignored and no slashes will be stripped from string.

Return values: fwrite() returns the number of bytes written, or FALSE on error.



Note:
Writing to a network stream may end before the whole string is written. Return value of fwrite() may be checked:
<?php
function fwrite_stream($fp, $string) {
    for ($written = 0; $written < strlen($string); $written += $fwrite) {
        $fwrite = fwrite($fp, substr($string, $written));
        if ($fwrite === false) {
            return $written;
        }
    }
    return $written;
}
?>
Note:
On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.
Note:
If handle was fopen()ed in append mode, fwrite()s are atomic (unless the size of string exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to flock() a resource before calling fwrite(); all of the data will be written without interruption.
Note:
If writing twice to the file pointer, then the data will be appended to the end of the file content:
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

// the content of 'data.txt' is now 123 and not 23!
?>


Example #1 A simple fwrite() example

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>
fputs — Alias of fwrite
This function is an alias of: fwrite().