Previzualizare curs:

Extras din curs:

Pentru a incepe lucrul, trebuie mai intai sa deschidem fisierul, folosind functia fopen(),

careia ii atasam un pointer (o variabila ce reprezinta conexiunea logica cu fisierul).

<?php

$handle = fopen("/home/rasmus/file.txt", "r");

$handle = fopen("/home/rasmus/file.gif", "wb");

$handle = fopen("http://www.example.com/", "r");

$handle = fopen("ftp://user:password@example.com/somefile.txt", "w");

?>

Pentru windows, se dubleaza backslash –ul :

<?php

$handle = fopen("c:datainfo.txt", "r");

?>

Parametrii de legatura cu fisierul sunt conform tabelului :

'r' Open for reading only; place the file pointer at the beginning of the file.

'r+' Open for reading and writing; place the file pointer at the beginning of the file.

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to

zero length. If the file does not exist, attempt to create it.

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate

the file to zero length. If the file does not exist, attempt to create it.

'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.

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not

exist, attempt to create it.

Citirea din fisier pentru tip de date alfanumeric se face folosind functia fgets (), pentru

citire de stringuri (pana la primul newline) :

<?php

$handle = fopen("/tmp/inputfile.txt", "r");

while (!feof($handle)) {

$buffer = fgets($handle, 4096);

echo $buffer;

}

fclose($handle);

?>

Citirea pentru tip de date binary se face folosind functia fread() , functie ce trebuie sa

aiba ca parametrii de intrare calea catre fisier si numele fisierului, urmat de dimensiunea

pana unde citim (in bytes).

<?php

// get contents of a file into a string

$filename = "/usr/local/something.txt";

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));

fclose($handle);

?>

Pe sistemele de operare ce nu fac diferenta intre tip de date text sau binar (Windows),

fisierul trebuie deschis cu optiunea b de la binary.

<?php

$filename = "c:filessomepic.gif";

$handle = fopen($filename, "rb");

$contents = fread($handle, filesize($filename));

fclose($handle);

?>

Pentru scriere folosind caractere alfanumerice se foloseste functia fputs() care este un

alias la fwrite(), functie folosita pentru date de tip binar.

<?php

$filename = 'test.txt';

$somecontent = "Add this to the filen";

// Let's make sure the file exists and is writable first.

if (is_writable($filename)) {

// In our example we're opening $filename in append mode.

// The file pointer is at the bottom of the file hence

// that's where $somecontent will go when we fwrite() it.

if (!$handle = fopen($filename, 'a')) {

echo "Cannot open file ($filename)";

exit;

}

// Write $somecontent to our opened file.

if (fwrite($handle, $somecontent) === FALSE) {

echo "Cannot write to file ($filename)";

exit;

Download gratuit

Documentul este oferit gratuit,
trebuie doar să te autentifici in contul tău.

Structură de fișiere:
  • Curs 6 - PHP.pdf
Alte informații:
Tipuri fișiere:
pdf
Nota:
5/10 (1 voturi)
Nr fișiere:
1 fisier
Pagini (total):
2 pagini
Imagini extrase:
2 imagini
Nr cuvinte:
494 cuvinte
Nr caractere:
2 767 caractere
Marime:
12.04KB (arhivat)
Publicat de:
NNT 1 P.
Nivel studiu:
Facultate
Tip document:
Curs
Domeniu:
Limbaje de Programare
Predat:
la facultate
Materie:
Limbaje de Programare
Sus!