Jump to content

fwrite() expects parameter 1 to be resource, string given


Recommended Posts

fopen returns a handler which you need to capture using a variable when calling fopen. You then pass this handler as the first parameter to fwrite. You don't pass the filename.

 

Corrected code

$handle = fopen($file,'a') or die("can't open file");
fwrite($handle, $Username);

Is it possible to open a file in append mode only if the file exists, otherwise open it in write mode?

Why when that is what append mode does. Quote from the manual

Mode 'a'

Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

This is the updated code but I am getting Warning: fopen(E:/wamp/www/Project/Changes/3.txt) [function.fopen]: failed to open stream: No such file or directory.  The else statement is executing but the file isn't being created.

$file = "E:/wamp/www/Project/Changes/".$LineNo.".txt";
if(file_exists($file))
{
$handle = fopen($file,'a') or die("can't open file");
fwrite($handle, $Username);
}
else
{
$handle = fopen($file,'w') or die("can't open file");;
fwrite($handle ,$Username);
}

Does the folder Changes exist within the Projects folder? fwrite will not create the directory structure if it doesn't exist. You'll first need to create the directory structure and then create/write to the file.

 

Also make sure the folder Changes if it exists is not set to Read Only in Windows.

Got the that working now thanks.  My next hurdle now is to create a link to that file and insert it into a table cell on the page. 

The is the link

$Link = "<a href='E:/wamp/www/Project/Changes/{$file}>{$Count['0']}</a>";

This is what I have so far, how do I get the link into the cell?

$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->Load('file.php');
$Id = $doc->getElementById('Cell{$changes[LineNo']}');

 

 

I have 10 cells in a table row withe this format

echo "<tr><td>Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td><td>Counter1</td><td>Button1</td><td>Counter2</td><td>Button2</td><td>Counter3</td>Button3</tr>"";

if(isset($_POST['Button2'])) 
$LineNo= reset(array_keys($_POST['Button2']));

gives me the line number of the button that was pressed.

How do or can I using the array_keys insert something into Counter2 cell?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.