So, after pouring through the 260 phpbuilder posts that my search came up with I'll post here and hope someone can provide some insite.
I'm writing a script that is supposed to hit a URL text file, grab a portion of the file and then write that portion into a new file on my server. When running this code I get the following error:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/sbgaming/public_html/installer/getnews.php on line 40
I get the same error for each instance of fwrite in the script. I cannot fo the life of me figure out what it wrong with my fwrite()s. I have chmod the calling file, and the folder to 777 and still I have no luck. I would also like to point out that the file "created" by this snipet of code is created each time the script is run, it just doesn't write to it:
$filedate = date("U");
$newsfile = "/home/sbgaming/www/installer/$filedate.txt";
fopen($newsfile, 'a');
echo $newsfile;
Line 40 is this line:
fwrite($handle, "$filename $lastrow\n");
Also included at the end of this post is a second "test script" that I did that works perfectly fine... so I'm completely baffled.
I'm using the following code:
<?php
error_reporting(E_ALL);
fopen ('/home/sbgaming/www/installer/newsreader.txt', 'r+');
$handle = file("/home/sbgaming/www/installer/newsreader.txt");
$count = sizeof($handle);
$filename = trim(substr($handle[$count-1], 0, 6));
$lastrow = trim(substr($handle[$count-1], 7,3));
$openfile = "http://URL/";
$openfile .= $filename;
$openfile .= ".txt";
if ($newsfeed = file($openfile)) {
$num = sizeof($newsfeed);
if ($lastrow == $num) {
$i = 0;
while ($i < 10) {
$filename++;
$openfile = "http://URL/";
$openfile .= $filename;
$openfile .= ".txt";
if ($newsfeed = file($openfile)) {
fwrite($handle, "$filename $lastrow\n");
break;
}
$i++;
}
} else {
$filedate = date("U");
$newsfile = "/home/sbgaming/www/installer/$filedate.txt";
fopen($newsfile, 'a');
echo $newsfile;
while ($lastrow < $num) {
$row = $newsfeed[$lastrow];
fwrite($newsfile, $row);
$lastrow++;
}
fwrite($handle, "$filename $lastrow\n");
}
} else {
$i = 0;
while ($i < 10) {
$filename++;
$openfile = "http://URL/";
$openfile .= $filename;
$openfile .= ".txt";
if ($newsfeed = file($openfile)) {
fwrite($handle, "$filename $lastrow\n");
break;
}
$i++;
}
}
?>
And this is the test code that works.
<?
$handle = fopen("/home/sbgaming/www/installer/newnewsfile.txt", "a+");
$filename = 123456;
$lastrow = 12345;
if (fwrite($handle, "$filename $lastrow\n") === FALSE) {
echo "<br>Cannot write to file ($filename)";
exit;
}
echo "<br>Success, wrote ($filename $lastrow) to file.";
?>