rpjd Posted May 22, 2011 Share Posted May 22, 2011 This is my code $file = "E:/wamp/www/Project/Changes/".$LineNo.".txt"; if(file_exists($file)) { fopen($file,'a') or die("can't open file"); fwrite($file,$Username); } else { fopen($file,'w'); fwrite($file,$Username); } Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/ Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218695 Share on other sites More sharing options...
rpjd Posted May 22, 2011 Author Share Posted May 22, 2011 Is it possible to open a file in append mode only if the file exists, otherwise open it in write mode? The code as it is creates and writes to the file in append mode when the file doesn't initially exist. Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218731 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218746 Share on other sites More sharing options...
rpjd Posted May 22, 2011 Author Share Posted May 22, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218768 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218770 Share on other sites More sharing options...
rpjd Posted May 22, 2011 Author Share Posted May 22, 2011 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']}'); Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218801 Share on other sites More sharing options...
rpjd Posted May 22, 2011 Author Share Posted May 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/237120-fwrite-expects-parameter-1-to-be-resource-string-given/#findComment-1218815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.