With PHP, it is possible to upload files to the server.
Create an Upload-File Form
To allow users to upload files from a form can be very useful.
Look at the following HTML form for uploading files:
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> |
Notice the following about the HTML form above:
- The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
- The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field
Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.
Create The Upload Script
The "upload_file.php" file contains the code for uploading a file:
<?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } ?> |
By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:
- $_FILES["file"]["name"] - the name of the uploaded file
- $_FILES["file"]["type"] - the type of the uploaded file
- $_FILES["file"]["size"] - the size in bytes of the uploaded file
- $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
- $_FILES["file"]["error"] - the error code resulting from the file upload
This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.
Restrictions on Upload
In this script we add some restrictions to the file upload. The user may only upload .gif or .jpeg files and the file size must be under 20 kb:
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?> |
Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg.
Saving the Uploaded File
The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.
The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> |
The script above checks if the file already exists, if it does not, it copies the file to the specified folder.
Note: This example saves the file to a new folder called "upload"
Its the Excellent informative information..This post found the lots of information about PHP..Am learned about PHP..Thank you for sharing the helpful information..Website Designing Services in Bangalore | Website Development Services in Bangalore
ReplyDeleteVery descriptive and useful information, I really appreciate your effort for presenting it clearly.
ReplyDeleteWeb Design Services in Bangalore | Web Development services in Bangalore
This is also a very good post which I really enjoyed reading
ReplyDeleteEcommerce Development Bangalore | Magento Development Bangalore
useful information..its provide the lots of information about PHP...Thank you for sharing the information..
ReplyDeleteWebsite Designing Company in Delhi | Website Designing Company in Rohini | Website Designing Company in Kirti Nagar
thanks for sharing the useful information.....keep updating....
ReplyDeletehttp://www.amorvet.com
animal feed supplements | herbal veterinary products | animal health products manufacturers | Veterinary Product Manufacturer in India,Delhi
This comment has been removed by the author.
ReplyDeletevery useful information for me... thanks for sharing the post....keep updating...
ReplyDeletehttp://www.arosolchemicals.com/
organic feed additives | veterinary feed additives | Pig and swine feed additives | Veterinary Product Manufacturer in India,Delhi | poultry feed supplements
Hi,
ReplyDeleteThanks for sharing great article about PHP CodeIgnitor. This is very useful information for online blog review readers. Keep it up such a nice posting like this.
From,
Maestro Infotech,
Web Design Company Bangalore
i got excellent information from you.thanks for sharing.
ReplyDelete: Web Design Company Bangalore
Its the Excellent informative information.web design company in chennai
ReplyDeleteThis is really very nice thank you for sharing valuable information
ReplyDeleteweb development company in chennai
web design company in chennai
mobile app development company in chennai
your all articles is very useful for me...
ReplyDeleteanimal feed supplements | poultry feed supplements | cattle feed supplements | vitamins and minerals
Thank you sharing this informative information.It is very helpful for people who are looking for PHP CodeIgniter development services.
ReplyDeleteGet the best and high-quality digital marketing services by hiring the best SEO Company in Noida. We can really help you in reaching your business goal.
ReplyDeleteExotica Housing is one of the best infrastructure company in Delhi. You can connect with us if you want to get your dream house at the best location. Connect with us for more information. top real estate builders in noida extension
ReplyDeleteIf you are looking for the best provider of Residency Visa in Poland then you must go with Steadfast Services.
ReplyDeleteHere is a great deal for you If you want to make any Hoarding for your business advertisement so connect with hoarding IndiaFor more information please visit our website
ReplyDelete
ReplyDeleteArtattackk is a well-knowned Website Designing Company In Delhi having 11+years of experience.
Worship has different importance in our country. If you are living in Bengaluru and looking for North Indian Pandit ji in Banglore to perform puja, you can get solutions to all your problems through AstroPanditam, and book pandit ji for puja at home.
ReplyDeleteIf you need help with your "Saudi Arabia Visa apply online," I highly recommend visiting the website of visits visa. We offer a variety of services, including visa assistance, visa processing, and visa tracking. Our team of experts is knowledgeable and experienced, and they can help you to make sure that your visa application is complete and accurate.
ReplyDeleteRecoolhair Provide 100% Curly Lace Front Wigs, Highlighted Wigs, Frontal Lace Wigs, Glueless Wigs, Short Bob Wigs, Colored Wigs, Pre Bleached Knots Wig, Air Cap Wig, Loose Deep Wave Wigs, Glueless Lace Wig, Hair Bundles with Closure. Free Shipping.
ReplyDeleteIf you're looking for a 5-year e-tourist visa then no look further than Indian Visa For 5 Year Tourist Visa. For applying for a visa you should create an account on India Visa Service and provide your accurate personal information in the visa application form.
ReplyDeleteDiscover the eligibility criteria for an India employment visa with a Visits Visa. To qualify, applicants must have a job offer from an Indian employer, have specialized skills, and earn a minimum annual salary. With a Visits Visa, your journey to professional success begins seamlessly. Apply online for your India Visa today.
ReplyDelete