Jazzy 1012
asked on
Create multiple files from one files
foreach($new_files as $new_file) {
//create file
$myfile = fopen($filename, "w");
//put contents in the file
fwrite($filename, $new_file['content']);
//close the file
fclose($myfile);
}
I have this code to create a new file, I want to be able to open $filename, and create multiple files from it
This code doesnt seem to work, any ideas?
ASKER
When I print_r() new files , I get the content i did for example:
Array
(
[0] => Array
(
[name] => 3650.5010.16231a_4
[content] => IS*00* *00*
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj
[1] => Array
(
[name] => 3650.5010.16231a_5
[content] => IA5765897
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh
Thats how I want it but each array in a seperate file, however thats not how it comes out
Array
(
[0] => Array
(
[name] => 3650.5010.16231a_4
[content] => IS*00* *00*
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj
[1] => Array
(
[name] => 3650.5010.16231a_5
[content] => IA5765897
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh
Thats how I want it but each array in a seperate file, however thats not how it comes out
Please use var_export() to print out variables that you want us to work with. Then you will get a code representation that you can post in the code snippet here at E-E. Much more useful than print_r()! This will save us (and you) a lot of time!
Okay, now I understand. I suppose you're creating only one file: if this is the case, the problem is that you foreach file you use the same file name ($filename): so you are overwriting the same file content.
Give me a minute to test.
Give me a minute to test.
Please see: https://iconoun.com/demo/temp_jasmine_4.php
<?php // demo/temp_jasmine_4.php
/**
* https://www.experts-exchange.com/questions/28967752/Create-multiple-files-from-one-files.html
*/
error_reporting(E_ALL);
echo '<pre>';
$data = Array
( 0 => [
'name' => '3650.5010.16231a_4'
, 'content' => 'IS*00* *00*
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj' ]
, 1 => [
'name' => '3650.5010.16231a_5'
, 'content' => 'IA5765897
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh' ]
)
;
// DOES IT LOOK RIGHT? YES
var_export($data);
echo PHP_EOL;
// THE DIRECTORY TO HOLD THE FILES
$dir = 'storage';
// WRITE ONE FILE FOR EACH INTERNAL ARRAY
foreach ($data as $thing)
{
$path = $dir . DIRECTORY_SEPARATOR . $thing['name'];
file_put_contents($path, $thing['content']);
}
// SHOW LINKS SO WE CAN VERIFY THE CONTENT OF THE FILES
foreach ($data as $thing)
{
$path = $dir . DIRECTORY_SEPARATOR . $thing['name'];
$link = '<a target="_blank" href="' . $path . '">' . $thing['name'] . '</a>' . PHP_EOL;
echo $link;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Jasmine: Did you test the code posted here? What was wrong with the answer?
ASKER
It wasn't wrong but I just had to changed my variable for my code to work
In general, if you want to split a file in several smaller files you can use file() function to create an array from a file: each line will become an array item; then you can iterate through the array to get content blocks and save them in another file following your specific criteria.