Text Files File writing File reading Files n
- Slides: 20
Text Files File writing File reading
Files n n n USE DATABASE for data storing on server side PHP can create, write and read files on server side only Normally there are no rights to write files (umask = 0022) Files are used to logging and other special purposes Manipulating files is a basic necessity for serious programmers and PHP gives you a great deal of tools for creating, uploading, and editing files.
File Create n n n In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file (i. e. read from the file, write information, etc). $our. File. Name = "test. File. txt"; $our. File. Handle = fopen($our. File. Name, 'w') or die("can't open file"); fclose($our. File. Handle);
File Handle n $our. File. Name = "test. File. txt"; Here we create the name of our file, "test. File. txt" and store it into a variable $our. File. Name. n $our. File. Handle = fopen($our. File. Name, 'w') or die("can't open file"); This bit of code actually has two parts. First we use the function fopen and give it two arguments: our file name and we inform PHP that we want to write by passing the character "w". n Second, the fopen function returns what is called a file handle, which will allow us to manipulate the file. We save the file handle into the $our. File. Handle variable. We will talk more about file handles later on. n fclose($our. File. Handle); We close the file that was opened. fclose takes the file handle that is to be closed. We will talk more about this more in the file closing lesson.
File Handle n n If you are trying to get a file writing program to run and you are having errors, you might want to check that you have granted your file access to write information to the server’s hard drive. Setting permissions is most often done with the use of an FTP program to execute a command called CHMOD. Use CHMOD to allow the PHP file to write to disk, thus allowing it to create a file.
File Properties AND SECURITY ONLY IN TESTING! CGI-ohjelmoiti No write access to file folders Check all input data on forms Use mysql_real_escape_string()-function Send data only to hard coded urls Don’t use get on forms NORMAL!
File Permission For example, let’s say you want file data. txt to have these permissions n n n rwx r-- info. sh => you group other Under each letter, write a digit 1; under each dash write a digit zero. Ignore the dash at the very beginning that tells you whether it’s a file or directory. This gives you three binary numbers. rwx r-- info. sh becomes 111 100 Now convert each set of three digits to a single digit using this table: the 111 100 translates to the number 754. n Now use that number in a chmod command to set your desired permissions on the file: chmod 754 info. sh 111 = read, execute, and write access to the user (that's you) 101 = read and execute access to the group 100 = read access to others
File Open For many different technical reasons, PHP requires you to specify your intentions when you open a file. Below are three basic ways to open a file and the corresponding character that PHP uses. n n n Read: 'r' Open a file for read only use. The file pointer begins at the front of the file. Write: 'w' Open a file for write only use. In addition, the data in the file is erased and you will begin writing data at the beginning of the file. This is also called truncating a file, which we will talk about more in a later lesson. The file pointer begins at the start of the file. Append: 'a' Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file. http: //catcode. com/teachmod/index. html
Advanced fopen
File Writing lines into a file called data. txt: $file. Name="data. txt"; $fp = @fopen($file. Name, "w"); if(is_resource($fp)){ //file open ok? fwrite($fp, $Course. "n"); fwrite($fp, $Number. "n"); for($i=0; $i<$Number; $i++){ fwrite($fp, $Names[$i]. "n"); fwrite($fp, $Groups[$i]. "n"); fwrite($fp, $Points[$i]. "n"); fwrite($fp, $Grade. Desc[$i]. "n"); } fclose($fp); }else{ echo(”Problems”); }
File Reading lines from a file called data. txt. Length of one liune can be 4096 chars at maximum. $file. Name="data. txt"; $fp = @fopen($file. Name, "r"); if(is_resource($fp)){ $Course=fgets($fp, 4096); $Number=fgets($fp, 4096); $i=0; while(!feof($fp)){ $Names[$i]=fgets($fp, 4096); //4096 is NOT NEEDED! $Groups[$i]=fgets($fp); // OK! $Points[$i]=fgets($fp); $Grade. Desc[$i]=fgets($fp); $i++; } fclose($fp); }else{ echo(”Problems”); }
$file. Name="visitors. dat"; $fp = fopen($file. Name, "r"); $text=fgets($fp, 4096); fclose($fp); echo("<p>Number of visitors: $text</p>"); //Type casting from string to integer $number= (int)$text; $number++; echo("<p>New number of visitors: $number</p>"); $fp = fopen($file. Name, "w") or die("can't open file $file. Name"); fwrite($fp, $number. "n"); fclose($fp); Visitors $time. Stamp = date("Y-m-d H: i: s"); $ip = $_SERVER['REMOTE_ADDR']; $browser = $_SERVER['HTTP_USER_AGENT']; $connection = gethostbyaddr($REMOTE_ADDR); $fil 1 = $SERVER_NAME; $fil 2 = $REQUEST_URI; echo("<h 4>Data of you: </h 4>". "<p>". "ip: $ip</br>". "browser: $browser</br>". "connection: $connection</br>". " server name: $fil 1</br>". " request url: $fil 2, </br></p>");
//HERE file name does NOT exist $file. Name="No. File. txt"; file error $handle = fopen($file. Name, 'r'); //write a line, n is a new line character in a file echo(fgets($handle). " "); echo("File wreading is ok. "); fclose($handle); Warning: fopen(No. File. txt) [function. fopen]: failed to open stream: No such file or directory in D: Eclipse_WorkspaceW 13 Pictures. Files3. File. Dief. Read. php on line 15 Warning: fgets(): supplied argument is not a valid stream resource in D: Eclipse_WorkspaceW 13 Pictures. Files3. File. Dief. Read. php on line 17 Warning: fgets(): supplied argument is not a valid stream resource in D: Eclipse_WorkspaceW 13 Pictures. Files3. File. Dief. Read. php on line 18 Warning: fgets(): supplied argument is not a valid stream resource in D: Eclipse_WorkspaceW 13 Pictures. Files3. File. Dief. Read. php on line 19 Warning: fclose(): supplied argument is not a valid stream resource in D: Eclipse_WorkspaceW 13 Pictures. Files3. File. Dief. Read. php on line 21 // Each line causes error message !
or die(”Can’t open file”) //HERE file name does NOT exist $file. Name="No. File. txt"; //DON'T give additional information to a hacker $handle = fopen($file. Name, 'r') or die(); Message given: Warning: fopen(No. File. txt) [function. fopen]: failed to open stream: No such file or directory in D: Eclipse_WorkspaceW 13 Pictures. Files2. File. Dief. Read. php on line 15 Exceution is stopped after the first error!
is_resource //HERE file name does NOT exist $file. Name="No. File. txt"; if(is_resource($handle = fopen($file. Name, 'r'))){ //write a line, n is a new line character in a file echo(fgets($handle). " "); echo("File wreading is ok. "); fclose($handle); }else{ //TURN ALSO WARNINGS OFF IN php. ini echo("Something else is done"); } Warning: fopen(No. File. txt) [function. fopen]: failed to open stream: No such file or directory in D: Eclipse_WorkspaceW 13 Pictures. Files4. File. Resourcef. Read. php on line 15 Something else is done is_resource() checks whether value is a resource pointer that has been returned by a call to fopen() , popen() , opendir() , etc. , or one of the database functions such as mysql_connect() , msql_pconnect() , etc. If so, the function returns TRUE. If not, FALSE is returned.
Suspress fopen warning $file. Name="No. File. txt"; //@ character suspresses warning $handle = @fopen($file. Name, 'r'); //Study if opening ok if(is_resource($handle)){ //write a line, n is a new line character in a file echo(fgets($handle). " "); echo("File wreading is ok. "); fclose($handle); //Remember to refresh your PROJECT in Eclipse }else{ echo("Something else is done"); } PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
Securing php step by step See article: http: //www. securityfocus. com/infocus/1706 Study carefully php. ini file http: //www. reallylinux. com/docs/php. ini or on your own server.
Graphics <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Transitional//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 transitional. dtd"> <html xmlns="http: //www. w 3. org/1999/xhtml" lang="en_US" xml: lang="en_US"> <head> <title> Graphics</title> </head> <body> <img src="grid. php" alt="Line graphics"/> <h 2>Line graphics</h 2> <img src="bar. php" alt="Bar diagram"/> <h 2>Bar diagram</h 2> </body> </html>
Bars <? //1. Add values to the graph $graph. Values=array(0, 80, 23, 11, 190, 245, 50, 80, 111, 240, 55); //2. Define. PNG image //6. Create bar charts header("Content-type: image/png"); $img. Width=250; for ($i=0; $i<10; $i++){ $img. Height=250; imagefilledrectangle($image, $i*25, (250// 3. Create image and define colors $graph. Values[$i]), ($i+1)*25, 250, $image=imagecreate($img. Width, $img. Height); $color. Dark. Blue); $color. White=imagecolorallocate($image, 255, 255); $color. Grey=imagecolorallocate($image, 192, 192); $color. Dark. Blue=imagecolorallocate($image, 104, 157, 228); imagefilledrectangle($image, ($i*25)+1, (250$color. Light. Blue=imagecolorallocate($image, 184, 212, 250); $graph. Values[$i])+1, (($i+1)*25)-5, 248, //4. Create border around image $color. Light. Blue); imageline($image, 0, 0, 0, 250, $color. Grey); } imageline($image, 0, 0, 250, 0, $color. Grey); //7. Output graph and clear image from memory imageline($image, 249, 0, 249, $color. Grey); imageline($image, 0, 249, $color. Grey); imagepng($image); //5. Create grid //imagepng($image, "koe. png"); for ($i=1; $i<11; $i++){ imagedestroy($image); imageline($image, $i*25, 0, $i*25, 255, $color. Grey); ? > imageline($image, 0, $i*25, 255, $i*25, $color. Grey); } NO file saving is needed
GD Library /*PHP has the amazing ability to create dynamic images on the fly using * server-side scripts. The basis for this functionality is the GD Library, * an ANSI C library designed by Thomas Boutell. The library supports most * popular image file formats except for. GIF files (although the author * promises to add. GIF support once the LZW patent expires on July 7, 2004). * The GD Library is integrated in PHP versions 4. 3 and above. * If you are using an older version of PHP, you will have to install * the graphics support manually. Plenty of information is available on setup. */ http: //fi. php. net/gd
- File mode python
- Cjis security awareness
- Ncic hosts restricted files and non-restricted files
- Making connections images
- File-file yang dibuat oleh user pada jenis file di linux
- While reading activities
- What does a markup tag tells the web browser
- Difference between logical file and physical file
- Fungsi sistem file
- Distributed file system definition
- In a file-oriented information system, a transaction file
- Round robin reading vs popcorn reading
- Difference between silent reading and reading aloud
- What is post reading stage
- Edb net
- Is an active process of discovery
- Real definition of extensive reading
- Intensive reading and extensive reading
- Types of reading skill
- Bmi emulator
- Text file