GithubHelp home page GithubHelp logo

lividavi19 / php-file-uploader Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 156 KB

PHP utility script for uploading files to a server

License: MIT License

PHP 100.00%
file files php upload uploader utility file-system file-sharing file-upload file-uploader php-file-upload php-library php-utility

php-file-uploader's Introduction

PHP File Uploader ๐Ÿ“ ๐Ÿ“‚

This is a utility script that helps in uploading files to the server using PHP programming language. People uploading files to server

Installation

To start uploading files with this library, download the file-uploader.php file, then include it in your code ๐Ÿ‘‡

require_once "file-uploader.php";

Or ๐Ÿ‘‡

include_once "file-uploader.php";

Arguments

After including the file-uploader.php in your code, you need to invoke upload() function, passing to it the $file argument and an optional $uploadFolder argument ๐Ÿ‘‡

$file = $_FILES["html_file_input"];
$uploadFolder = "my_uploading_folder";

upload ($file, $uploadFolder);

$file

This should be the first argument, it is the file you are trying to upload. This argument is mandatory.

Important

This argument mut be a file, otherwise the function returns an empty string "" as shown in the next two code snippets ๐Ÿ‘‡

// Calling upload() with no argument
upload ();
// Calling upload() with a non-file as an argument
$myVariable = "String type";
upload ($myVariable);

$uploadFolder

This should be the second argument of the function. It specifies the folder you want to save your file into. This is an option argument.

Note

If not specified the script will attempt to upload the file to the current-working-directory.

Configurations

There are few a stuffs you need to do to configure the library to work depends on your specific uses cases. Things like setting the specifying file types and also maximum file size allowed for upload.

Allowed File Types

By default the library supports upload of PNG JPG JPEG images and PDF documents. But your project might be supporting different range of file types than the default ones. To support different file types follow steps below ๐Ÿ‘‡

  • Open file-uploader.php file
  • Locate $EXTENSIONS_ALLOWED variable in that file
  • Initialize it to an array of strings, representing file types you need to support
  • Following example supports only PNG JPG JPEG image files ๐Ÿ‘‡
$EXTENSIONS_ALLOWED = ["PNG", "JPG", "JPEG"];

Maximum File Size

Default maximum size allowed is set to 2MB for this library. To change it to a different size do the following ๐Ÿ‘‡

  • Open file-uploader.php file
  • Locate $MAX_SIZE_ALLOWED variable in that file
  • Change the number outside the brackets to a maximum file size (in MB) you want to allow
  • Following example set maximum size allowed for upload to 10MB ๐Ÿ‘‡
$MAX_SIZE_ALLOWED = 10 * (1024 * 1024);

Successful Upload

Upon successfull upload of the file, this function returns name of the file with it's extension appended to it, for example uploaded-document.pdf. In code snippet below, the $fileName will have name of the uploaded file. You can further-use this filename in your code, for-instance saving it to the database, etc ๐Ÿ‘‡

$file = $_FILES["html_file_input"];

// The $fileName below will have name of the file uploaded
$fileName = upload ($file);

if ($fileName) {
	// Print info about uploaded file
	echo "Uploaded file is {$fileName}";
} else {
	// Prompt client that upload failed
	echo "Upload failed!";
}

๐Ÿ‘‰ ๐Ÿ‘‰ ๐Ÿ‘‰ Uploaded file is uploadedFileName.png

Unsuccessful Upload

If upload was not successful, the function will return an empty string "" In code snippet below, the $fileName will have an empty string stored in it. You can proceed with the execution of your code depending on this value, for-instance prompting users that file upload failed ๐Ÿ‘‡

// The upload() function expects a file as the first argument
// Since we are calling the function with a string as the first argument
// Then upload process will fail, and the upload() function returns an empty string

$myVariable = "String type";
$fileName = upload ($myVariable); // empty string ""

if ($fileName) {
	// Print info about uploaded file
	echo "Uploaded file is {$fileName}";
} else {
	// Prompt client that upload failed
	echo "Upload failed!";
}

๐Ÿ‘‰ ๐Ÿ‘‰ ๐Ÿ‘‰ Upload failed!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.