?
Solved

debug / get fwrite() working!!!

Posted on 2009-12-20
8
Medium Priority
?
519 Views
Last Modified: 2013-12-12
I am trying to run the simplest of PHP script use fwrite() and it does not work.. The script is hosted with a2hosting and they did have to turn the ability on to use fwrite() but it still does not work. Also, the hosting company said that the file I write to must be set to 777 or it will not work - but I have done this and it still does not work. Is there a way to debug this. I have checked the server logs but they are limited (not system.logs) and all looks fine.

the output of the page is:
00  Error:  [8] Undefined property: stdClass::$debug

00 for the two fwrite() and the other is from customError(), but I don't think its a problem.

what can I do?
<?php
	ini_set('display_errors',1);
	error_reporting(E_ALL);
	
	function customError($errno, $errstr){
	  echo "<b> Error: </b> [$errno] $errstr <br>";
	}
	
	//set error handler
	set_error_handler("customError");
	
	$myFile = "http://dev.mydomain.com/text.txt";
	$fh = fopen($myFile, 'w') or die("can't open file");
	$stringData = "Bobby Bopper\n";
	echo fwrite($fh, $stringData);
	$stringData = "Tracy Tanner\n";
	echo fwrite($fh, $stringData);
	fclose($fh);		
?>

Open in new window

0
Comment
Question by:waffe
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1600 total points
ID: 26093988
You are trying to open a remote connection for writing via HTTP, you are not writing a local file in the filesystem so the 777 permissions are irrelevant.

If this http:// address is yours then amend you program to write to the folder in the filesystem space instead - something like /var/www/htdocs/mysite.com/folder/file
0
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 400 total points
ID: 26094030
1. I think that you probably have some other code that you're not showing - I don't see any reference to a "debug" method / property at all in your code.

2. The problem you're seeing is because you're trying to fwrite to a file via the "http://" protocol. Let me give you an analogy to help explain:

Let's say you wanted to go from your house to the supermarket. There are different ways you could travel to the supermarket. Each different method you can take to get there is called a "protocol". So you could WALK to the supermarket or you could take the bus there.

Both of those methods get you from your house to the supermarket, but they ARE different. For example, if you walked to the supermarket, you could decide at any time to give up and go back home. It's also free, but it's also slower than taking a bus. If you take a bus, you get there faster but you probably can't tell the bus driver to turn around and drive you back home.

These are just examples of protocols in real life (it's not a perfect example, but you get the idea). In the computer world, you also have protocols of accessing files, and each protocol acts a different way (and has its own advantages/disadvantages/etc).

Now, when you're accessing a file via a URL that starts with http:// or https://, you can ONLY READ from the file. You cannot write to a file via the HTTP protocol (at least not usually). If you think about it - that's a good thing. Otherwise, anyone could just take your script, change $myFile to be Google's homepage and overwrite it, right? Or someone else could put in YOUR URL and change YOUR file (that would be bad if they could do that).

So now the question is - how DO you write to that file? I am assuming that text.txt is in the same directory as your PHP script or at least on the same server. If so, then you're in luck. Remember the supermarket example? The supermarket is sort of like the file we're trying to change. Just like there are different ways to get to the same physical supermarket, there are different ways to gett to the same physical file. Using a URL is one way, but it's read-only, so that doesn't help you. But you CAN change files that are located on the same server if you have access to them (also known as being on the same filesystem).

So text.txt might be accessible via:
http://dev.mydomain.com/text.txt
and also via
/home/web/dev.mydomain.com/htdocs/text.txt

The second one is just an example of a filesystem path. (It probably is not the correct path to your file, but you can probably see the correct path in an FTP program or something). If you change $myFile to be the filesystem path to your text.txt file (and if text.txt is 777), then you should be able to write to it. Keep in mind that you're writing to the same physical file, so if you update the file using the filesystem method / protocol, then you will be able to see the changes via the HTTP URL as well.

Hopefully that explains some of it. There are some more advanced concepts about permissions and stuff, but that's probably something to learn later.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 26094031
One disadvantage of writing a long explanation - people can get in a shorter answer before you. :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26094068
:-D

0
 

Author Comment

by:waffe
ID: 26094140
OK, thanks for the info!

but it still does not work!!

I now have the txt file the same folder as my script, the file opens wit no errors and then fwrite() echos "13". When I look at the txt file it is empty :(
<?php
	$myFile = "text.txt";
	$fh = fopen($myFile, 'w') or die("can't open file");
	$stringData = "Bobby Bopper\n";
	echo fwrite($fh, $stringData);
	$stringData = "Tracy Tanner\n";
	echo fwrite($fh, $stringData);
	fclose($fh);		
?>

Open in new window

0
 

Author Comment

by:waffe
ID: 26094238
Crazy! Finally if I put both script and txt file in the root directory it writes!

Thanks again, I always have a hard time getting relative URL to work so I resort to absolute URLs; I will stop doing that and figure out why the relative paths don't work. It looks like I am going to have to keep the txt file on the root and have the script point to it - I think!
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26094249
For relative URLs start them with a dot to make them relative to the current directory and .. for the directory above.

For instance, if your current directory is /var/www/mysite then

./file.txt is /var/www/mysite/file.txt
./someOtherFolder/file.txt is /www/var/mysite/someOtherFolder/file.txt

../file.txt is /var/www/file.txt
../someOtherFolder/file.txt is /www/var/someOtherFolder/file.txt
0
 

Author Closing Comment

by:waffe
ID: 31668412
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question