Nakon sto prebacite fajl, mozete mu pristupi iz PHP-a. Za pristup prebacenim fajlovima mozete da koristite funkciju fopen. Funkcija fopen otvara fajl ili URL.
fopen(ime_fajla, pristup_fajla)
ime_fajla - obavezan parametar. Odredjuje fajl ili URL za otvaranje.
pristup_fajla - obavezan parametar. Odredjuje vrstu pristupa koja vam je potrebna u fajlu.
Moguce vrednosti:
."r" - samo za citanje. Pocinje na počctku fajla.
"w" - samo za pisanje. Otvara i brise sadrzaj fajla ili stvara nov fajl ako on ne postoji.
"a" - samo za pisanje. Otvara i pise na kraju fajla ili stvara nov fajl, ako on ne postoji.
"x" - samo za pisanje. Stvara nov fajl. Vraca netacno i gresku ako fajl vec postoji.
"r+" - citanje/pisanje. Pocinje na pocetku fajla.
"w+" - citanje/pisanje. Otvara i brise sadrzaj fajla ili stvara nov fajl ako on ne postoji.
"a+" - citanje/pisanje. Cuva sadrzaj fajla pisanjem na kraju fajla.
"x+" - citanje/pisanje. Stvara nov fajl. Vraca netacno i gresku ako fajl vec postoji.
Primer: za otvaranje fajla upotrebicemo funkciju fopen:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
.
.
.
.
?>
Kroz sve linije teksta mozete proci uz pomoc petlje while. Petlja se zavrsava kada stignete do kraja fajla, sto se proverava pomocu funkcije feof:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
while (!feof($rukovanje_fajlovima)) {
# kod...
}
?>
Tekst iz fajla mozete citati uz pomoc funkcije fgets:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
while (!feof($rukovanje_fajlovima)) {
$tekst = fgets($rukovanje_fajlovima);
echo $tekst, "<br />";
}
?>
Privremeni fajl cete zatvoriti uz pomoc funkcije fclose:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
while (!feof($rukovanje_fajlovima)) {
$tekst = fgets($rukovanje_fajlovima);
echo $tekst, "<br />";
}
fclose($rukovanje_fajlovima);
?>
Sacuvajte ovaj fajl kao citanje-prebacenih-fajlova.php u folderu htdocs folderu, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/prebacivanje-fajlova/citanje-prebacebih-fajlova.php.
Rezultat:
Napomena: izmena u kodu (iz prethodnom clanka) u fajlu prebacivanje-fajlova.html. Umesto prebacivanje-fajlova.php atributa action, stavicu citanje-prebacenih-fajlova.php.
HTML kod iz prethodnog clanka
<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>
<body>
<h1>Prebacivanje fajlova</h1>
<h3>Prebacivanje fajla poruka.txt</h3>
<form enctype="multipart/form-data"
action="prebacivanje-fajlova.php" method="post">
Prebacivanje fajla: <input name"poruka" type="file">
<br /><br />
<input type="submit" value="Prebacite fajl">
</form>
</body>
</html>
Novi HTML kod
<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>
<body>
<h1>Prebacivanje fajlova</h1>
<h3>Prebacivanje fajla poruka.txt</h3>
<form enctype="multipart/form-data"
action="citanje-prebacenih-fajlova.php" method="post">
Prebacivanje fajla: <input name"poruka" type="file">
<br /><br />
<input type="submit" value="Prebacite fajl">
</form>
</body>
</html>
After you transfer the file, you can access it from PHP. To access the transferred files, you can use the function fopen. Function fopen opens a file or URL.
fopen (file_name, file_mode)
file_name - mandatory parameter. Specifies a file or URL to open.
file_mode - mandatory parameter. Determines the type of access that you need in file.
Possible values:
"r" - read-only. Starts at the beginning of the file.
"w" - just for writing. Opens and clears the contents of the file or creates a new file if it does not exist.
"a" - just for writing. Opens and writes to the end of the file or creates a new file if it does not exist.
"x" - just for writing. Creates a new file. Returns false and error if the file already exists.
"r+" - reading/writing. Starts at the beginning of the file.
"w+" - reading/writing. Opens and clears the contents of the file or creates a new file if it does not exist.
"a+" - reading/writing. Saves the contents of a file by writing at the end of the file.
"x+"- reading/writing. It creates a new file. Returns false and error if the file already exists.
For example, to open the file we will use the function fopen:
<?php
$handling_files = fopen("message.txt","r");
.
.
.
.
?>
Through all the lines of text you can pass with the help of loop while. The loop ends when you reach the end of the file, which is checked with help of function feof:
<?php
$handling_files = fopen("message.txt","r");
while (!feof($handling_files)) {
#code....
}
?>
The text of the file you can read with the help of function fgets:
<?php
$handling_files = fopen("message.txt","r");
while (!feof($handling_files)) {
$text = fgets($handling_files);
echo $text, "<br />";
}
?>
You will close file with the help of function fclose.
<?php
$handling_files = fopen("message.txt","r");
while (!feof($handling_files)) {
$text = fgets($handling_files);
echo $text, "<br />";
}
fclose($handling_files);
?>
Save this file as reading-files.php, in folder htdocs, of the folder xampp. In my case, the path is C:/xampp2/htdocs/PHPtuts/uploading-files/reading-files.php.
Result:
Note: changes in the code (from the previous article) in the file uploading-files.html. Instead of uploading-files.php of attribute action, I'll put reading-files.php.
HTML code from the previous article
<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>
<body>
<h1>Uploading files</h1>
<h3>Uploading file message.txt</h3>
<form enctype="multipart/form-data"
action="uploading-files.php" method="post">
Uploading file: <input name"message" type="file">
<br /><br />
<input type="submit" value="Upload file">
</form>
</body>
</html>
New HTML code
<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>
<body>
<h1>Uploading files</h1>
<h3>Uploading file message.txt</h3>
<form enctype="multipart/form-data"
action="reading-files.php" method="post">
Uploading file: <input name"message" type="file">
<br /><br />
<input type="submit" value="Upload file">
</form>
</body>
</html>
fopen(ime_fajla, pristup_fajla)
ime_fajla - obavezan parametar. Odredjuje fajl ili URL za otvaranje.
pristup_fajla - obavezan parametar. Odredjuje vrstu pristupa koja vam je potrebna u fajlu.
Moguce vrednosti:
."r" - samo za citanje. Pocinje na počctku fajla.
"w" - samo za pisanje. Otvara i brise sadrzaj fajla ili stvara nov fajl ako on ne postoji.
"a" - samo za pisanje. Otvara i pise na kraju fajla ili stvara nov fajl, ako on ne postoji.
"x" - samo za pisanje. Stvara nov fajl. Vraca netacno i gresku ako fajl vec postoji.
"r+" - citanje/pisanje. Pocinje na pocetku fajla.
"w+" - citanje/pisanje. Otvara i brise sadrzaj fajla ili stvara nov fajl ako on ne postoji.
"a+" - citanje/pisanje. Cuva sadrzaj fajla pisanjem na kraju fajla.
"x+" - citanje/pisanje. Stvara nov fajl. Vraca netacno i gresku ako fajl vec postoji.
Primer: za otvaranje fajla upotrebicemo funkciju fopen:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
.
.
.
.
?>
Kroz sve linije teksta mozete proci uz pomoc petlje while. Petlja se zavrsava kada stignete do kraja fajla, sto se proverava pomocu funkcije feof:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
while (!feof($rukovanje_fajlovima)) {
# kod...
}
?>
Tekst iz fajla mozete citati uz pomoc funkcije fgets:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
while (!feof($rukovanje_fajlovima)) {
$tekst = fgets($rukovanje_fajlovima);
echo $tekst, "<br />";
}
?>
Privremeni fajl cete zatvoriti uz pomoc funkcije fclose:
<?php
$rukovanje_fajlovima = fopen("poruka.txt","r");
while (!feof($rukovanje_fajlovima)) {
$tekst = fgets($rukovanje_fajlovima);
echo $tekst, "<br />";
}
fclose($rukovanje_fajlovima);
?>
Sacuvajte ovaj fajl kao citanje-prebacenih-fajlova.php u folderu htdocs folderu, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/prebacivanje-fajlova/citanje-prebacebih-fajlova.php.
Rezultat:
Napomena: izmena u kodu (iz prethodnom clanka) u fajlu prebacivanje-fajlova.html. Umesto prebacivanje-fajlova.php atributa action, stavicu citanje-prebacenih-fajlova.php.
HTML kod iz prethodnog clanka
<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>
<body>
<h1>Prebacivanje fajlova</h1>
<h3>Prebacivanje fajla poruka.txt</h3>
<form enctype="multipart/form-data"
action="prebacivanje-fajlova.php" method="post">
Prebacivanje fajla: <input name"poruka" type="file">
<br /><br />
<input type="submit" value="Prebacite fajl">
</form>
</body>
</html>
Novi HTML kod
<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>
<body>
<h1>Prebacivanje fajlova</h1>
<h3>Prebacivanje fajla poruka.txt</h3>
<form enctype="multipart/form-data"
action="citanje-prebacenih-fajlova.php" method="post">
Prebacivanje fajla: <input name"poruka" type="file">
<br /><br />
<input type="submit" value="Prebacite fajl">
</form>
</body>
</html>
After you transfer the file, you can access it from PHP. To access the transferred files, you can use the function fopen. Function fopen opens a file or URL.
fopen (file_name, file_mode)
file_name - mandatory parameter. Specifies a file or URL to open.
file_mode - mandatory parameter. Determines the type of access that you need in file.
Possible values:
"r" - read-only. Starts at the beginning of the file.
"w" - just for writing. Opens and clears the contents of the file or creates a new file if it does not exist.
"a" - just for writing. Opens and writes to the end of the file or creates a new file if it does not exist.
"x" - just for writing. Creates a new file. Returns false and error if the file already exists.
"r+" - reading/writing. Starts at the beginning of the file.
"w+" - reading/writing. Opens and clears the contents of the file or creates a new file if it does not exist.
"a+" - reading/writing. Saves the contents of a file by writing at the end of the file.
"x+"- reading/writing. It creates a new file. Returns false and error if the file already exists.
For example, to open the file we will use the function fopen:
<?php
$handling_files = fopen("message.txt","r");
.
.
.
.
?>
Through all the lines of text you can pass with the help of loop while. The loop ends when you reach the end of the file, which is checked with help of function feof:
<?php
$handling_files = fopen("message.txt","r");
while (!feof($handling_files)) {
#code....
}
?>
The text of the file you can read with the help of function fgets:
<?php
$handling_files = fopen("message.txt","r");
while (!feof($handling_files)) {
$text = fgets($handling_files);
echo $text, "<br />";
}
?>
You will close file with the help of function fclose.
<?php
$handling_files = fopen("message.txt","r");
while (!feof($handling_files)) {
$text = fgets($handling_files);
echo $text, "<br />";
}
fclose($handling_files);
?>
Save this file as reading-files.php, in folder htdocs, of the folder xampp. In my case, the path is C:/xampp2/htdocs/PHPtuts/uploading-files/reading-files.php.
Result:
Note: changes in the code (from the previous article) in the file uploading-files.html. Instead of uploading-files.php of attribute action, I'll put reading-files.php.
HTML code from the previous article
<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>
<body>
<h1>Uploading files</h1>
<h3>Uploading file message.txt</h3>
<form enctype="multipart/form-data"
action="uploading-files.php" method="post">
Uploading file: <input name"message" type="file">
<br /><br />
<input type="submit" value="Upload file">
</form>
</body>
</html>
New HTML code
<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>
<body>
<h1>Uploading files</h1>
<h3>Uploading file message.txt</h3>
<form enctype="multipart/form-data"
action="reading-files.php" method="post">
Uploading file: <input name"message" type="file">
<br /><br />
<input type="submit" value="Upload file">
</form>
</body>
</html>
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.