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
Solved

Create multiple files from one files

Posted on 2016-09-05
8
47 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 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 109

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 109

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 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 109

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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