Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

write to file

Links for php scripts

Moderators: macek, egami, gesf

write to file

Postby mamandis » Thu Apr 26, 2012 3:37 am

ok.. I start learn writing to file script from start.. :)

when I try a script without headers, file creats with $data in a file - ok..
but when I wrote a code to download a file, in a file is written "succeed" only

I want to understand what is wrong :help:

Code: Select all
<?php

$handle = fopen('data.txt', 'w+');
$data = "song: lalala\3 times\r";
$success = fwrite($handle, $data);
fclose ($handle);

header('Content-Type: data.txt/txt');
//header("Content-length: " . filesize($NewFile));
header('Content-Disposition: attachment; filename="' . $handle . '"');

if($success){
    echo "succeed :)";
    }
    else{
        echo "fail";
        }



?>
mamandis
New php-forum User
New php-forum User
 
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

Re: write to file

Postby egami » Thu Apr 26, 2012 5:47 am

header('Content-Disposition: attachment; filename="' . $handle . '"');

This is your problem.. because $handle is a resource ID, and not an actual filename.

change this to data.txt and it should work, provided your filesystem let you write the file.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: write to file

Postby mamandis » Thu Apr 26, 2012 7:15 am

I wrote as You said:

header('Content-Disposition: attachment; filename= "data.txt" ');

but the result is the same.. succeed


:help:
mamandis
New php-forum User
New php-forum User
 
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

Re: write to file

Postby egami » Thu Apr 26, 2012 7:37 am

$filename = "data.txt";
$handle = fopen($filename, 'w+');
$data = "song: lalala times\r";
$success = fwrite($handle, $data);
fclose ($handle);

header('Content-Type: text/txt');
header("Content-length: " . filesize($filename));
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($filename);
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: write to file

Postby mamandis » Thu Apr 26, 2012 11:22 pm

Thank You for Your answer very much.. ;) it works :)

and I have one more quastion about this-
to $date variable I want put another information, I want that information which is showing on a screen be written to a file. I show what I have in another script (below):

in my script I have classes, ex.
class GetProductList
{
public $GetProductListRequest;
}


I have arrays $map = array, ex.
"GetProductListRequest"=>"GetProductListRequest"

and of course I have public function, ex.

Code: Select all
public function getProductList () {
      try
      {
         $this->init();
         $request = new GetProductListRequest();
         $request->Filters = new ArrayOfFilter();               
         $language = new Filter();
         $language->Name = "Language";
         $language->Value= "lt-lt";
         $currency = new Filter();
         $currency->Name = "Currency";
         $currency->Value = "LTL";
         $request->Filters->Filter = array( $language,$currency);
         $params = new GetProductList();
         $params->GetProductListRequest = $request;
         $result = $this->soapClient->GetProductList($params);
         

         foreach ($result->GetProductListResult->ProductList->Product as $p)
         {
               
               print $p->SupplierCode." ".$p->Name."<br />";
         }
      

         //echo "<pre>". print_r($result,1) . '</pre>';
      }
      catch(SoapFault $e) {
         echo '<xmp>' . $this->soapClient->__getLastRequestHeaders()  .  $this->soapClient->__getLastRequest() .   '</xmp>';
         echo "<pre>".    print_r($e,1) . '</pre>';
      }
   }


I dont know what to write to $data.. do I need to write $p->SupplierCode." ".$p->Name."<br />";

explain me plese :help:
mamandis
New php-forum User
New php-forum User
 
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

Re: write to file

Postby minimihi » Sat Apr 28, 2012 12:29 pm

I didn't clearly understood you.

If you want to put text generated by getProductList() function into $data variable.
You would need to return function value to variable.

Returning what is generated by function:
Code: Select all
public function getProductList () {
      $output = "";
      try {
   //  ...
         foreach ($result->GetProductListResult->ProductList->Product as $p) {
               $output .= $p->SupplierCode." ".$p->Name."<br />"; 
         
}
      
      
}
      catch(SoapFault $e) {
          $output .=  '<xmp>' . $this->soapClient->__getLastRequestHeaders()  .  $this->soapClient->__getLastRequest() .   '</xmp>';
      }
      return $output;
   } 


and to receive returned value to variable
Code: Select all
$data = getProductList(); 

if function is inside of class and you're calling function not from inside of class
Code: Select all
$data = GetProductList::getProductList(); 

if function is inside of class and you're calling function from inside of class
Code: Select all
$data = $this->getProductList(); 
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania


Return to PHP Scripts

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron