[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Create multiple files from one files

Posted on 2016-09-05
8
Medium Priority
?
58 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
Industry Leaders: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

656 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