/    Sign up×
Community /Pin to ProfileBookmark

Simple File Saving Issue

I have the following code:

[code=php]$sourcefile = fopen(‘http://foo.com/file.xml’,r);
$writefile = fopen(‘file.xml’, w);[/code]

What function can I use to save the ‘sourcefile’ as the ‘writefile’?

I tried:

[code=php]fwrite ($writefile,$sourcefile);[/code]

but ended up with a file named file.xml containing “Resource id #1”

Thanks for your help!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelMay 26.2005 — Mostly copied from http://php.net/manual/en/function.file-get-contents.php
[CODE]
<?php
$filename = '[COLOR=Red]new_file.php[/COLOR]';
$somecontent =[COLOR=Red] file_get_contents('source_file.php');[/COLOR]

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, '[COLOR=Red]w[/COLOR]')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
?>
[/CODE]

(The red text is what i changed.)
×

Success!

Help @lunarkid2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...