Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/40644-creating-files-question/
Share on other sites

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.

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']

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.

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

 

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

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.

 

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

 

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, "&nbsp (" );
    echo "&nbsp &#40";
    fwrite ( $fp, $body[0][$i] );
    echo $body[0][$i];

    touch("as".$body[0][$i].".php");

    fwrite ( $fp, ") $nbsp" );
    echo ") &nbsp";
    $i++;}
   
   fclose ( $fp );

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. 

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.