bpross
bpross

Reputation: 353

Checking if multiple files exsist if so create another

I'm having a problem trying to figure out a way to check for a file and if it exists check the next and so on.

I'm not familiar enough with loops so looking for help.

I'm putting together a small set of php forms to help me do rental inspections. The form will create a page for each area/item for the inspection with a photo and description of the problem if any. The form for the photo's is already working and not part of this.

I'm matching the paper form they will have me use. This could save me an hour or so on the windoze spreadsheet they would want me to put everything in and then print to PDF.

My laptop/PC is Linux. Would also save me having to get a Win machine or tablet.

I have everything else working. Just the page creation is giving me fits. I understand a loop should be the easiest to save having to write the file_exists search for each page up to 40 pages.

Here is a snip of were I'm at. The location this is sitting is not publicly accessible.

Thanks in advance Bob

<?php

// This will be accessed for each area/item inspected

$dirpath = "/localhost/rent.inspect/";

// Get POST info from form page

$a1 = $_POST["a1"];
$a2 = $_POST["a2"];
$a3 = $_POST["a3"];
$a4 = $_POST["a4"];
…...
$a40 = &_POST[“a40”];

// File names we write to can be any name
$FileName1 = "$dirPath/page1.php";
$FileName2 = "$dirPath/page2.php";
$FileName3 = "$dirPath/page3.php";
$FileName4 = "$dirPath/page4.php";
…...
$FileName3 = "$dirPath/page39.php";
$FileName4 = "$dirPath/page40.php";

// Check if the first file is already created.
// If not create it and write, if is does exist check for the
// next file. Keep checking until one not created is found.
//  Should never get to the 40th file at this time.

// Check if first file has already been created.
if(file_exists("$FileNam1"))
    {
      if(file_exists("$FileNam2"))
        {
          if(file_exists("$FileNam3"))
            {
              // Check for next one.... Should never see 40
              // but keep checking to it just in case something is added
            }
            else
               {
                 $myfile = fopen("$dirPath/$filename3", "w") or die("Unable to open file!");
                 $txt = "<font size=\"2\">$a1 $a2 $a3</font></b><br /><br />";
                 fwrite($myfile, $txt);
                 fclose($myfile);
               }
        }
        else
           {
              $myfile = fopen("$dirPath/$filename2", "w") or die("Unable to open file!");
              $txt = "<font size=\"2\">$a1 $a2 $a3</font></b><br /><br />";
              fwrite($myfile, $txt);
              fclose($myfile);
           }

    }
    else
       {
         $myfile = fopen("$dirPath/$filename1", "w") or die("Unable to open file!");
         $txt = "<font size=\"2\">$a1 $a2 $a3</font></b><br /><br />";
         fwrite($myfile, $txt);
         fclose($myfile);
       }
?>

Upvotes: 2

Views: 105

Answers (2)

Cl&#233;ment Malet
Cl&#233;ment Malet

Reputation: 5090

Several ways to do so, an easy one fitting your current problem could be :

for ($i = 1; $i < 41; ++$i) {
    if (!file_exists($dirPath . '/page' . $i . '.php')) {
        // fopen, fwrite, fclose ...
        break;
    }
}

You could also improve your variable initializations using an array to store your variables, even more when it's all about changing an increment integer.

Here is an example, not really useful, but explaining how you could do :

for ($i = 0; $i < 41; ++$i) {
    $myVar['a' . $i] = $_POST['a' . $i];
}

Upvotes: 1

Dhiraj Wakchaure
Dhiraj Wakchaure

Reputation: 2706

you can check till the file exixts and increment the counter

$filepath = "/somepath/";
$filename  = "FileNam"
$i=1;

$pathtocheck = $filepath + $filename  + $i;
while ( file_exists ($pathtocheck ))
{
$i++
$pathtocheck = $filepath + $filename  + $i;

}

// your code for file write will be here 
// this code will check is there file exist if not while will break otherwise it will continue till no file like FileNam1 ,FileNam2 and so on ...

Upvotes: 0

Related Questions