Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable saved as a file 2

Status
Not open for further replies.

solex

Programmer
Feb 28, 2003
91
GB
Hello,

I want to save the contence of a variable as a .torrent file in a specified location, but can only find info on handling uploads and im not sure how to translate this to what i need

Thanx in advance

solex
 
Have you tried lookig up the [blue]fopen()[/blue] and [blue]fwrite[/blue] functions in the PHP online manual?


PHP Manual said:
resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )

fopen() binds a named resource, specified by filename, to a stream.


int fwrite ( resource handle, string string [, int length] )

fwrite() writes the contents of string to the file stream pointed to by handle

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Beat me to the punch Sleipny.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
great, not to sure how to set the save directory path tho :s

thanx
 
Are you not reading the php manual we pointed you to? the function fopen takes a parameter of String Filename which is in itself the path to the file you want to create and or write to.

so
Code:
fopen("\path\to\file.torrent","w");

Effectively creates a file in that direcotry if none exists, otherwise it opens it to be written to.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
sorry, from what i read in the manual and from the examples it gives I took it to just be the file name in that section of the function, as the example uses the file name saved as $filename.

charles
 
I don't understand. The example code on the PHP online manual page for fopen() all use absolute pathnames, which include directories. The two exceptions are the two examples which are interacting with FTP or HTTP sites.



Want the best answers? Ask the best questions! TANSTAAFL!
 
odd, in getting a:


Warning: fwrite(): supplied argument is not a valid stream resource in C:\Program Files\Abyss Web Server\htdocs\torrentSearch.php on line 101
 
Could we see some code?

Did you use fopen to open the file and then pass the stream to fwrite?

such as:
Code:
$handle=fopen("path\to\fie.torrent","w");
fwrite($handle,"write this to the file");

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ohh, adn don;'t forgewt to issue [blue]fclose [/blue] after you're done writing to the file.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
// get page source
$torrentDownloadReturned = fopen($torrentDownloadAddress, "r") or die("cannot open remote file");
$torrentDownloadReturned = stream_get_contents($torrentDownloadReturned);

// Save returned file as torrent file
$torrentFileName = "torrent/{$torrentSearchName}-{$torrentIDNumber}.torrent";
print($torrentFileName);
fwrite("$torrentFileName","$torrentDownloadReturned");
 
(i have verified that the "//get page source" works correctly, in that i can print the contence of the file)
 
That means its opening the original file correctly, however you have to create a new file to use fwrite to put the contents in it.
So once you have the original file opneed, issue another fopen command to create the new file with the $torrentfilename, to write to it.


and pass that handle to fwrite().





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
(also that the torrent file name returns the desired name)
 
ammeded code:

// get page source
$torrentDownloadReturned = fopen($torrentDownloadAddress, "r") or die("cannot open remote file");
$torrentDownloadReturned = stream_get_contents($torrentDownloadReturned);

// Save returned file as torrent file
$torrentFileName = "torrent/{$torrentSearchName}-{$torrentIDNumber}.torrent";
fopen($torrentFileName,"x+");
fwrite($torrentFileName,$torrentDownloadReturned);

with errors:

Warning: fopen(torrent/lost-9163418.torrent) [function.fopen]: failed to open stream: No such file or directory in C:\Program Files\Abyss Web Server\htdocs\torrentSearch.php on line 100

Warning: fwrite(): supplied argument is not a valid stream resource in C:\Program Files\Abyss Web Server\htdocs\torrentSearch.php on line 101

played about with no luck so far...

thanx
 
If you are running PHP on Windows, you must either:

use "\\" as your directory separator (because "\" is PHP's escape character(

or

use "/" even though you are on Windows. PHP will understand what you are talking about.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Is there a torrent directory in that path??? If this is on a windows machine the directory slahes are inverted and must be doubled. i.e torrent\\lost-9163418.torrent

Other than that it might be a permissions issue with the OS. Does the webserver user have read and write rights to the folders in question?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top