Page 1 of 1

Why doesn't this code write the file to the correct folder.

Posted: Fri Jul 02, 2004 1:07 pm
by andylyon87

Code: Select all

<?php
function WriteToFile($firstname){
         $TheFile = "php/form_data/focus/$array[shop_name]/contact.txt";
         $Open = fopen ($TheFile,"a+");
         if($Open){
                   fwrite ($Open,"$firstname");
                   fclose($Open);
                   $Worked=True;
         } else {
           $Worked = False;
          }
          return $Worked;
          }//end of write to file function
?>
Why doesn't this put the file in the correct folder, thought it was strings so used:

Code: Select all

<?php
$TheFile = "php/form_data/focus/".$array[shop_name]."/contact.txt";
?>
As this is usually how you fuse variables with the normal isn't it. the file gets written but in the focus folder. the $array[shop_name] variable has had its own directory created and it appears ok

Posted: Fri Jul 02, 2004 2:20 pm
by redmonkey
$array is not accessable by the function i.e. it is outside the variable scope of the function.

Re: Why doesn't this code write the file to the correct fold

Posted: Fri Jul 02, 2004 3:16 pm
by Calimero

Code: Select all

<?php
$TheFile = "php/form_data/focus/".$array[shop_name]."/contact.txt";
?>

// have you tried / in front of the name of the

php/form_data/focus/".$array[shop_name]."/contact.txt";

Ex

Code: Select all

<?php
$TheFile = "/php/form_data/focus/".$array[shop_name]."/contact.txt";
?>

Posted: Sat Jul 03, 2004 3:39 am
by andylyon87
Doesn't apear to be workin have also tried using if statements :?

Code: Select all

<?php
function WriteToFile($firstname){

         $Open = fopen ($TheFile,"a+");
         if($Open){
                   fwrite ($Open,"$firstname");
                   fclose($Open);
                   $Worked=True;
         } else {
           $Worked = False;
          }
          return $Worked;
          }//end of write to file function
?>

Code: Select all

<?php
function handleform(){
global $array;
$make = mkdir("php/form_data/focus/$array[shop_name]", 0666);
if($make){
if($array[overview]){
$TheFile = "php/form_data/focus/".$array[shop_name]."/overview.txt";
WriteToFile("Overview Of Company:\n \t$array[company]\n");}
if($array[shop_name]){
$TheFile = "php/form_data/focus/".$array[shop_name]."/contact.txt";
WriteToFile("Representatives Name: $array[name]\nCompany Name: $array[shop_name]\nCompany Contact (Tel): $array[contact]\nCompany Contact (E-mail): $array[email]\nWebsite Address: $array[address]\n");}
mail("lyonclan@onetel.net.uk", "new_focus", "$array[shop_name]", "from:$array[name]" );
   print("<TABLE width=450 cellpadding=0 cellspacing=0 border=0>
                 <TR>
                     <TD>Thank you $array[name].</TD>
                 </TR>
          </TABLE>\n");
        }else{
print("directory could not be created for you");
}
}

if($array[beensubmitted]){
                   handleform();
                   }
                   createform();
?>

Posted: Sat Jul 03, 2004 5:44 am
by feyd
andy, you're getting into a real bad habit of not quoting your indexes into the array...

why not pass $array to the function?

Posted: Sat Jul 03, 2004 8:25 am
by andylyon87
I dont understand what your gettin at show me what you mean. And what dyou mean quote my indexes?

Lastly if you mean put $thefile into the function WriteToFile() I already tried that and that doesn't work.

Posted: Sat Jul 03, 2004 8:58 am
by feyd
How about this:

Code: Select all

<?php 
function WriteToFile($TheFile,$firstname){ 

         $Open = fopen ($TheFile,"a+"); 
         if($Open){ 
                   fwrite ($Open,"$firstname"); 
                   fclose($Open); 
                   $Worked=True; 
         } else { 
           $Worked = False; 
          } 
          return $Worked; 
          }//end of write to file function 


function handleform($array){ 
if(!is_array($array)) return false;
$make = mkdir("php/form_data/focus/$array[shop_name]", 0666); 
if($make){ 
if($array['overview']){ 
$TheFile = "php/form_data/focus/".$array['shop_name']."/overview.txt"; 
WriteToFile($TheFile,"Overview Of Company:\n \t$array[company]\n");} 
if($array['shop_name']){ 
$TheFile = "php/form_data/focus/".$array['shop_name']."/contact.txt"; 
WriteToFile($TheFile,"Representatives Name: $array[name]\nCompany Name: $array[shop_name]\nCompany Contact (Tel): $array[contact]\nCompany Contact (E-mail): $array[email]\nWebsite Address: $array[address]\n");} 
mail("lyonclan@onetel.net.uk", "new_focus", "$array[shop_name]", "from:$array[name]" ); 
   print("<TABLE width=450 cellpadding=0 cellspacing=0 border=0> 
                 <TR> 
                     <TD>Thank you $array[name].</TD> 
                 </TR> 
          </TABLE>\n"); 
        }else{ 
print("directory could not be created for you"); 
} 
} 

if($array['beensubmitted']){ 
                   handleform($array); 
                   } 
                   createform(); 
?>

Posted: Sat Jul 03, 2004 12:37 pm
by andylyon87
ok but doesn't the

Code: Select all

<?php
if(!is_array($array)) return false;
?>
lne just translate to

Code: Select all

<?php
global $array;
?>
and why do you use the handleform($array)

Posted: Sat Jul 03, 2004 3:47 pm
by feyd
because it's better to pass data into functions, than use the global system (most of the time)

and no, is_array($array) does not translate into global $array.