All4172 Posted March 1, 2007 Share Posted March 1, 2007 When I use this: $i = 0; while ($i < 10) { echo $body[0][$i];} It'll return 0 1 2 3 4 5 6 7 8 9 Now I would like it to create files for each. Like 0.php and 1 .php ect... So I tried this: $i = 0; while ($i < 10) { touch $body[0][$i]".php";} However, this returns Array[0].php Array[1].php ect. Any ideas how I can arrange it to create the files in the style 0.php 1.php ect? Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/ Share on other sites More sharing options...
MadTechie Posted March 1, 2007 Share Posted March 1, 2007 you missed a dot <?php $i = 0; while ($i < 10) { touch $body[0][$i].".php"; } ?> try <?php for ($n=0, $n<10, $n++) { $ourFileHandle = fopen($n, 'w') or die("can't open file"); fclose($ourFileHandle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-196597 Share on other sites More sharing options...
All4172 Posted March 2, 2007 Author Share Posted March 2, 2007 When I try: <?php $i = 0; while ($i < 10) { touch $body[0][$i].".php"; } ?> Nothing is created. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-197945 Share on other sites More sharing options...
pkSML Posted March 2, 2007 Share Posted March 2, 2007 Try this: <?php for ($n=0, $n<10, $n++) { $filename = $n.".php"; $ourFileHandle = fopen($filename, 'w') or die("can't open file"); fwrite($ourFileHandle, "add file contents here"); fclose($ourFileHandle); } ?> Just added a little bit to MadTechie's code above. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-197955 Share on other sites More sharing options...
All4172 Posted March 2, 2007 Author Share Posted March 2, 2007 I'm trying to create a new file, and not trying to open an existing file. So I have to use touch in order to create it. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-197958 Share on other sites More sharing options...
pkSML Posted March 2, 2007 Share Posted March 2, 2007 I'm trying to create a new file, and not trying to open an existing file. So I have to use touch in order to create it. Touch does not create files --> "touch -- Sets access and modification time of file". The fopen function will create a file is it does not already exist due to the 'w' argument. It will write to the filename. About the 'w' argument in fopen: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist' date=' attempt to create it.[/u'] Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198036 Share on other sites More sharing options...
All4172 Posted March 2, 2007 Author Share Posted March 2, 2007 Touch does not create files --> "touch -- Sets access and modification time of file". According to my book it says "If a file does not yet exist, you can create one with the touch() function. Given a string representing a file path, touch() attempts to create an empty file of that name." I also know touch does that since it works when I have: $filename = "test.php"; if (!file_exists($filename)) { touch($filename); chmod($filename,0777); The code you offered just returns a "can't open file". So its not creating a file. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198066 Share on other sites More sharing options...
Adika Posted March 2, 2007 Share Posted March 2, 2007 Hi! Your first code has a BIG BEGINNER ERROR!!! You didn't put "()" in your touch statement! Here is your wrong script: <?php $i = 0; while ($i < 10) { touch $body[0][$i].".php"; } ?> Here is the right code: <?php $i = 0; while ($i < 10) { touch($body[0][$i].".php"); } ?> Do you see the difference? Is it working now? All the best, Adika Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198093 Share on other sites More sharing options...
All4172 Posted March 2, 2007 Author Share Posted March 2, 2007 Do you see the difference? Is it working now? All the best, Adika Nope...when I have it as: touch("$body[0][$i].php"); It creates files names such as Array[0].php ect. When I try it as: touch($body[0][$i].".php"); Nothing is created still.... Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198171 Share on other sites More sharing options...
Adika Posted March 2, 2007 Share Posted March 2, 2007 Try this: touch("".$body[0][$i].".php"); Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198175 Share on other sites More sharing options...
All4172 Posted March 2, 2007 Author Share Posted March 2, 2007 Try this: touch("".$body[0][$i].".php"); Still no luck. I also tried: touch("ab".$body[0][$i].".php"); No luck with that one either. Very strange... Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198185 Share on other sites More sharing options...
Adika Posted March 2, 2007 Share Posted March 2, 2007 Hi! If you want only to create files like 0.php, 1.php etc., you can use only this: <?php $i = 0; while ($i < 10) { touch($i.".php"); $i++; } ?> You complicated the whole thing with that $body[0] thing. All the best, Adika Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198190 Share on other sites More sharing options...
Adika Posted March 2, 2007 Share Posted March 2, 2007 One more thing! Your first code: $i = 0; while ($i < 10) { echo $body[0][$i];} Does not return 0, 1, 2, 3, 4, 5, 6, 7, 8, 9! It is an infinite loop! You need an $i++; to be placed under the echo statement and before the closing bracket. Than, the script will return those numbers. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198191 Share on other sites More sharing options...
jcbarr Posted March 2, 2007 Share Posted March 2, 2007 Nope, it is not an infinite loop. At least I don't believe so. $i increments with every addition to the array, so when it reaches the 10th element ($i=9) it will stop as after that $i will no longer be less than 10. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198195 Share on other sites More sharing options...
Adika Posted March 2, 2007 Share Posted March 2, 2007 Hi jcbarr! If you don't believe it is an infinite loop, why don't you try to run this script on your server?? It is an infinite loop! I tried! Why is it an infinite loop? Because, $i does not increment with every addition to the array. By the way, echo $body[0][$i] means: show the first character of the $body variable. If $body[1][$i], then it will be: show the second character of the $body variable. Try out the script, and you will see that you must add $i++; or else, the $i will stay 1 and you will just crush down the server. All the best, Adika Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198204 Share on other sites More sharing options...
All4172 Posted March 2, 2007 Author Share Posted March 2, 2007 I didn't post the whole code, just the part with the problems. Its not an indefinate loops. Also the reason for the muli-dimensional array is that is what you get when you pull data from pret_match_all. I can't use numbers, but want to use the $i portion. It properly echo's the correct values (such as: AMPS, or DMPS ect). I can't use numbers for it either . Here's the entire for statement of code: $filename = "gainers.txt"; if (!file_exists($filename)) { touch($filename); // Create blank file chmod($filename,0777); } $fp = fopen( $filename, 'w'); $i = 0; while ($i < 10) { fwrite ( $fp, $name[0][$i] ); echo $name[0][$i]; fwrite ( $fp, "  (" ); echo "  ("; fwrite ( $fp, $body[0][$i] ); echo $body[0][$i]; touch("as".$body[0][$i].".php"); fwrite ( $fp, ") $nbsp" ); echo ")  "; $i++;} fclose ( $fp ); Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198229 Share on other sites More sharing options...
MadTechie Posted March 3, 2007 Share Posted March 3, 2007 if thats your full code then the problem is $name or $body is not set anyway!! as for the rest of the code thats fine! Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198497 Share on other sites More sharing options...
All4172 Posted March 3, 2007 Author Share Posted March 3, 2007 if thats your full code then the problem is $name or $body is not set anyway!! as for the rest of the code thats fine! Not the problem again. That's the whole area of code that involves the while statement. I'm not gonna post over 1200 lines of code. My question is simple really. How to create a file with a multi dimensional array using touch. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198550 Share on other sites More sharing options...
ted_chou12 Posted March 3, 2007 Share Posted March 3, 2007 When I use this: $i = 0; while ($i < 10) { echo $body[0][$i];} It'll return 0 1 2 3 4 5 6 7 8 9 Now I would like it to create files for each. Like 0.php and 1 .php ect... So I tried this: $i = 0; while ($i < 10) { touch $body[0][$i]".php";} However, this returns Array[0].php Array[1].php ect. Any ideas how I can arrange it to create the files in the style 0.php 1.php ect? I dont quite understand what you are trying to do here, but what you wish to do is to create filenames according to an increment, then I suggest you use a counter txt file, everytime a file is created, reenter a bigger number into the txt file, then use that number to name the php file. Ted Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198570 Share on other sites More sharing options...
All4172 Posted March 3, 2007 Author Share Posted March 3, 2007 I dont quite understand what you are trying to do here, but what you wish to do is to create filenames according to an increment, then I suggest you use a counter txt file, everytime a file is created, reenter a bigger number into the txt file, then use that number to name the php file. Ted Hi Ted, What I'm trying to do is use touch with a multi demensional array. Basically I do a preg_match_all earlier in the script that outputs the data to a multi-dimensional array. I can echo that multi-dimensional array and it works fine. But now I'm trying to take that array and use touch(), and create a file with that name. The value of that multi-dimensional array is always 4 letters. Right now when I use touch with that multi-dimensional array [ touch ($body[0][$i].".php"); ] it creates nothing. However when I just say something like [ touch($body[0][$i]); ] it'll create but it creates it as Array[0] Array[1] ect. So basically I'm trying to find out how to create files with a multi dimensional array with the value of the array that I echo. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198629 Share on other sites More sharing options...
Kasuke_Akira Posted March 3, 2007 Share Posted March 3, 2007 He is correct. It will loop indefinitely because you have no way of incrementing it in the loop. 'i' will stay the same if you don't use "1++;" or "i = i + 1;" It needs to go in after the touch() function, other wise the number will always be 0, and always be less than 10. Quote Link to comment https://forums.phpfreaks.com/topic/40644-creating-files-question/#findComment-198657 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.