?
Solved

Create multiple files from one files

Posted on 2016-09-05
8
Medium Priority
?
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 31

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 111

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

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
 
LVL 111

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 31

Accepted Solution

by:
Marco Gasi earned 2000 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 111

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 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