Solved

Create multiple files from one files

Posted on 2016-09-05
8
27 Views
Last Modified: 2016-09-06
     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?
0
Comment
Question by:Jasmine Ikhreishi
  • 3
  • 3
  • 2
8 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 41784714
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.
1
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41784719
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41784799
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!
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 41784806
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41784815
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

1
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41784824
Sorry, I've jut realized your code is wrong:
fwrite($filename, $new_file['content']);

Open in new window

should be:
fwrite($myfile, $new_file['content']);

Open in new window


But give a different filename to your files. I used thi code:
$new_files = array(
		array(
				"name" => "3650.5010.16231a_4",
				"content" => "IS*00*          *00* 
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj      
gfhfidfioghio
dfgjkhguidf
kfjghjdgfuighuigt
489764896hj"),
		array(
				"name" => "3650.5010.16231a_5",
				"content" => "IA5765897
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh
dghertughfh
457885639057
764769487634
457659639486
fhfidhfh"
		)
);

foreach ($new_files as $new_file) {
	$filename =  $new_file['name'].'.txt';
	//create file
	$myfile = fopen($filename, "w");
	//put contents in the file               	
	fwrite($myfile, $new_file['content']);
	//close the file
	fclose($myfile);
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41786064
@Jasmine: Did you test the code posted here?  What was wrong with the answer?
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41786068
It wasn't wrong but I just had to changed my variable for my code to work
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now