Jump to content

How to create a text file while runnning PHP program from browser?


Recommended Posts

Hello Friends...

 

I am creating a HTML webpage and connecting with MYSQL.

 

If the client will check the conditions, it will pass it to PHP and get values from MYSQL and print it in the webpage as a HTML table format.

 

Now i want to create a SAVE button and save the HTML TAble values as TXT tab demilited file.

 

I tried of creating a TXT file by using fopen() and write the values by using fwrite() function. But it is working only in the putty. While running the program from the browser, its not working.

 

Will somebody help me to solve this prob?

 

I also was searched for creating a TXT file from HTML table. But i wouldnt get any codes reg this....

 

Thanks

Rose

 

Hello....

I first want to thank u for your reply.

 

I am trying to run the program from IE.

 

Below i have listed the code.

 

U can see the fopen() and fwrite() in getDataFromDB() function.

 

Waiting for your reply.

 

<?php

 

if($_GET["gene"])

{

    $select_species_human =  $_GET['Human'];

    $select_species_mouse =  $_GET['Mouse'];

    $genesymbol = $_GET['genesymbol'];

    $gene =  $_GET['gene'];

    $submit_button =  $_GET['submit'];

   

    connectDatabase();

    //echo "$select_species_human\t$select_species_mouse\n";

   

    //SET THE TAX_ID

    //**************   

    if(strlen($select_species_human)!=0)

        $tax_id = "9606";

    elseif(strlen($select_species_mouse)!=0)

        $tax_id = "10090";

    else

      echo("<SCRIPT LANGUAGE='JavaScript'> window.alert('Choose a Species') </SCRIPT>");

 

 

    if($genesymbol=="genesymbol")

    {

        $value = "genesymbol";

    }

    elseif($genesymbol=="geneid")

    {     

      $value = "geneid";

      //getDataFromDB($tax_id,$value,$gene); 

    }

 

 

?>

 

<!------------------------------------------------------------------------------------------------------------------>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Bioinformatics Biology Service Core</title>

<link rel="stylesheet" type="text/css" href="style.css" />

</head>

 

<body>

<div id="header">

  <br><br><center>

<h2>Bioinformatics Biology Service Core</h2>

<h3>Institute of Molecular Biology</h3>

</center>

</div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="container-top"></div>

<div id="container">

<div id="left">

<div id="left-top"></div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="left-centre">

<h2>Select a Species</h2>

 

<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 

<ul class="menu">

<li><input type="checkbox" name="Human" value="human">Human</li>

<li><input type="checkbox" name="Mouse" value="mouse">Mouse</li>

</ul>

 

<h2>Input details</h2>

                <p>

                  <input type="checkbox" name="genesymbol" value="geneid">

                  GeneID

                  <input type="checkbox" name="genesymbol" value="genesymbol">Gene Symbol</p>

                  Enter Gene ID (or) Name

                  <input type="text" name="gene"/>

                  <br />

                  <input type="submit" value="Submit" name ="submit">

  </form>             

  </div>

<div id="left-bottom"></div>

</div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="right">

<div id="tabs">

<ul>

<li><div class="tab-active"><a href="#"><span>main</span></a></div></li>

<li><div class="tab"><a href="#"><span>latest news</span></a></div></li>

<li><div class="tab"><a href="#"><span>about</span></a></div></li>

<li><div class="tab"><a href="#"><span>contact us</span></a></div></li>

</ul>

               

               

</div>

<div id="right-top"></div>

<div id="right-centre">

<h1>Main page</h1>

<p>

    <!-- <a href="http://192.168.100.152/~rekha/Webpage/result.xls"> SAVE </a>  -->

 

    <?php

   

   

    connectDatabase();

      getDataFromDB($tax_id,$value,$gene); 

     

     

      ?>

   

</div>

<div id="right-bottom"></div>

</div>

<div class="clear"></div>

</div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="container-bottom"></div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="footer-top"></div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="footer-centre">

<p>© 2007 Your Name Here<br />

<a href="http://gregdev.net">Bioinformatics Biology Service Core</a></p>

</div>

<!------------------------------------------------------------------------------------------------------------------>

<div id="footer-bottom"></div>

</body>

 

</html>

 

<!------------------------------------------------------------------------------------------------------------------>

<?php

function myfunction($v1,$v2)

{

    return $v1 .",". $v2;

}

//*************************************************************************************************

function getDataFromDB($tax_id,$value,$gene)

{

    //CODINGS FOR "GENE ID"

    //*********************

    if($value == "geneid")

    {

        $total_gene_id = explode(",",$gene);

        $size_of_total_gene = sizeof($total_gene_id);

             

        $result_gene_id = (array_reduce($total_gene_id,"myfunction"));

       

        $extract_gene_id_list = substr($result_gene_id,1);

        //echo $extract_gene_id_list;

             

        $query = "SELECT *

                  FROM gene2go

                  WHERE (tax_id = '$tax_id') AND

                        (GeneID IN ($extract_gene_id_list))";       

    }

    //CODINGS FOR "GENE SYMBOL"

    //*************************

    else if($value == "genesymbol")

    {

    $total_gene_symbol = explode(",",$gene);

    $size_of_total_gene_symbol = sizeof($total_symbol);

   

        $result_gene_symbol = (array_reduce($total_gene_symbol,"myfunction"));

       

        $extract_gene_name_list = substr($result_gene_symbol,1);

       

        $query = "SELECT a.*

FROM gene2go a,`gene_info` b

WHERE b.`tax_id` =10090 AND

(b.Symbol IN('$extract_gene_name_list') OR b.Synonyms IN ('%$extract_gene_name_list%'))AND

(b.GeneID = a.GeneID)";

        echo $query;

    }

    else

    {

      echo "\nError in choosing the Gene Id or Symbol\n";

    }

$result = mysql_query($query) or die('Error, query failed'.mysql_error());

 

    $fp = @fopen("sample.txt",'w') or die("Could not open sample.txt file");

   

    //echo "<a href='http://192.168.100.152/~rekha/Webpage/result.xls'> SAVE </a>";

echo "<TABLE>";

echo "<TR>";

echo "<TH>TAX_ID</TH>";

echo "<TH>Gene_ID</TH>";

  echo "<TH>Go_ID</TH>";

echo "<TH>Evidence</TH>";

echo "<TH>Qualifier</TH>";

echo "<TH>GO_Term</TH>";

echo "<TH>Pubmed</TH>";

echo "<TH>Catagory</TH>";

echo "</TR>";

 

 

 

 

while($row = mysql_fetch_array($result, MYSQL_BOTH))

{

$tax_id = trim($row[0]);

$gene_id = trim($row[1]);

$go_id = trim($row[2]);

$evidence = trim($row[3]);

$qualifier = trim($row[4]);

$go_term = trim($row[5]);

$pebmed = trim($row[6]);

$catagory = trim($row[7]);

 

  echo "<TR><TD>$tax_id</TD> <TD>$gene_id</TD><TD>$go_id</TD><TD>$evidence</TD><TD>$qualifier</TD><TD>$go_term</TD><TD>$pebmed</TD><TD>$catagory</TD></TR>";

  fwrite($fp,"$tax_id."\t".$gene_id."\t".$go_id."\t".$evidence."\t".$qualifier."\t".$go_term."\t".$pebmed."\t".$catagory."\n");      

 

  }

  echo "</TABLE>";

 

//fclose($fp);

}

//*************************************************************************************************

//CONNECTING WITH THE DATABASE

//*****************************

function connectDatabase()

{

mysql_connect("IPaddress","uname","password") or die("could not connected".mysql_error());

mysql_select_db("NCBI_gene_info") or die(mysql_error());

}

 

 

?>

 

 

Ok, so your code is working on the command line, but not through the webserver. That means php probably doesn't have permissions to write to the folder this file is in.

 

Also, this:

fwrite($fp,"$tax_id."\t".$gene_id."\t".$go_id."\t".$evidence."\t".$qualifier."\t".$go_term."\t".$pebmed."\t".$catagory."\n");

should be

fwrite($fp,$tax_id."\t".$gene_id."\t".$go_id."\t".$evidence."\t".$qualifier."\t".$go_term."\t".$pebmed."\t".$catagory."\n");

 

I corrected it. While clicking the Submit button im getting error "Could not open sample.txt file"'.

 

I heard from one of my friend that if we run from browser, we cant able to write data in the file.

 

I tried a simple program.

<?php

 

 

//OPENING the input FILE

//**********************

$fp = @fopen("sample.txt",'w') or die("Could not open sample.txt file");

 

fwrite($fp,"Testing\n");

fclose($fp);

 

?>

 

<HTML>

 

File open testing

</HTML>

 

 

It is running well in putty but not from IE.

 

Plz help me.....

You have to set the permissions of the folder to let php write to it. Since I don't know what system you have, let's say you have a ubuntu installation. Apache runs under www-data, so you would do this in putty:

chown :www-data /path/to/directory
chmod 775 /path/to/directory

Replace /path/to/directory with whatever folder this script is in.

Try writing the file the directory "/tmp". Everyone should have write permissions to that directory. After the file is created do an "ls -l" on the file and see who the owner is. It's probably not your userid. That is probably where you're getting confused.

 

Ken

 

Hi Ken....

 

I used ls -l for checking the file permission in Linux. I found for user it doesnt has W permission. So i used chmod.

 

 

  
chmod -R 777 *

ls -l

 

Now it is working well............

 

Thanks u friends for ur help.............. :);)

 

 

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.