Zymic

Webmaster resources

Follow us on Twitter!

Hosting status updates - Click here

PHP & MySQL

Free Tutorials » PHP & MySQL » Creating a file upload form with PHP

Learn the basics of coding an upload file form in PHP.

Step 1

Firstly we need a form that can upload an image to save with PHP. This could be something like this:

<form action="./upload.php" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
      <button>Upload File</button>
   <p>
</form>

On line one we tell the form to send the information to our PHP file (which we will create later) named upload.php, we then tell the form to send the information as "multipart/form-data", this is required to send files through the form. Then we create an input with type="file", this makes an input with the required buttons to select a file. Finally we add a button to submit the form.

Step 2

Now we can create our upload.php file. To start we'll check that the file upload is safe by setting a list of allowed filetypes and disallowing all other file uploads. This will prevent people from uploading malicious files. Then we will check the filesize to prevent large files from being uploaded.

<?php
   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
 
   // We'll start handling the upload in the next step
 
?>

It's worth noting, that by default PHP will not handle file uploads larger than 2MB, if you require PHP to handle larger files then you must first set upload_max_filesize and post_max_size in your php.ini file to be larger than 2MB.

Step 3

Now that we know we have a suitably small file of a safe filetype we can upload it to where we want it to go. Using the same file:

<?php
   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
 
   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
 
?>

There you have it! A safe and suitable upload script.

Step 4

For more information look through the Handling File Uploads section at php.net

Tutorial comments

31.08.2016 -

atlanticvn says …

