Questions tagged [file-put-contents]

`file_put_contents()` is a PHP function which writes a string to a file.

file_put_contents() is a PHP function, introduced in PHP 5.1.0, which writes a string to a file. It was added as a shortcut and calling this function is identical to calling fopen(), fwrite(), and fclose() successively to write data to a file.

If the file attempting to be written to does not exist, the default behavior of the function is to create the file.

For more information on this function, please see the PHP manual: https://php.net/manual/en/function.file-put-contents.php

245 questions
1
vote
2 answers

How to use foreach loop to save $_POST to text file

When submit a form, I want to write the form inputs to a text file, including a 2 columns of a html table: function toString() { $filename = 'D:\file_' . time() . '_' . $_SESSION['username'] . '.txt'; $data = 'grid_blocks=' .…
user10695289
1
vote
1 answer

Angular POST to PHP: file_put_contents not writing/wrong submit order

I'm using angular http to post data from my Ionic app to a PHP script. In that script a json file gets read and decoded, so that the incoming data can be appended and then be written back to the file. This basically works fine, but in some cases…
GoYoshi
  • 253
  • 1
  • 3
  • 17
1
vote
1 answer

Async file_put_contents/file_get_contents?

Is there a way to download file asynchronously with php? Found this answer but seem like it's not possible to download file using this method The problem is that I'm need to download file from API but it took a while to download all images and URL…
Splinteer
  • 1,126
  • 4
  • 13
  • 28
1
vote
0 answers

performance of two file fopen vs one big file fopen

i am trying to write in txt file and my data are comma separated numbers which are appended every time. i have two types of data which i am trying to compare so if we are using one txt file then it has to be written in new line . …
Steeve
  • 423
  • 2
  • 9
  • 23
1
vote
3 answers

Write JSON data to text file with PHP

Problem: I have a script that send JSON data to a PHP file in this way: var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", "process-survey.php"); xmlhttp.setRequestHeader("Content-Type",…
kexxcream
  • 5,873
  • 8
  • 43
  • 62
1
vote
5 answers

How to replace a specific value in a hard-coded array inside of a php file?

I use a config.php file that returns an array. Before releasing the project, I usually manually change the 'apiKey' value that I use while developing in the file. I forget to perform this replacement sometimes, so I'm looking for a programmatical…
1
vote
3 answers

PHP - appending JSON to existing file inside new category

Please, looking for little push over my problem. Having this script which append data to my existing JSON. What I am looking is: when I append new data it is create new $key (category) and set it all to JSON. Better to show on example. This is my…
x-magix
  • 2,623
  • 15
  • 19
1
vote
1 answer

PHP flock(): can I use it around file-get- and file-put-contents (read-modify-write)

I want to do a read-modify-write to a file whilst I have an exclusive lock. Clearly I can use flock() and do the sequence fopen, flock, fread, fwrite, flock, fclose, but I my code would look neater if it I could use file_get_contents and…
D.Gibson
  • 79
  • 6
1
vote
0 answers

Keeping Automatic Log of one week Transaction in PHP

Case: I have the following query for the INSERT Operation as: $sql = "BEGIN -- pkg_transaction.set_transaction('" . $_SESSION ['user'] . "', 'INSERT EMPLOYEES'); -- insert into employees…
Dixon Chaudhary
  • 321
  • 1
  • 7
  • 21
1
vote
2 answers

php file_get_contents loop all files in same directory

I'm working on a simple comments removal script in PHP. What I'd like to do is loop this entire script for all the files in the same directory. At this point I'm not concerned with subdirectories or subfolder. This example only has 1 file. I know…
Viktor
  • 517
  • 5
  • 23
1
vote
2 answers

insert text after specified string using php

I want to add text to a file after specified string using PHP. For example, I want to add the word 'ldaps' after #redundant LDAP { string I used this code without result: $lines = array(); foreach(file("/etc/freeradius/sites-enabled/default") as…
M.Bwe
  • 159
  • 2
  • 4
  • 16
1
vote
1 answer

PHP Shuffle Text File and Rewrite Using File_Put_Contents

I've been trying for two days to create an array from a text file list, shuffle it, then rewrite the text file using file_put_contents. I've succeeded in doing so BUT when I run the script more than once it creates multiple and random spaces between…
user2921896
1
vote
0 answers

file_put_content is not working in nginx server

I tried to store my image file in nginx server. However, the file_put_content keep return me false. I have tried to set the chmod 777 but It is still not worked so I don't think it is a permission issue. This is my php code how I tried to store the…
phoon
  • 369
  • 1
  • 6
  • 21
1
vote
1 answer

PHP saves broken image files downloaded from a URL

No matter which function I'm using: copy("http:" . $imglink, "images/" . substr($imglink, 34)); //or file_put_contents("images/" . substr($imglink, 34), file_get_contents("http:" . $imglink)); //or file_put_contents("images/" .…
1000Gbps
  • 1,455
  • 1
  • 29
  • 34
1
vote
2 answers

Remove from multidimensional array if has seen id before in an basic array

I would like in php to stop duplicate messages by logging msgid to a text file using something like this file_put_contents("a.txt", implode(PHP_EOL, $array1), FILE_APPEND); and then converting it back to an array using $array1 = file("a.txt"); I…