ahhhh chmod 703 [directoryname]!! that was my unix lesson for the day. now to start the code to delete the old file and write the new file when using fopen (the manual said if you utilize the "w" the file size will grow indefinitely and that would be bad)
soshea
extract information from url-txt
Moderator: General Moderators
got the code for delete-create-write down
well, i think i am getting the hang of this here is my code for the correct data from the bogus_ski.txt (this is the smaller file that has the proper snowdepth for this area, the larger file has the other ski areas that i will be working on)
check it out and let me know if you would make any changes:
the file came from:
http://idsnow.id.nrcs.usda.gov/snow/pub/bogus_ski.txt
thanx!! soshea
check it out and let me know if you would make any changes:
the file came from:
http://idsnow.id.nrcs.usda.gov/snow/pub/bogus_ski.txt
Code: Select all
<?php
//set file to delete
$fileDel = "bogus.txt";
//see if exists and delete
if(file_exists($fileDel)) {
unlink("$fileDel");
}
//got the quicker page than ftp
$file_array = file("bogus_ski.txt");
//trim the values in the array
$snowdepth = array_map("trim", $file_array);
//search for end of file
$searchsnowS = array_search("end of file", $snowdepth);
//go back one for the current values
$searchsnowS = $file_array[$searchsnowS-1];
//set the file variable to open for writing
$file_write = "bogus.txt";
//open the file
$fp = fopen($file_write, "w");
//write the latest data
fwrite($fp, $searchsnowS);
//close the file
fclose($fp);
?>Looks good to me. In time, you'll want to do things like check for the success of the various functions, but barring the occurrence of any errror, what you got there looks like it will do the trick.
I will say that deleting the file up at the top of the script isn't really needed. When you do that file open using "w", all the contents are being removed anyways.
Also, what if the script fails to get the bogus_sk.txt file for some reason? You've allready deleted what you allready had too. Now what?
If you don't harm the old file until you're sure you got the bogus_ski.txt file into an array, you can have something to fall back on should the script fail somewhere.
Let me know if that makes sense.
Cheers,
BDKR
I will say that deleting the file up at the top of the script isn't really needed. When you do that file open using "w", all the contents are being removed anyways.
If you don't harm the old file until you're sure you got the bogus_ski.txt file into an array, you can have something to fall back on should the script fail somewhere.
Let me know if that makes sense.
Cheers,
BDKR
it completelly makes sense, i had read that the "w", eventhough it is removing the current data, it still makes the file size grow. i will change the code appropriately to check and die or continue.
the main problem i am having now is the depth1.txt file. i can grab the individual area, but the problem is getting the last depth line. what is consistent is the empty line(empty array) but i have not had any luck replacing the blank or searching for the blank and going back one (as you can tell by the blown code below). any suggestions?
reference: ftp://ftp.wcc.nrcs.usda.gov/states/id/depth/depth1.txt
the main problem i am having now is the depth1.txt file. i can grab the individual area, but the problem is getting the last depth line. what is consistent is the empty line(empty array) but i have not had any luck replacing the blank or searching for the blank and going back one (as you can tell by the blown code below). any suggestions?
reference: ftp://ftp.wcc.nrcs.usda.gov/states/id/depth/depth1.txt
Code: Select all
<?php
$file_array = file("depth1.txt");
# $file_array = trim($file_array);
# Get rid of newline characters
while(list($key, $val)=each($file_array))
{
$val = trim($val, "\n");
$val = trim($val, "\r");
$val = str_replace('','end of marker',$val);
}
# Move back to element 0
reset($file_array);
# Create references for our search
$reference="******************** WATER PREC Deg.C DEPTH ";
$SpacerLine=" ";
$end="-----------------------Temperature Conversions-------------------------------";
# Now parse according to what we want to see
while(list($key, $val)=each($file_array))
{
if(strstr($val, $reference))
{
$SnowMarker = str_replace('Date, PST Time SNOW AIR TEMP SNOW','',$file_array[$key-1]);
if (strstr($SnowMarker,"HIDDEN LAKE")) {
echo $SnowMarker;
$SnowDepth = $file_array[$key+50];
echo $SnowDepth;
#$Snow_Depth_Search = array_search("end of marker",$file_array);
#$Snow_Depth_Search = $Snow_Depth_Search[$key-1];
#echo $Snow_Depth_Search;
/*if (strstr($file_array,'')) {
$SnowDepth = $file_array[$key-1];
echo $SnowDepth;
}*/
echo "\n";
print "<br>";
}
} #sleep(1);
if(strstr($val, $end))
{
break;
}
}
?>