Open php file from html

PHP File Open/Read/Close

In this chapter we will teach you how to open, read, and close a file on the server.

PHP Open File — fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

We will use the text file, «webdictionary.txt», during the lessons:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:

Example

$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
echo fread($myfile,filesize(«webdictionary.txt»));
fclose($myfile);
?>

Tip: The fread() and the fclose() functions will be explained below.

The file may be opened in one of the following modes:

Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
Читайте также:  Python yandex translate api

PHP Read File — fread()

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The following PHP code reads the «webdictionary.txt» file to the end:

PHP Close File — fclose()

The fclose() function is used to close an open file.

It’s a good programming practice to close all files after you have finished with them. You don’t want an open file running around on your server taking up resources!

The fclose() requires the name of the file (or a variable that holds the filename) we want to close:

PHP Read Single Line — fgets()

The fgets() function is used to read a single line from a file.

The example below outputs the first line of the «webdictionary.txt» file:

Example

$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
echo fgets($myfile);
fclose($myfile);
?>

Note: After a call to the fgets() function, the file pointer has moved to the next line.

PHP Check End-Of-File — feof()

The feof() function checks if the «end-of-file» (EOF) has been reached.

The feof() function is useful for looping through data of unknown length.

The example below reads the «webdictionary.txt» file line by line, until end-of-file is reached:

Example

$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
// Output one line until end-of-file
while(!feof($myfile)) echo fgets($myfile) . «
«;
>
fclose($myfile);
?>

PHP Read Single Character — fgetc()

The fgetc() function is used to read a single character from a file.

The example below reads the «webdictionary.txt» file character by character, until end-of-file is reached:

Example

$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
// Output one character until end-of-file
while(!feof($myfile)) echo fgetc($myfile);
>
fclose($myfile);
?>

Note: After a call to the fgetc() function, the file pointer moves to the next character.

Complete PHP Filesystem Reference

For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.

Источник

Calling a PHP File From HTML’s Script Tag

CodeinWP CodeinWP

Calling a PHP File From HTML

Okay, admittedly, this is a pretty crazy and virtually useless tip. But it’s just one of those things that illustrates the flexibility of our craft and how sometimes solutions to problems can be found from some really outside-the-box thinking.

Back when I worked for a Toronto web design agency, we often had to update sites that were written in straight HTML, with no server-side programming at all. So every year, clients that owned such sites would ask us to update all the pages to display the current copyright year. That was pretty lame. Not exactly the kind of work we wanted to be doing.

So we wanted to create a way that the current year would be printed on each page automatically. But we didn’t want to include a JavaScript library, we didn’t want to use Ajax, and we wanted the year to come from the server, not the client.

So, one of our crafty PHP developers thought of doing this:

It’s just a regular HTML tag that calls a PHP file instead of a JavaScript file. But there’s one catch: In that PHP file, you still have to use JavaScript. So we did something like this:

If you do just straight PHP, you won’t see any output, because the browser is expecting JavaScript. So, we give the browser JavaScript by echoing out the lines using PHP.

This allowed us to fulfil all of our requirements: We didn’t have to bother updating those copyright dates every year; we didn’t include any Ajax functions or any JavaScript library; and we retrieved the date from the server rather than the client.

Why Would You Need to Do This?

You probably wouldn’t. This is pretty useless for almost all projects because any project built in the last 5 or 6 years will be using some kind of server-side programming and/or a JavaScript library. So if a JS library is available, then you could just call the PHP file using Ajax.

But I thought this was a great example of some really crafty thinking, and it helped us get out of a little jam, while still utilizing server-side resources on a static site. It also minimized the amount of code used. And if nothing else, it’s interesting to see that the tag can load pretty much any kind of file.

Are there any security issues with this? I really have no idea. I believe there are tracking applications and web counters that have used this approach. Would be interested to hear if anyone has any input on this.

46 Responses

Wow, for a site called Impressive Webs, I didn’t expect something so… let’s say “unimpressive”. Why didn’t you want the date coming from the client again? Would have saved you an unnecessary network request if you didn’t have that requirement. It’s the same thing as doing an ajax request without all the ajax handling. Once you start using php, it isn’t really “static” either…

I didn’t think of the idea, bob. I just implemented it at the time. I’m completely aware of how useless this is, as I already stated in the article. JavaScript gets the date from the client’s system, whereas PHP gets it from the server. Yes, it’s extremely unlikely that there would be any difference between the two, but we just chose to do it that way, because… well, we could. 🙂 It’s just a ridiculous hack that illustrates what’s possible with the script tag. I’m not advocating this method for getting date/time, I’m just illustrating that it’s possible to call any file using the script tag.

Just thought I’d add — the script block is not actually processing any php (just in case a new web dev stumbles along this…). In fact, when you omit “type” from the script tag, standard browsers will assume “text/javascript”, but the browser doesn’t care about the src attribute’s extension (php). So the javascript is generated returned (probably as text/html?) and luckily browsers don’t care too much about the content type, otherwise this might not work! I wonder if adding the text/javascript header to the PHP file’s response might be necessary for some older or stricter browsers though… In fact, you should be able to just use , and the browser will try to parse the contents of the HTML file as Javascript. Of course, HTML files don’t usually include server side processing (some exceptions!) and the cool thing about using a php file is that it’s automatically handled by PHP and so you can do things like get the server time, or request a new webpage and tell javascript to write it into the document (thereby getting around cross-site scripting restrictions). Also, as for the date coming from the server rather than the client, I think that’s a good idea. It is a copyright after all. If it’s expired (or appears expired to the user who has his or her system time set back by a few years for some stupid reason), then … well… that would be stupid. Not sure if anyone would be able to use the copyrighted work and claim it had expired (and they could always modify the source of the page to display whatever the hell they want)… but still… using the server date gives the programmer better control over the page look. And it won’t be able to show up in search engine results with the wrong copyright date (actually may show with none at all if JS is not evaluated by the spider…)

I don’t actually think it’s appropriate to get a copyright date from a client’s system. If the system date were, say, a year ahead, the copyright date would appear to be a year in the future, which would look weird and unprofessional. Admittedly there would be no legal implications that I’m aware of, but it would still be undesirable. The risk might be small, but why take it when you could avoid it? IMO, they chose the right course of action.

plus…wouldnt you want YOUR copyrighted content copyrighted with the date from YOUR servers…just my thoughts

Источник

Оцените статью