http://chungcuhn24h.net/chung-cu-hd-mon-city/
http://chungcuhn24h.net/biet-thu-nghi-duong-bien-oceanami/
http://chungcuhn24h.net/chung-cu-gemek-tower-hh2-geleximco/
http://chungcuhn24h.net/
http://chungcuhn24h.net/chung-cu/
http://chungcuhn24h.net/chung-cu/quan-bac-tu-liem/
http://chungcuhn24h.net/chung-cu/quan-cau-giay/
http://chungcuhn24h.net/chung-cu/quan-hai-ba-trung/
http://chungcuhn24h.net/chung-cu/quan-hoang-mai/
http://chungcuhn24h.net/chung-cu/quan-nam-tu-liem/
http://chungcuhn24h.net/chung-cu/quan-thanh-xuan/
http://chungcuhn24h.net/chung-cu/quan-dong-da/
http://chungcuhn24h.net/lien-ke-biet-thu/
http://chungcuhn24h.net/chung-cu-c37-bo-cong-an/
http://chungcuhn24h.net/chung-cu/quan-nam-tu-liem/du-an-vinata-towers/
http://chungcuhacc1complexbuilding.blogspot.com/
http://chungcuhn24h.net/chung-cu-ruby-tower-dinh-cong/
http://chungcuhn24h.net/du-an-chung-cu-nui-truc-plaza/
http://chungcuhn24h.net/chung-cu-green-stars-234-pham-van-dong/
http://chungcusunsquare21leductho.blogspot.com/
http://chungcuhn24h.net/chung-cu-the-garden-hill-99-tran-binh/
http://chungcuhn24h.net/chung-cu-hacc1-complex-building-le-van-luong/
http://chungcuhn24h.net/chung-cu-legend-tower-109-nguyen-tuan/
http://chungcuhn24h.net/chung-cu-hacinco-complex-le-van-luong/
http://chungcuhn24h.net/chung-cu-tincom-city-360-giai-phong/
http://chungcuhn24h.net/chung-cu-pvv-tower-60b-nguyen-huy-tuong/
http://chungcuhn24h.net/chung-cu-317-truong-chinh-bid-tower-mo-ban/
http://chungcuhn24h.net/chung-cu-60b-nguyen-huy-tuong-gia-goc-cdt/
http://chungcuhn24h.net/chung-cu-pvv-vinapharm-tower/
http://chungcuhn24h.net/chung-cu-central-point-219-trung-kinh/
http://chungcuhn24h.net/chung-cu-sun-square-21-le-duc-tho/
http://chungcuhn24h.net/chung-cu-e4-vu-pham-ham/
http://chungcue4yenhoa-chungcue4vuphamham.blogspot.com/
http://chungcuhn24h.net/chung-cu-times-tower-hacc1-le-van-luong/
http://chungcuorientalplaza16langha.blogspot.com/
http://chungcu16langha.blogspot.com/
http://chungcuhorizontowerngoaigiaodoann03.blogspot.com/
http://chungcugiatot24h.blogspot.com/
http://chungcuhacincocomplex.blogspot.com/
http://chungcugiatot24h.blogspot.com/2015/09/chung-cu-tincom-city-360-giai-phong.html
http://chungcucentralpoint219trungkinh.blogspot.com/
http://greenstars-geleximco.blogspot.com/
http://geleximco-greenstars.blogspot.com/
http://vinaenco87-linhnam.blogspot.com/
http://chungcuhn24h.net/chung-cu-oriental-plaza/
http://chungcuhn24h.net/chung-cu-16-lang-ha/
http://chungcuhn24h.net/chung-cu-handi-resco-le-van-luong/
http://chungcuhn24h.net/chung-cu-park-view-tower-cdt-dong-phat/
http://chungcuhn24h.net/chung-cu-horizon-tower-ngoai-giao-doan-n03/
http://chungcuhn24h.net/chung-cu-green-stars-234-pham-van-dong/
http://chungcuhn24h.net/du-an-chung-cu-nui-truc-plaza/
http://chungcuhn24h.net/chung-cu-41-le-van-luong/
http://chungcuhn24h.net/chung-cu-mbland-central-point-my-dinh-home-3/
http://chungcuhn24h.net/chung-cu-me-tri-plaza-emico-vov-complex/
http://chungcuhn24h.net/chung-cu-ecolife-capitol-60-le-van-luong/
http://chungcuhn24h.net/cho-thue-chung-cu-green-stars-234-pham-van-dong/
http://chungcuhn24h.net/chung-cu-e4-yen-hoa/
http://chungcuhn24h.net/chung-cu-21-le-van-luong-thanh-an-tower/
https://www.facebook.com/chothuechungcugreenstars234phamvandong
http://chungcuhn24h.net/du-an-new-hoi-an-city/
http://chungcuhn24h.net/du-an-tri-viet-hoi-an-resort/
http://chungcuhn24h.net/vincom-shophouse-bac-ninh/
http://chungcuhn24h.net/vincom-shophouse-thai-binh/
http://chungcuhn24h.net/vinhomes-green-city-cau-dien-khu-do-thi-thanh-pho-xanh/
http://chungcuhn24h.net/chung-cu-imperial-plaza-360-giai-phong/
http://chungcuhn24h.net/chung-cu-udic-riverside-122-vinh-tuy/
http://chungcuhn24h.net/chung-cu-vinhomes-gardenia-my-dinh/
http://chungcuhn24h.net/du-an-sonasea-villas-resorts-phu-quoc/
http://chungcuhn24h.net/chung-cu-hongkong-tower-243a-de-la-thanh/
http://chungcuhn24h.net/chung-cu-69-vu-trong-phung/
http://chungcuhn24h.net/chung-cu-vinhomes-paradise-me-tri/
http://chungcuhn24h.net/chung-cu-phu-my-complex-n01-t4-ngoai-giao-doan/
http://chungcuhn24h.net/chung-cu-282-nguyen-huy-tuong-aqua-spring/
http://chungcugiatot24h.blogspot.com/
http://batdongsan.com.vn/ban-can-ho-chung-cu-duong-le-van-luong-phuong-nhan-chinh-prj-hacinco-3-7-hoang-dao-thuy/chu-dau-tu-thuc-mo-compx-luong-pr7833137
http://www.chungcu122vinhtuy.org/
http://batdongsan.com.vn/ban-can-ho-chung-cu-duong-giai-phong-phuong-phuong-liet-prj-tincom-city-point/bang-gia-imperial-plaza-360-phong-pr7833397
http://chungcuhn24h.net/nang-luc-tai-chinh-cua-chu-dau-tu-chung-cu-e4-yen-hoa/
http://chungcuhn24h.net/li-do-ban-nen-mua-chung-cu-e4-yen-hoa-vu-pham-ham/
http://chungcuhn24h.net/chung-cu-ha-noi-center-point-le-van-luong/
http://chungcuhn24h.net/chung-cu-vinata-tower-289-khuat-duy-tien/
http://chungcuhn24h.net/chung-cu-69b-thuy-khue-sun-legacy/
http://chungcuhn24h.net/chung-cu-31-lang-ha-sun-group/
http://chungcuhn24h.net/chung-cu-97-99-lang-ha/
http://www.chungcue4yenhoa.org/
http://batdongsan.com.vn/ban-can-ho-chung-cu-duong-nguyen-tuan-prj-legend-tower-109-nguyen-tuan/mo-30-suat-ngoai-giao-the-gia-c-ki-hap-dan-pr7833490
http://chungcuhn24h.net/chung-cu-29-lang-ha-hai-dang-tower/
http://chungcuhn24h.net/chung-cu-vinhomes-bac-ninh/
http://chungcuhn24h.net/chung-cu-imperia-sky-garden-423-minh-khai/
http://chungcuhn24h.net/biet-thu-lien-ke-108-nguyen-trai/
http://www.chungcuparkviewcity.com/
http://www.chungcuparkviewcity.com/2016/04/park-view-city-yen-hoa.html
http://www.chungcuparkviewcity.com/2016/04/nhung-ly-do-nen-mua-chung-cu-park-view.html
http://chungcuhn24h.net/chung-cu-kim-van-kim-lu-toa-b-vinaconex-2/
http://chungcuhn24h.net/chung-cu-park-view-city-yen-hoa-vu-pham-ham/
http://chungcuhn24h.net/chung-cu-yen-hoa-sunshine-vu-pham-ham/
http://chungcuhn24h.net/chung-cu-440-vinh-hung-tt-riverside/
http://chungcuhn24h.net/chung-cu-rivera-park-ha-noi-69-vu-trong-phung/
http://www.chungcuanbinhcity.xyz/
http://www.chungcuanbinhcity.xyz/2016/05/tong-quan-chung-cu-an-binh-city.html
http://www.chungcuanbinhcity.xyz/2016/05/an-binh-city-thanh-pho-giao-luu.html
http://chungcuhn24h.net/chung-cu-phu-my-complex-n01-t4-ngoai-giao-doan/
http://batdongsan.com.vn/ban-can-ho-chung-cu-duong-vu-trong-phung-phuong-thanh-xuan-trung-prj-chung-cu-69-vu-trong-phung/rivera-park-ha-noi-0936201858-pr9277797
http://chungcuhn24h.net/chung-cu-e2-yen-hoa-tran-kim-xuyen/
http://chungcuhn24h.net/chung-cu-park-residence-le-van-luong/
http://chungcuhn24h.net/chung-cu-the-golden-palm-le-van-luong/
http://chungcuhn24h.net/du-an-biet-thu-lien-ke-xuan-phuong-tasco/
http://chungcuhn24h.net/mat-bang-chung-cu-e4-yen-hoa-park-view-city/
http://chungcuhn24h.net/tien-do-du-an-chung-cu-park-view-city/
http://chungcuhn24h.net/chung-cu-central-field-219-trung-kinh/
http://chungcuhn24h.net/chung-cu-golden-field-my-dinh/
http://chungcuhn24h.net/chung-cu-chelsea-residences-e2-yen-hoa/
http://chungcuhn24h.net/chung-cu-mandarin-garden-2-tan-mai-hoa-phat/
http://chungcuhn24h.net/thiet-ke-can-ho-mandarin-garden-2-toa-b-va-c/
http://chungcuhn24h.net/thiet-ke-can-ho-mandarin-garden-2-toa-a-va-d/
http://chungcuhn24h.net/chung-cu-my-dinh-pearl-residences/
http://chungcuhn24h.net/chung-cu-48-tran-duy-hung/
http://chungcuhn24h.net/lien-ke-kim-van-kim-lu-golden-silk/
http://batdongsan.com.vn/ban-can-ho-chung-cu-duong-nghiem-xuan-yem-phuong-dai-kim-prj-khu-do-thi-kim-van-kim-lu-golden-silk/mo-toa-b-vinaconex-2-gia-re-097-599-3392-pr9704799
http://www.chungcukimvankimlu-toab.com/
http://www.chungcukimvankimlu-toab.com/2016/07/chung-cu-kim-van-kim-lu-toa-b.html
http://chungcuhn24h.net/chung-cu-thanh-xuan-complex-hapulico-24t3/
http://www.chungcukimvankimlu-toab.com/2016/07/tong-quan-toa-b-chung-cu-kim-van-kim-lu.html
http://www.chungcukimvankimlu-toab.com/2016/07/vi-tri-khu-do-thi-kim-van-kim-lu-golden-silk.html
http://www.chungcukimvankimlu-toab.com/2016/07/chu-dau-tu-vinaconex-2.html
http://chungcuhn24h.net/chung-cu-c1-c2-xuan-dinh/
http://www.hanhud.info/
http://www.hanhud.info/2016/07/chung-cu-hanhud-234-hoang-quoc-viet.html
http://chungcuhn24h.net/chung-cu-hanhud-234-hoang-quoc-viet/
http://chungcuhn24h.net/chung-cu-flc-green-home-18-pham-hung/
http://chungcuhn24h.net/chung-cu-brg-park-residence-le-van-luong/
http://chungcuhn24h.net/vinhomes-shophouse-my-gia-ha-long/
http://chungcuhn24h.net/chung-cu-viet-duc-complex-164-khuat-duy-tien/
http://www.thanhxuan-complex.info/
http://www.bietthuoceanamilonghai.com/
http://chungcuhn24h.net/chung-cu-ha-noi-aqua-central-44-yen-phu/
http://www.chungcuanbinhcity.info/
http://chungcuhn24h.net/chung-cu-an-binh-city-gia-chu-dau-tu/
http://www.chungcuanbinhcity.info/2016/06/bang-gia-chung-cu-an-binh-city.html
http://www.chungcuanbinhcity.info/2016/05/nhung-ly-do-nen-mua-chung-cu-an-binh-city.html
http://www.chungcuanbinhcity.info/2016/08/chung-cu-an-binh-city-toa-a7.html
http://www.chungcuanbinhcity.info/2016/08/chung-cu-an-binh-city-toa-a8.html
http://www.chungcuanbinhcity.info/2016/07/chung-cu-an-binh-city-toa-a2.html
http://www.chungcuanbinhcity.info/2016/07/chung-cu-an-binh-city-toa-a1.html
http://chungcuhn24h.net/chung-cu-nt-complex-82-nguyen-tuan/
http://chungcuhn24h.net/chung-cu-quang-minh-tower-35-le-van-thiem/
http://chungcuhn24h.net/chung-cu-royal-park-le-van-thiem/
http://chungcuhn24h.net/chung-cu-gelexia-riverside-885-tam-trinh/
http://chungcuhn24h.net/chung-cu-my-dinh-plaza-2/
http://chungcuhn24h.net/chung-cu-789-xuan-dinh-bo-quoc-phong/
http://www.vietduc-complex.info/
http://www.thanhxuan-complex.info/
http://www.mydinh-pearl.info/
http://www.chungcu93langha.com/
http://www.chungcu29langha.net/
http://www.vincomshophousenamdinh.com/
http://chungcuhn24h.net/du-an-vincom-shophouse-nam-dinh/
http://chungcuhn24h.net/chung-cu-my-dinh-pearl/
http://chungcuhn24h.net/chung-cu-anland-hh01-complex-building/
http://chungcuhn24h.net/chung-cu-sunshine-riverside/
http://chungcuhn24h.net/chung-cu-ha-do-park-side-cc1-dich-vong/
http://chungcuhn24h.net/chung-cu-king-palace-108-nguyen-trai/
http://chungcuhn24h.net/chung-cu-sunshine-center-16-pham-hung/
http://chungcuhn24h.net/lien-ke-khu-thuong-mai-dich-vu-nha-o-1-5-dong-anh/
http://www.chungcusunshine-center.net/
http://www.chungcusunshine-center.net/2016/08/tong-quan-chung-cu-sunshine-center.html
http://www.chungcusunshine-center.net/2016/08/vi-tri-chung-cu-sunshine-center.html
http://www.chungcusunshine-center.net/2016/08/tien-ich-chung-cu-sunshine-center.html
http://www.chungcusunshine-center.net/2016/08/mat-bang-can-ho-sunshine-center.html
http://www.chungcusunshine-center.net/2016/08/gia-ban-va-tien-do-thanh-toan-sunshine-center.html
http://www.chungcuanbinhcity.info/2016/08/chung-cu-sunshine-center-16-pham-hung.html
http://chungcuhn24h.net/chung-cu-mon-elite-29-lang-ha/
http://chungcuhn24h.com/
http://www.chungcuanbinhcity.info/2016/08/chung-cu-sunshine-center-16-pham-hung.html

