Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

PHP File functions!

max_file_size

  • Please log in to reply
7 replies to this topic

#1 Epatron

Epatron

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 13 August 2011 - 02:14 AM


Posted Image
PHP File Functions!
Tutorial by: Epatron

  • 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
?>

Explanation:

$Filehandle = fopen($File, 'w') or die("Can't create or open the file!");

We created variable $Filehandle, which include fopen function. We gave the fopen function two "missions", to open our file ($File) and we reported with PHP that we want to write by character 'w'!
(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:
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 ).
?>

Explanation:
$text_Data = "Hello! I just writed into a file with PHP ! Kinda cool!"; 

We created variable which includes the text "Hello!........" It can be anything like: "Learning a file functions!"! Be creative!
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'
?>

Explanation:
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!
HTML 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>

Little explanation of the html code:
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.
PHP CODE:
PS: Create a another file called "upload.php"
<?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 :(!";
}
?>

Explanation:
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 :(!";
}

We are using move_uploaded_file function, and this function need to know the path where the file will be moved and
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!

Wasn't that hard to code cool thing? Huh? : )
Errors / Questions? Post it below or PM me !
  • 1

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 10:34 AM

Another nice and simple tutorial. Perhaps you can include some checks to restrict the uploads to certain files?
  • 0

#3 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 29 August 2011 - 06:33 AM

Really nice! How do you append to a file? When you open a file to write it erases the contents of the file. What if you want to add to the end of a file? Like if you are creating a running error log or chat log?

And that point about using fclose is really important. Don't forget it (even when reading from files).

Totally unrelated but in this code:


[COLOR=#FF8000]<form action=[COLOR=#0000FF]"upload.php"[/COLOR] enctype=[COLOR=#0000FF]"multipart/form-data"[/COLOR] method=[COLOR=#0000FF]"post"[/COLOR]>[/COLOR] [COLOR=#FF8000]
<input type=[COLOR=#0000FF]"hidden"[/COLOR] name=[COLOR=#0000FF]"MAX_FILE_SIZE"[/COLOR] value=[COLOR=#0000FF]"100000"[/COLOR] />[/COLOR]
 [COLOR=#000080]<b>[/COLOR]Choose your file:[COLOR=#000080]</b>[/COLOR][COLOR=#000080]<br />[/COLOR] [COLOR=#FF8000]
<input type=[COLOR=#0000FF]"file"[/COLOR] name=[COLOR=#0000FF]"file"[/COLOR] />[/COLOR][COLOR=#000080]<br />[/COLOR] 
[COLOR=#FF8000]<input type=[COLOR=#0000FF]"submit"[/COLOR] value=[COLOR=#0000FF]"Upload!"[/COLOR] />
[/COLOR][COLOR=#FF8000]</form>
[/COLOR]

A few things you should always use the name attribute for your form elements (and the form itself). This way you can get the value from $_POST should you need to.
  • 0

#4 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 07 April 2012 - 11:11 AM

Nice tut. My only concern is that it appears the max file size value could be easily bypassed by creating my own form pointing to your upload.php file.
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#5 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 07 April 2012 - 11:13 AM

Yeah that is possible. Whenever you use HTML or anything that the client can see you should ALWAYS validate it server side to make sure they didn't make a local copy to bypass your "restrictions".
  • 0

#6 Jeremy Morgan

Jeremy Morgan

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon

Posted 08 April 2012 - 09:42 PM

Really nice! How do you append to a file? When you open a file to write it erases the contents of the file. What if you want to add to the end of a file? Like if you are creating a running error log or chat log?


This is actually determined when you open the file, and pass the parameter to fopen.

In this line of code:


$Filehandle = fopen($File, 'w') or die("Can't create or open the file!");

Note the "W" in the fopen function. This parameter is what you want to change, here are the most common ones:


r = open file for reading only, pointer is at beginning of the file
r+ = open file for reading and writing, pointer at beginning.

w = open file for writing only, pointer at beginning, but if the file doesn't exist it will be created.
w+ = open file for reading and writing, pointer at beginning, but if the file doesn't exist it will be created.

a = open file for writing only, pointer at end of file
a+ = open file for reading and writing, pointer at end of file, if file doesn't exist it will be created.


So for writing to a log file, you want to use a or a+ as the 2nd parameter in fopen.
  • 0

#7 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1307 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 08 April 2012 - 09:57 PM

There are also file_put_contents () and file_get_contents () , if you rather prefer handling things a file at a time (not recommended for large files).

As for the HTML side of things, they probably don't even need to download a copy of the HTML upload page, they could probably use firebug or something similar to modify the HTML right there. And if they're hacky engough, they could probably even exploit any JavaScript you have on your pages, so client-side checks are not sufficient.
  • 0
Regards,
RR

#8 surajkumardotin

surajkumardotin

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Ludhiana
  • Programming Language:PHP, JavaScript
  • Learning:C, C++, PHP, JavaScript

Posted 18 August 2015 - 11:57 PM

There are many usefull file related functions in php. I have created a post for file functions in php with example code. You can check at the following link.

 

File functions in php


  • 0





Also tagged with one or more of these keywords: max_file_size