Php Script for Uploading Files Image

Php Script for Uploading Files

PHP Script is a standard file upload script with additional features that allow you to change the way and where the files are stored on your web site. For example, you can create a new directory structure and limit access by IP in order to protect your download area.

This php script will allow you to upload files from any server and directory. This is a server-side script, meaning that no scripts (html, etc) will appear on the user’s browser window. You can choose to accept uploads or deny files. It also has an option to delete a certain file after it was successfully uploaded.

Configure the PHP Settings

There are a couple of PHP configuration settings that you’ll want to check beforehand for successful file uploads. In this section, we’ll go through every important option in regards to PHP file upload. These options can be configured in the php.ini file.

If you’re not sure where to find your php.ini file, you can use the php_ini_loaded_file() to locate it. Just create a PHP file on your server with the following line, and open it from the browser.

1<?php echo php_ini_loaded_file(); ?>

Here’s an excerpt from a setup file with some useful defaults.

01020304050607080910111213141516171819; Whether to allow HTTP file uploads.file_uploads = On ; Temporary directory for HTTP uploaded files.; Will use system default if not set.;upload_tmp_dir =  ; Maximum allowed size for uploaded files.upload_max_filesize = 16M ; Maximum number of files that can be uploaded via a single requestmax_file_uploads = 20 ; Maximum size of POST data that PHP will accept.post_max_size = 20M max_input_time = 60memory_limit = 128Mmax_execution_time = 30

The Key Settings

file_uploads

The value of the file_uploads directive should be set to On to allow file uploads. The default value of this directive is On.

upload_max_filesize

The upload_max_filesize directive allows you to configure the maximum size of the uploaded file. By default, it’s set to 2M (two megabytes), and you can override this setting using the .htaccess file as well. Two megabytes isn’t very much by today’s standards, so you might have to increase this. If you get an error that file exceeds upload_max_filesize when you try to upload a file, you need to increase this value. If you do, be sure to also increase post_max_size (see below).

upload_tmp_dir

Sets a temporary directory which will be used to store uploaded files. In most cases, you don’t need to worry about this setting. If you don’t set it, the system default temp directory will be used.

post_max_size

The post_max_size directive allows you to configure the maximum size of POST data. Since files are uploaded with POST requests, this value must be greater than what you’ve set for the upload_max_filesize directive. For example, if your upload_max_filesize is 16M (16 megabytes), you might want to set post_max_size to 20M.

max_file_uploads

It allows you to set the maximum number of files that can be uploaded at a time. The default is 20, a sensible amount.

max_input_time

It’s the maximum number of seconds a script is allowed to parse the input data. You should set it to a reasonable value if you’re dealing with large file uploads. 60 (60 seconds) is a good value for most apps.

memory_limit

The memory_limit directive indicates the maximum amount of memory a script can consume. If you’re facing issues when uploading large files, you need to make sure that the value of this directive is greater than what you’ve set for the post_max_size directive. The default value is 128M (128 megabytes), so unless you have a very large post_max_size and upload_max_filesize, you don’t need to worry about this.

max_execution_time

It’s the maximum number of seconds a script is allowed to run. If you’re facing issues when uploading large files, you can consider increasing this value. 30 (30 seconds) should work well for most apps.

The HTML Form

First, we’ll create an HTML form that the user will see when they want to upload the file. Create a new folder for this example project, and within it, create an index.html file with the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP File Upload</title>
</head>
<body>
    <form action="fileUploadScript.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" name="the_file" id="fileToUpload">
        <input type="submit" name="submit" value="Start Upload">
    </form>
</body>
</html>

A couple important things to notice in the example above:

  • action="fileUploadScript.php" – This references the PHP script that will handle the file upload on the backend
  • method="post" – This tells the browser action the form will use when sending the file to the server (for uploads, this is almost always a POST action, sometimes a PUT)
  • enctype="multipart/form-data" – This determines the content-type that the form submits

Next, open your terminal and from the directory where you created the file, start the PHP server:

php-file-upload

Then, open your web browser and go to localhost:1234. You should see something like this:

php-file-upload

PHP file upload configuration

PHP has some important options that control the file upload. These options are in the php.ini file. If you don’t know where to find your php.ini file, you can use the php_ini_loaded_file() function as follows:

<?php


echo php_ini_loaded_file();Code language: HTML, XML (xml)

It’ll return the following file path if you use XAMPP on Windows:

C:\xampp\php\php.iniCode language: CSS (css)

Here are the important settings for file uploads in the php.ini file:

; Whether to allow HTTP file uploads.
file_uploads=On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir="C:\xampp\tmp"

; Maximum allowed size for uploaded files.
upload_max_filesize=2M

; Maximum number of files that can be uploaded via a single request
max_file_uploads=20Code language: plaintext (plaintext)

file_uploads

The file_upload directive should be On to allow file upload. It defaults to On.

upload_max_filesize

The upload_max_filesize specifies the maximum size of the uploaded file. By default, it’s 2M (MB). If you get an error saying that the file exceeds upload_max_filesize, you need to increase this value.

upload_tmp_dir

The upload_tmp_dir specifies the directory that stores the uploaded files temporarily.

post_max_size

The post_max_size specifies the maximum size of the POST data. Because you’ll upload files with the POST request, you need to make sure that the post_max_size is greater than upload_max_size.

max_file_uploads

The max_file_uploads directive limits the number of files that you can upload at a time.

Conclusion

Take the headache out of managing your files with this easy-to-use php script for uploading multiple files.

Similar Posts

0 Comments

No Comment.