A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP write to file - page 1
User InfoPosts
PHP write to file#1
below is some code I am using to "translate" a map array into SQL code so I can easily update my database when I have updated my game map. As you can see it prints out the SQL code onto the screen so I can copy and paste it.

As my maps will get bigger this will become inefficient as it will crash the browser due to mass output, so instead I am wondering if it is possible to make it create a .txt file and write all of the data to it instead of printing onto the screen?

if (isset($_POST[(code(])){
$map = $_POST[(code(];
$map = preg_replace("/,\\s*}/i", "}", $map);
$map = str_replace("{", "[", $map);
$map = str_replace("}", "]", $map);
$map = json_decode(([( . $map . (]();

$arrayCount1 = 0;
$arrayCount2 = -1;

$H = sprintf((%05d(, 00000);
$V = sprintf((%05d(, 00000);
$id = 1;

echo "INSERT INTO `map` (`id`, `horizontal`, `verticle`, `image`) VALUES" . "<br />";

for ($count1 = 0; $count1 < sizeof($map[0]); $count1++){
$arrayCount1 = 0;
$V = sprintf((%05d(, $V + 1);
$H = sprintf((%05d(, 00000);

for ($count2 = 0; $count2 < sizeof($map); $count2++){
echo "(" . $id . ", (" . $H . "(, (" . $V . "(, (" . $map[$arrayCount1][$arrayCount2] . "()," . "<br />";
$H = sprintf((%05d(, $H + 1);

posted date: 2009-04-10 03:59:00

Re: PHP write to file#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 03:59:01

Re: PHP write to file#3
write all the lines and then send the file to the client.Check this post for further instructions

posted date: 2009-04-10 04:03:00

Re: PHP write to file#4
$str = <<<your string comes here>>>if( $fh = @fopen( "myfile.txt", "a+" ) ) { fputs( $fh, $str, strlen($str) ); fclose( $fh );}this should do...

posted date: 2009-04-10 04:04:00

Re: PHP write to file#5
If this is something you plan on writing on a regular interval or by different scripts, look at using flock() to lock the file and prevent data corruption. $fp = fopen("/tmp/lock.txt", "w+");if (flock($fp, LOCK_EX)) { // do an exclusive lock fwrite($fp, "Write something here\n"); flock($fp, LOCK_UN); // release the lock} else { echo "Couldn(t lock the file !";}fclose($fp);

posted date: 2009-04-10 04:18:00

Re: PHP write to file#6
+my 2 cents:You may check your database servers mass data loading features, as most of them can load files in batch faster than performing thousands of inserts.

posted date: 2009-04-10 04:21:00

Re: PHP write to file#7
There is an even simpler approach:ob_start();# Your code here ...file_put_contents((yourfile.txt(, ob_get_clean());

posted date: 2009-04-10 04:39:00

Re: PHP write to file#8
note that there are three other similar answers here... ob_start and fwrite goes in the category "stream to file" while this is "push to file" all at once, thus better for dumping. the "lock" answer is the safest one, tho, but most likely overkill for most situations. something like this should be the preferable one, if you see it like that.

posted date: 2010-04-12 08:30:00

Re: PHP write to file#9
definetly! the question (from the title) indeed is already answered by others here. but to the OP actual issue it might be better to get another approach. dumping a TXT file because it's too big for the browser may not be the best one.

posted date: 2010-04-12 08:33:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.