/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] php to write text file


——————————————————————————–

I have a PHP code, which is to add some data to the MySQL database by the form method; follow by clicking the “submit” botton, the data is added successfully, and the this newly added data is to be writen to a text file:

[code=php]$id=mysql_insert_id();
if($typeid == ‘1’ && $catid == ‘1’ )
{
$filewrite1 = $typeid.” “.$catid.” “.$URL.” “.$title.” “.$author.” “.$publish_date.” “.$content;
$Text1 = “id”.$id.”txt”;
$file1 = “text by PHP/1business_1product/”.”1business_1product_”.$Text1;

if(!function_exists(‘file_put_contents’)) {
function file_put_contents($file1, $filewrite1, $file_append = false)
{
$fp = fopen($file1, ($file_append ? ‘w+b’ : ‘a+b’));
if(!$fp) {
trigger_error(‘file_put_contents cannot write in file.’, E_USER_ERROR);
return;
}
fputs($fp, $filewrite1);
fclose($fp);
}
}
else
$filecreate1 = file_put_contents($file1,$filewrite1);
}
[/code]

Currently, the directory “text by PHP/1business_1product”, containes some text files naming as “1business_1product_id2.txt”, etc. Therefore the newly added text file to this directory will have a similar file name with different “id”.

However, this PHP code is not writing the text file at all, without any error mesage. It seems to stop at the line of “if(!function_exists(‘file_put_contents’))”, and hence it does not excute the “function file_put_contents”…

Could any one help me debug the problems?

Thank you very much!

P.S.: I have another PHP code to write all data from MySQL to text files, using the same function, and it works well.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 05.2006 — I think you want to get rid of the [b]else[/b]. Otherwise, you never actually execute your conditionally defined file_put_contents() function:
[code=php]
$id=mysql_insert_id();
if($typeid == '1' && $catid == '1' )
{
$filewrite1 = $typeid." ".$catid." ".$URL." ".$title." ".$author." ".$publish_date." ".$content;
$Text1 = "id".$id."txt";
$file1 = "text by PHP/1business_1product/"."1business_1product_".$Text1;

if(!function_exists('file_put_contents')) {
function file_put_contents($file1, $filewrite1, $file_append = false)
{
$fp = fopen($file1, ($file_append ? 'w+b' : 'a+b'));

if(!$fp) {
trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
return;
}
fputs($fp, $filewrite1);
fclose($fp);
}
}
$filecreate1 = file_put_contents($file1,$filewrite1);
}
[/code]

PS: cleaning up the indenting might have made that easier to see.
Copy linkTweet thisAlerts:
@yunfannyauthorJul 06.2006 — Thank you,NogDog . I have got the codes working.
×

Success!

Help @yunfanny 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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