Automated file upload with PHP made easy
- Created at:
- Updated at:
File uploading is a process that enables web applications and sites to accept and manage user files, such as images, PDFs, and videos. PHP is a popular programming language for server-side applications. It can help upload, verify, and validate files uploaded by clients. You can build your own file upload script in PHP in just a few minutes.
However, when scripting a PHP upload solution yourself, you need to take care of additional considerations:
- Files uploaded by users need to be stored somewhere, and if there are a large number of files they can exhaust your storage space.
- If you need to deliver these files to users over the Internet, your local server might not provide sufficient performance or bandwidth and might not have the proper CDN setup.
- Security is another concern - files uploaded by users could contain malware or be used in other cyber attacks such as local file inclusion (LFI).
All these issues can be addressed by dedicated file upload services. In this article, I’ll show the basic file upload mechanism provided by PHP and review two managed solutions that can help you set up a large-scale, secure file upload solution.
How does PHP file upload work?
You can use PHP scripts with HTML forms to allow users to upload files to your server. The file is first uploaded to a temporary directory and then relocated to the target location via a PHP script. Uploaded files can be text files, image files, or any document.
The phpinfo.php page provides information on the temporary directory used for file uploads, known as upload_tmp_dir and the maximum permitted size of files that can be uploaded, which is called upload_max_filesize. These parameters are set in the php.ini configuration file.
The file upload process in PHP follows these steps:
- The user opens a page containing a text file, a browse button, and an HTML form with a submit button.
- The user clicks the browse button and selects the file to upload from their local PC.
- The full path to the selected file is displayed in a text field and the user clicks the submit button.
- The selected files are transferred to a temporary directory on the server.
- The PHP script specified as the form handler in the form's action attribute checks if the file has arrived and then copies the file to the desired directory.
- The PHP script confirms success to the user.
PHP file uploads with Cloudinary
Cloudinary is a cloud-based image and video management platform. One of its features is fully managed PHP file upload, which supports PHP and many other languages and frameworks.
The general process works as follows:
- You install the Cloudinary PHP SDK.
- You add Cloudinary’s file upload widget to your PHP application, or implement file upload using convenient upload helper methods.
- Users upload files.
- Files are stored in cloud storage by Cloudinary.
- You can deliver files to users via fast CDN.
- Cloudinary lets you manage and administrate files via API and an extensive web UI.
- For images and video, the platform lets you automatically modify the uploaded images and video to any file format, shape or size.
Note that Cloudinary’s file upload functionality requires a server that runs PHP 5.3 or later.
Here is how to add Cloudinary-managed file upload to your PHP application, step by step:
- Sign up for Cloudinary’s free-forever tier.
- Include the required Cloudinary files:
require 'Cloudinary.php';
require 'Uploader.php';
require 'Helpers.php';
require 'Api.php';
- Set up the key values with the config method so that Cloudinary can verify that your account is valid:
\Cloudinary::config(array(
"cloud_name" => "my_cloud_name",
"api_key" => "my_api_key",
"api_secret" => "my_api_secret"
));
- Now upload your images and other files to Cloudinary’s cloud platform using the following lines of code:
Upload a local file:
\Cloudinary\Uploader::upload("/home/my_image.jpg")
Upload a file from a remote HTTP/S URL:
\Cloudinary\Uploader::upload("https://www.example.com/image.jpg")
Upload a file from an S3 bucket:
\Cloudinary\Uploader::upload('s3://my-bucket/my-path/my-file.jpg');
See more file upload options in the Cloudinary documentation.
PHP file uploads with Uploadcare
Uploadcare File Uploader is a mobile-friendly, responsive HTML5 website solution that allows users to select and upload multiple files from a variety of sources. It also includes an in-browser image editor. You can customize its design to suit your website look and feel.
Key features of File Uploader include:
- Supports files up to 5TB in size
- Supports upload sources such as local storage, cameras, social media, and cloud storage services.
- Multiple file upload
- Progress bar showing upload progress for each file
- Speeds up upload speeds using caching, similar to a CDN
Uploadcare supports pure HTML/JavaScript, so you can use it without PHP. However, if your website is built on PHP, the solution provides a well-documented PHP API.
Prerequisites. See this guide to learn how to install the Uploadcare library, get an API key, and fetch the PHP library via Composer.
Adding Uploadcare to your page. The following code shows how to use Uploadcare to pull images to a storage server supporting CDN:
<head>
<script>
UPLOADCARE_PUBLIC_KEY = 'YOUR_PUBLIC_KEY';
</script>
<?php echo $api->widget->getScriptTag(); ?>
</head>
<body>
<form method="POST" action="index.php">
<?php echo $api->widget->getInputTag('qs-file'); ?>
<input type="submit" value="Save!" />
</form>
</body>
When you run this code in your browser, the widget is displayed and users can immediately start uploading. Of course, it is recommended to customize the upload widget to your website’s look and feel.
Conclusion
In this article, I explained the basics of PHP file upload and showed how to easily implement file upload with two managed solutions:
- Cloudinary - lets your users upload files to cloud storage, delivers files over CDN, and enables rich transformations and management capabilities for images and video.
- UploadCare - provides a simple widget that allows users to upload files to cloud storage, supports CDN. Focused on upload without extensive management and media capabilities.
Your choice of solution will probably depend on your needs. UploadCare is easy to use but provides only basic upload functionality. Cloudinary has more features to learn and get used to, but helps you manage the entire lifecycle of an uploaded file, including management of file versions, transformations and format conversions.
I hope this will be useful as you build scalable file upload into your PHP applications.
No additional information provided by user.
Popular posts
-
5 Best Examples of Effective Mobile Website Design
- 4
- 0
-
5 Ways to Improve SEO on Your DIVI Website
- 1
- 0
-
Commonly Used Packages and Plugins in Flutter
- 3
- 0
-
7 Best Features of Big Sur to Try Now
- 4
- 0
-
7 tips for cryptocurrency trading
- 0
- 0
-
13 Expert Ways to Know if Your Business Needs a Mobile App
- 2
- 0