17.08.2016 -

DikshaMA says …

I appreciate this post and its seems looking so informative Thanks for sharing with us..
http://packersmoversbangalore.in/
http://blog.packersmoversbangalore.in/
We Provide Best Packers And Movers Bangalore List for Get Free Best Quotes, Compare Charges, Save Money And Time, Household Shifting Services.

21.06.2016 -

Moversfifth says …


http://www.movers5th.in/packers-and-movers-gurgaon/
http://www.movers5th.in/packers-and-movers-delhi/
http://www.movers5th.in/packers-and-movers-panchkula/
http://www.movers5th.in/packers-and-movers-panipat/
http://www.movers5th.in/packers-and-movers-pathankot/
http://www.movers5th.in/packers-and-movers-noida/

21.06.2016 -

Moversfifth says …



http://www.movers5th.in/packers-and-movers-bangalore/
http://www.movers5th.in/packers-and-movers-hyderabad/
http://www.movers5th.in/packers-and-movers-kolkata/
http://www.movers5th.in/packers-and-movers-ludhiana/
http://www.movers5th.in/packers-and-movers-mohali/
http://www.movers5th.in/packers-and-movers-ghaziabad/

21.06.2016 -

Moversfifth says …



http://www.movers5th.in/packers-and-movers-pune/
http://www.movers5th.in/packers-and-movers-mumbai/
http://www.movers5th.in/packers-and-movers-rajkot/
http://www.movers5th.in/packers-and-movers-vadodara/
http://www.movers5th.in/packers-and-movers-chennai/
http://www.movers5th.in/

View all user comments for this tutorial.

Tutorial statistics

Date added:
21.08.2007
Author:
Alex Elliott
User rating:
4/5
Rate tutorial:
Total views:
71511
Total comments:
502

Advertisements