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
2
votes
2 answers

PHP: file_put_contents and writing to multiple files in a for loop not working

For some background I have a bit of code that I am using to generate html files that contain a link to a unique code. Ultimately this ends up on a USB drive thats distributed to customers. This is done in batch so i can create as many custom files…
Ray
  • 82
  • 7
1
vote
3 answers

create a y-m-d.json file in the respective directory (year/month) by iterating multidimensional array

try to create or update a json file based on the processed data. first I arrange the contents using assamble() and create a multidimensional array: $logfile_accepted_array = [ "AAAA 2022 12 23 21:37:56 dc:16:b2:4c:d2:e6", "BBB 2023 01 12…
Cem Firat
  • 390
  • 2
  • 21
1
vote
1 answer

Can't write to a remote server location with PHP but I can browse and write to it in File Explorer?

Sorry if this a repeat of a question that has been asked before but I have not been able to find my exact situation. We are trying to migrate our website server from a Windows 10 VM (yes I know) to a Windows Server 2019 VM. We have some PHP on our…
Mike
  • 31
  • 5
1
vote
1 answer

How to check contents of one list with multiple lists

Here is a list created from YAML file. I want to compare the following contents of each memservers with other merserves in the list: If the rpc_interface is same. If rpc_interface is same, is fam_path same? If rpc_interface is same, is…
Vsdm in
  • 25
  • 4
1
vote
0 answers

php str_replace returns zero value

I want to add some lines to many css files in different folders. everything is ok. but str_replace function returns zero value when write is with file_put_contents. This is my code: $from = '@media (max-width: 767px) {'; $to = '.st'.$folder.' {}'.…
Jahan Web
  • 23
  • 3
1
vote
1 answer

Windows USB Serial CMD call using "mode com3: BAUD=38400... instead of PHP call

I asked a previous question about a PHP call no longer working on when Win10 upgraded to Win 11. It now strangely writes to a file rather than to the set COM port (may be a win11 apache/php issue, see my other question?). The code that used to work…
aku foo
  • 53
  • 6
1
vote
2 answers

Using file_put_contents inside of a foreach loop only downloads the last item

I am trying to download a large list of mp4 files by looping through them and using file_put_contents() to save to a directory. The problem is that only the last item in the video list is getting downloaded. Here is my code:
GLevy
  • 21
  • 4
1
vote
3 answers

unlink after file_put_contents

I am trying to detect status of invoice from a json file, then if the status is a confirmed payment, update the status and write the json to a new location, then unlink the existing json location.
Bruce
  • 1,039
  • 1
  • 9
  • 31
1
vote
1 answer

how to parse   into txt file with file_put_contents

I am trying to parse a space form an input field in a txt file with file_put_contents but i do not succeed in it with htmlentities.
john
  • 1,263
  • 5
  • 18
1
vote
1 answer

Crazy PHP fopen / fwrite / file_put_contents / move_uploaded_file bug with binary mode writing on CentOS Linux Adding 0x0D to every 0x0A

Seems the file keeps adding 0x0D to my saved binary files no matter what setting I put in the fopen mode flag. It's not only affecting fopen/fwrite.. but also file_put_contents. file_get_contents I thought was the problem to begin with.. but it…
SSpoke
  • 5,656
  • 10
  • 72
  • 124
1
vote
1 answer

Weird issue with PHP file_put_contents()

Using PHP v5.6.40 under Ubuntu 18.04.04 (through php5.6 apache mod), this is the line of code: file_put_contents('/tmp/deploy.hook.log', date('Y-m-d H:i:s') . ': ' . $data . "\n", FILE_APPEND); It works in every other server / environment I have…
Emanuel
  • 43
  • 6
1
vote
1 answer

PHP - How to process/save weekly files

I need to process a file by adding to it, but then saving as separate weekly files. It is easy to do monthly, which I am doing as follows: // Set variables $month = date('m'); $year = date('Y'); // Load previous file content $newreportfile =…
omega1
  • 1,031
  • 4
  • 21
  • 41
1
vote
1 answer

What happens when two scripts want to write at the same time on a file with LOCK_EX?

What happens if a PHP script wants to: file_put_contents("testfile", $s, FILE_APPEND | LOCK_EX); while another script already does the same thing on the same file (with a LOCK_EX too)? It's unlikely to happen that 2 scripts want to write exactly…
Basj
  • 41,386
  • 99
  • 383
  • 673
1
vote
1 answer

fopen($filename, "a+") nor file_put_contents($filename, = $data.PHP_EOL, FILE_APPEND) seem to append to file

Trying to append file with either: file_put_contents($filename, "\r\n" . $barcode_number.PHP_EOL, FILE_APPEND); or $myfile = fopen($filename, 'a+'); neither appending my file. Both are over writing old information Things I have tried are in code…
unko19
  • 13
  • 2
1
vote
1 answer

file writes in php for logging is that a performance blocker

I have a piece of code in most of my functions for logging purpose now on production and we see a logs files of 2gb per day is it okay to write directly to log files using file_put_contents. We are speculating this file_get_contents will become a…
jision
  • 181
  • 2
  • 12
1 2
3
16 17