05-10-2012, 12:03 PM | #1 |
Registered User
Join Date: May 2012
Posts: 4
|
PHP File functions
Create a file
Write into a file Overwriting Read a file Upload a file Create a file: Before you start reading, please notice that I'm from Finland! Hey! At this tutorial you will learn a lot ! How to create a file, write into a file, read a file and upload a file with PHP! We will start at the creating a file! It's very simple! We will use functions called fopen and fclose. Code:
<?php $File = "examplefile.txt"; // < File's name will be "examplefile" and it will be text type ".txt" $Filehandle = fopen($File, 'w') or die("Can't create or open the file!"); fclose($Filehandle); // < We close the file handle with function fclose ?> Code:
$Filehandle = fopen($File, 'w') or die("Can't create or open the file!"); (PS. Remember functions fopen and fclose) Write into a file: Now you know how to create / open and close the file, let's get into writing to file! Writing to file is useful way to eg. keep up on the chat log, purchase log or user register log. We will use fwrite function to write into a file! CODE: Code:
Remember to include the previous code ! <?php $text_Data = "Hello! I just writed into a file with PHP ! Kinda cool!"; fwrite($FileHandle,$text_Data); fclose($File); //< Remember to move the fclose function to the end of the code ( AL WA YS ). ?> Code:
$text_Data = "Hello! I just writed into a file with PHP ! Kinda cool!"; fwrite($FileHandle,$text_Data); Now we're using fwrite function, it uses the $FileHandle variable and writes the variable's ($text_Data) text into a text file! Now if you tried to write a couple times with this same code, you might notice that it overwrites the previous texts, because 'w' code always starts writing from the beginning of the file. Try to change code 'w' to 'a' and the writings will start at the end of the file and won't overwrite the previous texts! Read a file: Now you know how to create / open, write and close the file, It's time to learn how to read the file and show it in the web page! Code:
<?php $File = "examplefile.txt"; $FileHandle = fopen($File, 'r'); // < Changed the 'w' to 'r' ?> You might notice its the exactly the same code as we learned first (Creating / open the file). But now we changed the 'w' to 'r' ('r' = Read) ! So now the fopen function will read the variable's file ($File)! Upload a file: Uploading a file is a little more harder, because we are going to use HTML and PHP together and PHP code is little more harder. We will start at the creating a HTML upload FORM! Code:
<form action="upload.php" enctype="multipart/form-data" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <b>Choose your file:</b><br /> <input type="file" name="file" /><br /> <input type="submit" value="Upload!" /></form> action="upload.php" = When submit button is pressed it will lead us to upload.php page! enctype="multipart/form-data" = We will need this enctype for making our PHP functions working! input type="hidden" name="MAX_FILE_SIZE" value="100000" = This code makes us maxium file size which is 100KB. PS: Create a another file called "upload.php" Code:
<?php $file_path = "urfolder/"; // Put your folder where you want to save the files! $file_path = $file_path . basename( $_FILES['file']['name']); //Files place will be urfolder/filesname.filestype. if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) { echo "Your file has been uploaded! Yay!"; }else{ echo "Error! At uploading your file, sorry :(!"; } ?> Code:
if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) { echo "Your file has been uploaded! Yay!"; }else{ echo "Error! At uploading your file, sorry :(!"; } the path of temporary file! With this function we're using if function, so if uploaded file can be moved to urfolder/ you will get message "Your file has been uploaded! Yay!" else you will get error message!
__________________
Make Money Online by locking links - bee4.biz/?ref=17978 |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Importance of Php Development for Web Development | tech.biztech | Programming General | 35 | 11-22-2019 12:37 AM |
php 3 pdf file? | janejackson | PHP / mySQL | 0 | 04-05-2012 11:23 PM |
Are you aware of the fact that PHP programming is the most widely used one? | johnrichards774 | PHP / mySQL | 3 | 02-07-2012 05:41 AM |
form to file script. php or html | mjvndhsb | PHP / mySQL | 0 | 10-07-2011 08:55 PM |
is it possible to make an php file trough a php code | ncjbhd | PHP / mySQL | 0 | 09-21-2011 11:49 PM |