Avatar of Jazzy 1012
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);
                }

Open in new window


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?
PHP

Avatar of undefined
Last Comment
Jazzy 1012

8/22/2022 - Mon
Marco Gasi

What is the criterion to split the original file in other files? Or you just want to replicate the whole content of the original file in several different files?

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.
Jazzy 1012

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
Ray Paseur

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!
Your help has saved me hundreds of hours of internet surfing.
fblack61
Marco Gasi

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.
Ray Paseur

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;
}

Open in new window

ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

@Jasmine: Did you test the code posted here?  What was wrong with the answer?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jazzy 1012

ASKER
It wasn't wrong but I just had to changed my variable for my code to work