PDA

Click to See Complete Forum and Search --> : fread/fwrite help


PlaGuE
Nov 10th, 2005, 03:46 AM
Okay. How would i go about in doing this.

Open file -> delete contents -> add new content -> display content

so far ive only been able to read and write.but not delete the contents.

visualAd
Nov 10th, 2005, 04:23 AM
You can open the file in truncated write mode. Whereby the file contents is wiped when you open it.

$f = fopen('file/path', 'wb');

PlaGuE
Nov 10th, 2005, 04:30 AM
thanks...

PlaGuE
Nov 11th, 2005, 01:36 AM
Another question that goes along this line.

how would i go about in.... creating a file that doesnt exist. when the page loads...

heres my code.
all files are put to 0777
<?php

$filename = "empty_page.php";
$somecontent = "test\r\n";

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

if (!$handle = fopen($filename, 'wb')) {
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";
}
?>

visualAd
Nov 11th, 2005, 02:01 AM
If the file doesn't exists, fopen will always try to create it.

PlaGuE
Nov 11th, 2005, 02:09 AM
pfft...

now it works...

lol... i left it alone for 15 mins ... and come back to it... and it works... OoOo Scary. o.O

PlaGuE
Nov 11th, 2005, 02:37 AM
okay another Question...

NO STEALY IDEA.

if($_GET['page'] == ""){
$sql="Select * from pages WHERE name = 'home'";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)):
///////////////////////////////////////////////////////

$filename = $_GET['page'] . ".php";
$somecontent = "$row[content]";

// Let's make sure the file exists and is writable first.
if (!$handle = fopen($filename, 'wb')) {
echo "Cannot open file ($filename)";
exit;
}

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

fclose($handle);
chmod("$filename", 0777);
///////////////////////////////////////////////////////
include("$filename");
unlink($filename);
endwhile;

idk how to go about the deleting of the files. i tried unlink and unset.
as you can see i even chmod the file to 0777.

Once i get the basics down... ill be able to be more creative...

visualAd
Nov 11th, 2005, 03:09 AM
Does it come up with an error message? Maybe the include call which opens that file maintains a lock on the file until the PHP process have finished.

PlaGuE
Nov 11th, 2005, 03:18 AM
again... after i leave it alone... it fixes itself...

*** IS UP WITH THAT?.lol