473,498 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

content write to file

114 New Member
Dear Friends,

I am trying to write and append content to a notepad file and I am getting the error "Couldn't open file". Here is the code:

[PHP]<?php
$myfile = "dtest.txt";
$openfile = fopen ($myfile,"w") or die ("Couldn't open the file");

fwrite ($openfile,"This is a string \n");
fclose ($openfile);

$openfile = fopen ($myfile,"r") or die ("Couldn't open the file");
$file_size=filesize($myfile);

$file_contents = fread ($openfile,$file_size);
$msg ="$file_contents";

fclose ($openfile);
echo $msg;
?>[/PHP]

Please tell me where I am making mistake.

Please advice............

Thanks
Deepak
Jun 25 '07 #1
2 1508
ben808
2 New Member
mmm - for starters I think I would take out the "or die" statements first if you can just so you can view the real error message from php with line number etc. - it's probably a permissions issue I would guess - bit difficult to say without the true error message...
Jun 26 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, Deepak.

I am getting the error "Couldn't open file".
Unfortunately, you have two error messages that both say the same thing, so it won't help you much when you're debugging. The first thing I would do in this situation is to change the error messages you're getting to 'Couldn't open the file for writing.' and 'Couldn't open the file for reading.' That way, at least you know *when* your script is failing.

As ben808 mentioned, failing to open a file for writing is generally a permissions issue (whereas failing to open a file for reading is generally a 'file not found').
Jun 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2000
by: Christian | last post by:
Hello, is it possible to get everything between <form></form> into an variable even the plain text so i can work with it and write it to a file? thanks for the advice chris
10
3369
by: Blue® | last post by:
I would like to call the content of content.htm (containing only HTML codes) into index.htm. This is usually done by renaming index.htm to index.shtml and use this tag: <!--#include...
4
6298
by: Fabian | last post by:
Hi I want to change the content of text file. I use an StreamReader to read but I can´t use a StreamWriter for the same Stream. What I must do to implement content changing? Thanks for...
5
4814
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
2
1527
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to...
0
1450
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------390C0F3E0099" without the quotes),...
7
8688
by: xkeops | last post by:
Thinking of creating a website, most of the pages will have a general toolbar menu, a content and a footer. The content will be the only one who's gonna change but the rest (header,footer) will...
4
2485
by: rony_16 | last post by:
Hi, I have a program that connects to a site With WebRequest and WebResponse . The response of this site is a file (csv file). The problem is that the file do not comes as a stream , hi is a part...
2
2568
kamill
by: kamill | last post by:
i need to write content of one text file into another text file. My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
3
1270
by: swethak | last post by:
hi i want to write a content in $text into the file. For that i wrote the below code .But i run that code i got the following errors.How i avoid that errors and write the whole content in...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.