Curs 6 - PHP

Imagine preview
(5/10 din 1 vot)

Acest curs prezinta Curs 6 - PHP.
Mai jos poate fi vizualizat un extras din document (aprox. 2 pagini).

Arhiva contine 1 fisier pdf de 2 pagini .

Iti recomandam sa te uiti bine pe extras si pe imaginile oferite iar daca este ceea ce-ti trebuie pentru documentarea ta, il poti descarca.

Fratele cel mare te iubeste, acest download este gratuit. Yupyy!

Domeniu: Limbaje de Programare

Extras din document

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;

Fisiere in arhiva (1):

  • Curs 6 - PHP.pdf