Skip to content Skip to sidebar Skip to footer

Allow User to Upload and Delete Files Php

Read Fourth dimension: 8 mins Languages:

In this tutorial, we are going to learn file handling in PHP. I'll show you lot how to create, read, write, and delete files in PHP past using the built-in file handling functions.

File handling is something that you volition need to do very frequently as a PHP developer.

You could utilize PHP file handling functions to manipulate files in dissimilar ways. These functions could be used to build features in your applications that range from custom error logging to storing cached files. Examples of utility tools that yous could build with these functions are:

  • custom logging and debugging tools
  • application configuration storage
  • forepart-end and application caching
  • localization back up
  • and many more

PHP provides several file handling functions that allow you to perform various operations, like:

  • create and open a file
  • write into a file
  • read from a file
  • delete a file
  • close a file

Today, nosotros'll go through each and every file performance, along with examples of how to use them. I would encourage you to try out the examples in this tutorial as you follow along, and so that y'all tin really acquire and understand how they work. And if something doesn't work as expected for you, feel free to post your queries by using the feed at the end of this tutorial.

How to Create and Open up a File

In this section, nosotros'll come across how to create and open a file.

When information technology comes to creating a file, information technology'southward the fopen function which you'll terminate up using nearly of the time. It may seem a scrap confusing to use the fopen office to create a file. In fact, the fopen function does 2 things: it creates a file if it doesn't exist and also opens it for reading or writing.

Permit's go through the following case to understand how it works.

In the above case, the fopen function volition check if the /dwelling/tutsplus/files/tmp.txt file exists, and if it exists, information technology'll open up it for writing. By supplying 'w' in the second statement, we specify that nosotros volition exist writing to the file. If the file doesn't be, it'll be created right away. It'due south of import to note here that the /home/tutsplus/files/ directory in the above case must be writable by the spider web server user for the fopen office to be able to create a file.

The commencement argument of the fopen part is the filename which you want to open. In the above example, we've supplied the /abode/tutsplus/files/tmp.txt filename in the first argument. Again, it's important to note that we've supplied an absolute path name.

The 2d argument is the mode, which specifies the type of access y'all require to the opened file. The fopen function provides different modes yous tin choose from. For example:

  • apply the r mode to open a file for reading
  • the r+ way for both reading and writing
  • a mode for reading and appending

In our case, we've used the w mode, which opens the /home/tutsplus/files/tmp.txt file for writing only. Feel free to go through the official documentation to see different modes supported by the fopen function.

fopen returns a file arrangement pointer, which is used for the other file functions like reading and writing.

How Can I Open Remote Files?

The fopen function isn't just for local files. It as well supports other protocols and can open files from elsewhere on your network or the web. If y'all've enabled the allow_url_fopen directive in PHP, you could open remote files as well.

Information technology's actually important to note that when you enable the allow_url_fopen directive, you lot are creating some security risks, since it opens the door for remote file execution and other attacks. So make sure you take extra security measures in your awarding if you're going to enable this directive.

How to Write to a File With PHP

There are a couple of different means y'all can write to a file with PHP.

The fwrite Function

Get-go and foremost is the fwrite office, which allows you to write string contents to the file stream referenced by the file handle. Let's go through the following example to understand how it works.

Start, we've opened the /home/tutsplus/files/tmp.txt file with the a+ mode, which opens it for reading and writing, with the file pointer placed at the cease of the file. Thus, our content will exist appended to the end of the file, after whatever other contents. Next, we've used the fwrite function to write a string.

The offset argument of the fwrite function is the file organisation pointer returned by fopen—this is how fwrite knows where to write into. And the second statement is a string which we want to write into a file. As you tin see in the in a higher place example, you can use the fwrite function multiple times to write a series of strings before you shut the file.

Finally, nosotros've used the fclose function to close the file. It takes only ane statement, the file pointer that you want to shut. It'south ever a skillful practice to shut files by using the fclose role once you've finished with your file operations.

The file_put_contents Part: A PHP Shortcut for Writing to Files

In the previous section, we discussed the fwrite function, which is used to write into a file. You would have noticed that, if you desire to write into a file, you demand to open it with the fopen function in the first place. After that, you need to use the fwrite function to write your data into a file, and finally you demand to use the fclose part to close the file.

If that sounds too much to you lot, there's a shortcut: file_put_contents. The file_put_contents function allows yous to write data to a file in a single call.

Let'south see how information technology works.

The first argument of the file_put_contents function is a filename, and the second argument is a string which you lot want to write into a file. If the file doesn't exist, information technology'll be created.

As you can see, the file_put_contents function is a shortcut when you lot just desire to write a piece of data to a file.

How to Read From a File in PHP

Now you know how to create and write into a file. In this section, I'll testify you how to read from a file.

The fread Function

The fread function allows you to read from a file. It's like to fwrite, but you demand to provide the length in bytes y'all want to read.

Let'southward have a wait at the following example to understand how it works.

Equally we want to read from the /home/tutsplus/files/tmp.txt file, nosotros've opened information technology with the r mode. Next, we've used the fread function to read all the contents of the file into the $content variable.

The start argument of the fread function is the file system arrow, and so that it knows where to read from. The 2nd argument is the length in bytes y'all want to read from a file. In our case, nosotros want to read all the contents of the /domicile/tutsplus/files/tmp.txt file, and thus we've used the filesize function to measure out the size of the file.

The file_get_contents Function: A PHP Shortcut for Reading From Files

In the previous section, we discussed the fread function, which allows you to read a file by specifying the length in bytes you lot want to read. If you want to read an unabridged file at once, there's a function which allows you lot to practice just that:file_get_contents.

Let's see the file_get_contents function in activeness!

As you tin run into, it's pretty straightforward to use the file_get_contents function—just provide a filename in the kickoff statement.

How to Delete a File in PHP

In this final section, we'll meet how yous tin can delete files. To delete a file in PHP, utilize the unlink function. Permit'southward go through an example to encounter how it works.

The first statement of the unlink part is a filename which you want to delete. The unlink part returns either Truthful or False, depending on whether the delete operation was successful.

Determination

In this postal service, we discussed the basics of file handling in PHP. Throughout the commodity, we discussed different operations you can perform past using file functions in PHP. If yous have any queries or suggestions, feel costless to post them using the feed beneath!

Learn PHP With a Free Online Course

If y'all want to learn PHP, bank check out our free online course on PHP fundamentals!

In this course, you'll learn the fundamentals of PHP programming. Y'all'll starting time with the basics, learning how PHP works and writing unproblematic PHP loops and functions. Then you'll build up to coding classes for uncomplicated object-oriented programming (OOP). Along the fashion, yous'll learn all the well-nigh important skills for writing apps for the web: you'll get a run a risk to practise responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.

Did you find this post useful?

henriquessolerho.blogspot.com

Source: https://code.tutsplus.com/tutorials/create-write-read-and-delete-files-in-php--cms-34950

ارسال یک نظر for "Allow User to Upload and Delete Files Php"