header
 
     
 
pixel
pixel pixel

Upload files using a HTML form

This tutorial explains the basics of uploading images or files to your server using a HTML form and the PHP function copy. I haven't explained form validation or file checking - I'll go into further detail on that in future tutorials. To kick things off, you'll need a HTML form:

uploadform.htm
<html>
  <head>
    <title>HTML Form for uploading image to server</title>
  </head>
  <body>
    <form name="upload" method="post" action="uploadresults.php" enctype="multipart/form-data">
      <input type="file" name="Image"><br> 
      <input type="submit" value="Upload Image">
    </form>
  </body>
</html>

There are two things to note about the HTML code. Firstly, I've defined the attribute enctype="multipart/form-data" in the form tag. This encodes your form data differently to normal, allowing you to submit files. Secondly, I've used the input element with type = "file". The browser renders this as follows:

Note the 'Browse...' button. Clicking this opens a window allowing the user to browse their local computer and select a file (go on, try it). Once they've selected a file and clicked 'Open', the window closes and the file's local path appears in the form (eg - "C:\My Documents\button.gif"). Clicking 'Upload Image' (eg - the submit button) sends the form results, including the local file, to uploadresults.php for processing:

uploadresults.php
<?php
copy 
($_FILES['Image']['tmp_name'], $_FILES['Image']['name']) or die ('Could not upload');
?>

The uploaded image is stored on the server in a temporary location. You can reference it by using the global array $_FILES. There are several variables available:

  • $_FILES['file']['name']: name of the file
  • $_FILES['file']['type']: filetype of the uploaded file
  • $_FILES['file']['size']: size of the uploaded file (in bytes)
  • $_FILES['file']['tmp_name']: name for the temporary file stored on the server
  • $_FILES['file']['error']: an error code resulting from the file transfer
Once uploadresults.php has finished executing, the temporary file is deleted. You use the PHP functions copy or move_uploaded_file to store the file in a permanent location (in fact, our current webhost only allows move_uploaded_file). Copy and move_uploaded_file have two arguments - the first is the temporary location, the second is the final destination where you wish to save the file. In the example above, I saved the file in the same directory as uploadresults.php. If I wanted to save it in a subdirectory, I'd have used something like this:
<?php
copy 
($_FILES['Image']['tmp_name'], "images/".$_FILES['Image']['name']) or die ('Could not upload');
?>

IMPORTANT: be sure to turn on the write permissions of the directory where you're copying the files to or you may get a 'permission denied' or some similar error. Most FTP programs let you set the CHMOD for a directory - set it to 777 to give it write permissions.

So there you have it. There are checks you can perform on uploaded files to ensure they're the right size, filetype, etc. In fact, I'd strongly recommend you have some checks in place, particularly if your upload form is open to the public. For example, you can specify a maximum size of the uploaded file (which importantly kicks in before the file is uploaded). You can check the filetype to make sure it's a certain type. You can even resize images to create thumbnails on the fly. I'll cover all of these in future tutorials so stay tuned. :-)

Unhelpful Helpful Rating 4.0 (score out of 5, no. of ratings: 197)
Comments
Comment by Jyaif on 2005-11-01
this code is usefull

Comment by Allan on 2005-02-07
Nice Tutorial Thx,..... :)

Comment by Andre on 2005-02-10
Thank you very much this helped me a whole bunch. Can't believe I didn't figure this out on my own.

Comment by Cynthia on 2005-02-15
Finally someone that kept it simple! Other tutorials didn't work.

Comment by Dark Angel on 2005-02-21
.__. This didn't work because I always get errors.

Comment by Aircan on 2005-02-21
I'm getting this error: Warning: copy(uploads/bently_1.jpg): failed to open stream: Permission denied in /home/virtual/site189/fst/var/www/html/html_site/php/uploadresults.php on line 2 Could not upload Why?

Comment by gurk on 2005-02-23
worked fine for me. only forgot te set the atributes of the folders to write. mightbe helpfull to know. thanks

Comment by peter on 2005-02-24
100% perfect

Comment by Ressy on 2005-02-27
Good job on this tut. Does exactly what I wanted.

Comment by santhosh on 2005-02-27
Very useful

Comment by Amitesh on 2005-02-28
I tried this code but it is not working. File is not uploading. "); echo("Error is :: ".$_FILES['upfile']['error']); } @unlink($_FILES['upfile']['tmp_name']); ?> output of this code is :: Upload of Hlpstep3.gif to /home/toy/www/images/ failed!!!! Error is :: 0

Comment by Matt on 2005-03-03
The problem with your uploads is probably that your "root" access on the webserver doesn't have write access to that directory. Check/change your settings using chmod. (I'm going to use ftp once it gets up to the server so I don't have that security risk).

Comment by Rodney on 2005-03-03
This works perfectly! Simple solution!

Comment by Eskil on 2005-03-05
Great and Simple way to do this, thx for the tut :) How can I check that the file uploaded is an image (.gif or .jpg) Is there a simple way ?

Comment by shanmuga on 2005-03-09
nice one. thanks

Comment by MP on 2005-03-10
I get this: Warning: Unable to open '' for reading: No such file or directory in /nfs/cust/8/32/55/555238/web/karikatur/uploadresults.php on line 2 Could not upload

Comment by Ricardo on 2005-03-12
I don't get it... nothing happens...

Comment by Lukus on 2005-03-23
Worked great once I set the CHMOD to 777 for the file and used the right case for the file name.

Comment by Chandra Mohan Nayal on 2005-03-28
It was a good tip ! Thannks

Comment by mohd Javed on 2005-03-29
that code is not sufficient to post or Upload the Image,so please give me detail code in PHP. How to Upload the updated image direct server .

Comment by Richard on 2005-03-30
great turorial, just one question. How do you post the original location where the image came from.

Comment by madeshwaran on 2005-03-30
This was realy helpfull for me. and i got the result

Comment by Kurisu on 2005-03-30
Are there similar scripts that allow only image files like JPG/GIF/BMP/whatever? I think this one allows all sorts o files to be uploaded.

Comment by Hunen Daabool on 2005-04-04
I cannot Copy 'tmp_name' File To any folder under web site after Uplodin fIles Help Me Abbout This Plaese And thanks.

Comment by Clems on 2005-04-05
Thank you to keep it simple!

Comment by haq on 2005-04-05
wheres the demonstration ,if you wwant some good rating plz come with demonstration next time

Comment by Bill the Blagger on 2005-04-09
All credit to you for making a stab at a topic which confuses the hell out of people. You've tackled it in a straightforward and no-nonsense way instead of attempting to provide an ultra complex bells and whistles app which would be doomed to failure. One tip for your readers: If the script is not doing as required, try changing the copy() function for a move_uploaded_file() function.

Comment by John A Cove on 2005-04-14
Excellent, thanks :)

Comment by Gilles on 2005-04-14
Doesn.t  work!!!!

Comment by Jason Trautwein on 2005-04-21
This has been very informative and an excellent start for simple quick information for file uploads.

Comment by Ayman Wahby on 2005-04-26
Thanks somehow, because I used this function "@copy" on my localhost (apache server) and it wored successfully. But when I upload my application on the host server, it didn't work... Can anyone tell me any comment ? (ayman_wahby@yahoo.com). Great thanks for your care.

Comment by joe on 2005-04-28
Remember that you need to set permissions on the folder you are copying to. Look into CHMOD. Sometimes your FTP program will allow you to adjust permissions. On some ISP you have to do this with shell access. This would require root access to your server. Also check into your user account control panel and snoop around and look for folder permissions. Set permissions on your folder to be writable. I user CHMOD 777

Comment by Drax on 2005-05-05
Very heplful. . . Thanks a lot

Comment by pavan on 2005-05-13
how to upload swf image for jpeg we have image/pjpeg & bmp we have image/bmp for swf we have image/??? or anything pls send we the answer to my email tvpk272@rediffmail.com

Comment by sweetbee on 2005-05-30
How to upload the file at desired location?

Comment by JD on 2005-06-14
Does the job and I can work the rest out by myself. After reading these messages, I'm amazed how many stupid are out there.

Comment by david on 2005-06-25
Thanks so much, I have tried several other examples and tutorials, this one worked straight away!

Comment by lithuania on 2005-07-06
u need to chmod the folder where the images go to 777(u can do it with any ftp program)then ur images vil go fine ;]

Comment by Carlosgoias on 2005-07-12
EXCELENTE!!!!

Comment by Rick on 2005-07-12
Thanks.. but now I want to email a form uploaded image.. how do I do that?

Comment by Chris Miller on 2005-07-13
What a great example. It worked perfectly the first time. I can take it from here. At last, an example that informs instead of showing off.

Comment by hetiland on 2005-07-15
thanks boys. Very special

Comment by Kelly on 2005-07-23
That's Sooooooo HOT!!

Comment by Ann Stewart on 2005-07-26
Hi, thanks but don't understand. I need to upload my e-book but probably did not input it properly. Would someone please explain this CHMOD and 777 to me? Also, I'm working with a sitebuilder and their server. They're picky.

Comment by RK on 2005-08-16
why all could not upload ??? all just show could not upload

Comment by sendil on 2005-08-19
Need Help ! I try to set Read and Write permission for the folder where the images are uploaded. After setting the permissions ( 777 ). It turns back to 000 again. Can somebody tell me what the problem is.

Comment by Raj on 2005-08-25
It would be highly useful if uploading could be done for say 100 images

Comment by Armorine on 2005-08-26
Infiltrate...do you see anything?

Comment by mitanshu on 2005-08-28
can i save the file in mysql database itself

Comment by Ian Weir on 2005-09-02
At last, something that keeps it simple without making it too complicated. I have tried out other tutorials, and they were all too complicated. Simplicity is beauty. Keep up the good work!

Comment by 4736 on 2005-09-14
could be more detail

Comment by some who codes php for a living on 2005-09-18
useless

Comment by smalltree on 2005-09-27
Whats with all the negative feedback? This tutorial gives the basics and works fine if you've got your permissions set OK. Perfect for impatient people like me. Oh and on the last comment.. if you code PHP for a living why are you reading this? You might be a bit more positve and maybe give another link to help people out or better still write a tutorial that isnt 'useless' as you put it.

Comment by Gourav on 2005-10-06
this code is useless

Comment by lisa on 2005-12-04
brilliant worked first go - thanks for providing me with the essentials only first - makes sense in comparison to other tutorials ive read

Comment by suraj on 2006-02-18
hey the tutorial was great and very simple.... great job

Comment by Kirill on 2006-04-10
Hi! And what became with others?

Comment by Li on 2006-05-05
Hi all! I want to establish the same guest book. Prompt as?

Comment by Jude on 2007-02-01
Very helpful - thanks!

Comment by Sirle on 2007-06-19
a

Comment by taufik on 2008-01-14
it's too simple.. how if i want to create folder on server and i also upload a file on it? can you do that?

Comment by wow powerleveling on 2008-04-08
Buy cheap WOW Power Leveling,sell wow gold.welcome to buy cheap wow gold-We can have WOW PowerLeveling,buy wow gold game, World Of Warcraft Gold,wow Gold, world of warcraft gold deal,Cheap WOW Gold. Welcome here to buy the professional World Of Warcraft Power Leveling service, World Of Warcraft PowerLeveling for Cheap Wow Powerleveling, WoW Power leveling Guide. The best of luckgoogle Best wishesAlcohol Tester purchasing center is a professional enterprise Breathalyser,Breathalyzer specializes in breath Gas Alarm consulting,Gas Detector through internet.Co Detector,Co Alarm always have been designed to alarm.

Comment by 数据恢复 on 2008-04-08
专业从事数据恢复RAID数据恢复服务器数据恢复钢管加密锁数据恢复数据恢复数据恢复数据恢复数据恢复数据恢复数据恢复数据恢复数据恢复升降机幸福娱乐幸福女人娱乐博客相册树化玉 导航google排名google排名google排名台州网站建设优化推广网站建设网站优化网络公司 google左侧排名google左侧排名google排名google排名google排名google优化google排名网站优化搜索引擎优化搜索引擎排名网站优化搜索引擎优化网站推广网站优化百度优化SEO搜索引擎营销数据恢复数据恢复数据恢复SEO同声翻译SEO塑料同声传译设备租赁数据恢复数据恢复数据恢复数据恢复数据恢复台州做网站推广Hi Best wishes。bjseek by数据恢复数据恢复数据恢复 The best of luck Best wishes Best regards .by bjseek

Comment by sdf on 2008-04-25
北京酒店式公寓 北京短期租房 Lab glassware 卫星电视安装 安装卫星电视 北京卫星电视 卫星电视 玻璃仪器 婚庆 婚庆公司婚庆 婚庆公司 压力校验仪 压力变送器 超声波流量计 卫星电视安装 超声波液位计 电磁流量计 涡街流量计 涡轮流量计 手机定位器 手机定位 手机监听器 监听器 同声传译 牛皮癣 蟑螂药 激光治疗近视 荷兰留学 车库门 司法考试 白内障 和田玉 新疆和田玉 中国和田玉 防雷 防雷 视频会议 办公家具 排在第1页的关键词 反渗透设备 商标查询 去离子水 高纯水设备 去离子水设备 软化水 软化水设备 反渗透 北京短租公寓 北京日租公寓 商标局 中译英 北京安装卫星电视 北京卫星天线安装 北京卫星电视安装 水处理设备 超纯水机 光催化反应器 光化学反应器 北京海淀驾校 北京卫星电视安装 玻璃反应器 液位控制器 单法兰液位变送器 投入式液位计 脑囊虫病 治疗脑囊虫 北京安装卫星天线 北京卫星电视安装 北京安装卫星天线 北京安装卫星电视 防静电地板 北京日租房

Comment by sdf on 2008-04-25
北京酒店式公寓 北京短期租房 Lab glassware 卫星电视安装 安装卫星电视 北京卫星电视 卫星电视 玻璃仪器 婚庆 婚庆公司婚庆 婚庆公司 压力校验仪 压力变送器 超声波流量计 卫星电视安装 超声波液位计 电磁流量计 涡街流量计 涡轮流量计 手机定位器 手机定位 手机监听器 监听器 同声传译 牛皮癣 蟑螂药 激光治疗近视 荷兰留学 车库门 司法考试 白内障 和田玉 新疆和田玉 中国和田玉 防雷 防雷 视频会议 办公家具 排在第1页的关键词 反渗透设备 商标查询 去离子水 高纯水设备 去离子水设备 软化水 软化水设备 反渗透 北京短租公寓 北京日租公寓 商标局 中译英 北京安装卫星电视 北京卫星天线安装 北京卫星电视安装 水处理设备 超纯水机 光催化反应器 光化学反应器 北京海淀驾校 北京卫星电视安装 玻璃反应器 液位控制器 单法兰液位变送器 投入式液位计 脑囊虫病 治疗脑囊虫 北京安装卫星天线 北京卫星电视安装 北京安装卫星天线 北京安装卫星电视 防静电地板 北京日租房

Comment by df on 2008-04-28
隆胸 丰胸 假体隆胸 隆胸术 自体脂肪丰胸 改脸型垫下巴 颊脂垫去颊脂垫 注射美容注射除皱 面部整形吸脂 吸脂减肥腹部吸脂 瘦脸瘦脸针 隆鼻隆鼻术 鼻子整形鼻部整形 除皱除皱术 面部年轻眼部整形 双眼皮去眼袋 双眼皮手术整形 整形美容整形专家 整形医院整形美容医院 北京最好的整形医院整形整形美容 整形专家整形医院 整形美容医院北京最好的整形医院 排在第1页的关键词 自体脂肪移植丰胸自体脂肪移植隆胸 自体脂肪移植 自体脂肪填充自体组织充填 下颌角下颌角手术 下颌角肥大去下颌角 下颌角整形术 下颌角整形下颌整形 下颌骨整形隆下颌 隆下巴下颌整形 颧骨整形颧骨手术 颧骨高颧骨宽 颧骨突出透明质酸 波尿酸 BOTOX注射除皱

Comment by df on 2008-04-28
门窗塑钢门窗塑钢阳光房断桥铝门窗 塑钢门窗海螺塑钢门窗新世界塑钢门窗 维卡塑钢门窗实德塑钢门窗LG好佳喜塑钢门窗柯梅令塑钢门窗阳光房封阳台隔温彩钢板阳光房阳光板盖顶阳光房 钢化玻璃顶阳光房 中空钢化顶阳光房 翡翠楼阳光房水晶阁阳光房宝石屋阳光房 钻石楼阳光房 断桥铝门窗凤铝断桥铝门窗 澳联断桥铝门窗 北新断桥铝门窗华建断桥铝门窗 中旺断桥铝门窗和平断桥铝门窗 铝合金门窗tblog tblogtblog tblogtblogtblogtblogtblog tblogtblog tblogtblog tblog

Comment by 00DDD on 2008-04-28
南昌不锈钢手推车 吉安不锈钢手推车 不锈钢货架 泰州不锈钢货架 杭州不锈钢货架 绍兴不锈钢货架/a> 台州不锈钢手推车 衢州不锈钢货架 阜阳不锈钢货架 南昌不锈钢货架/a> 吉安不锈钢货架

Comment by quick on 2008-05-06
ffxi gil wow gold maple story mesos maplestory mesos maple story meso wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold

Comment by quick on 2008-05-06
ffxi gil wow gold maple story mesos maplestory mesos maple story meso wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold

Comment by KUI on 2008-05-12
wow leveling wow gold world of warcraft gold buy wow gold cheap wow gold wow power leveling wow powerleveling wow gold world of warcraft gold buy wow gold cheap wow gold wow power leveling wow powerleveling wow gold world of warcraft gold buy wow gold cheap wow gold wow power leveling wow powerleveling Maple Story Mesos maplestory Mesos maplestory meso Granado Espada Vis Granado Espada gold GE money dofus kamas kamas dofus runescape gold runescape money flyff penya buy flyff gold flyff money age of conan gold age of conan gold age of conan gold age of conan gold age of conan gold age of conan gold age of conan power leveling age of conan power leveling age of conan power leveling age of conan power leveling age of conan power leveling age of conan power leveling Age of Conan gold age of conan power leveling EverQuest plat buy eq gold EverQuest gold eq plat eq gold

Comment by wow power leveling on 2008-05-17
The fourth wow power leveling latest game in wow power leveling Warcraft series is ‘wow power leveling’. Also known as wow power leveling, it represents a wow power leveling multiplayer online wow power leveling game, the best of wow power leveling kind. Initially, it was wow gold it be released in 2001, but wow powerleveling was delayed wow powerleveling 2004, thus wow powerleveling the 10 years ofwow powerleveling franchise of thiswow gold series. The world of warcraft power leveling was not world of warcraft power levelingfulfilling, because wow power levelproblems with wow power level server’s stability power leveling wow performance occurred, but power leveling wow game still power leveling wow a financial success powerleveling wow the most powerleveling wow game of its kind. The number cheap wow power leveling users that play Maple Story mesos, exceeds 8.5 MapleStory mesos, worldwide.As a form ms mesos,recognition for mesos,outstanding popularity, the game SilkRoad Gold, received aSRO Gold, of awards. Now the question eq2 plat, why is eq2 gold, game eq2 Platinum, popular? For anyoneEverQuest 2 Platinum, played the previous EverQuest 2 gold, and EverQuest 2 plat, already initiated lotro gold, the mysterious world lotr gold, the breathtaking Lord of the Rings online Gold, this Rolex Replica nothing but an Replica Rolex adventure that continues the story of ‘Warcraft III: Frozen Throne’, four years after conclusion, in the world of Azeroth. The game is online role-playing, the previous versions being online and offline strategy games. The major thrills and unique features are present as in every Blizzard game.

Comment by ying3 on 2008-05-17
lotro gold wow gold wow gold wow gold wow gold aoc gold,aoc powerleveling buy age of conan gold,cheap age of conan gold age of conan accounts,age of conan guide age of conan gold,age of conan money age of conan power leveling,age of conan powerleveling Guild Wars Gold second life Eve Online gold SilkRoad EverQuest 2 wow cdkey wow The Burning Crusade CD Key wow TBC WOW 60 Tage Game Time Card Lord of the Ring gold Foreign Trade shoes luggage pvc printnig machine 标牌,工号牌 马口铁徽章、奖牌 钥匙扣,开瓶器 校徽,校牌 切卷机 图腾机柜 蜡像 玻璃瓶 压片机 压片机 制丸机 灌装机,熔封机,轧盖机 粉碎机 混合机 颗粒机 灌装机、轧盖机、熔封机 灌装机、轧盖机、熔封机 混合机 粉碎机 压片机 颗粒机 切片机,洗瓶机,数片机,填充机,糖衣机,切片机 流水线 生产线、生产线制造 颗粒包装机,气动式颗粒包装机,机械式颗粒包装机 冲瓶机 充填机 打码机 粉剂包装机 封口机 过滤器 灌装生产线 汽水混合机 水处理设备 三角包装机 贴标机 洗瓶机 液体包装机 真空包装机 温州网站建设、温州网页设计、温州网页制作 link link link link link link link link link link link 瑞安网站建设,瑞安网站网页设计、瑞安网页制作,瑞安网络公司 瑞安人才 瑞安人才网 模切机 温州家具 浸塑加工 烤炉、饺子机 汤圆机,洗碗机

Comment by ms on 2008-05-17
maple story mesos maple story mesos maple story mesos maple story mesos maple story mesos maple story mesos maplestory mesos maplestory mesos maplestory mesos maplestory mesos maplestory mesos maplestory mesos maple story gold maple story gold maple story gold maple story gold maple story gold maple story gold maplestory gold maplestory gold maplestory gold maplestory gold maplestory gold maplestory gold maple story money maple story money maple story money maple story money maple story money maple story money maplestory money maplestory money maplestory money maplestory money maplestory money maplestory money buy maple story mesos buy maple story mesos buy maple story mesos buy maple story mesos buy maple story mesos buy maple story mesos buy maplestory mesos buy maplestory mesos buy maplestory mesos buy maplestory mesos buy maplestory mesos buy maplestory mesos maple story maple story maple story maple story maple story maple story maplestory maplestory maplestory maplestory maplestory maplestory buy maple story mesos buy maple story mesos buy maple story mesos buy maple story mesos buy maple story mesos buy maple story mesos cheap mesos cheap mesos cheap mesos cheap mesos cheap mesos cheap mesos maple story shop maple story shop maple story shop maple story shop maple story shop maple story shop cheap msmesos cheap msmesos cheap msmesos cheap msmesos cheap msmesos cheap msmesos sell wow gold sell wow gold sell wow gold sell wow gold sell wow gold sell wow gold sell wow gold sell wow gold sell wow gold sell wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold sell wow cd key sell wow cd key sell wow cd key sell wow cd key sell wow cd key sell wow cd key sell wow cd key sell wow cd key sell wow cd key sell wow cd key trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow gold trade wow trade wow trade wow trade wow trade wow trade wow trade wow trade wow trade wow trade wow trade maple story mesos maple story mesos maple story mesos maple story mesos maple story mesos maple story mesos maple story mesos sell maple story mesos sell maple story mesos sell maple story mesos sell maple story mesos sell maple story mesos sell maple story mesos sell maple story mesos sell maplestory mesos sell maplestory mesos sell maplestory mesos sell maplestory mesos sell maplestory mesos sell maplestory mesos sell maplestory mesos sell maple story gold sell maple story gold sell maple story gold sell maple story gold sell maple story gold sell maple story gold sell maple story gold sell maple story money sell maple story money sell maple story money sell maple story money sell maple story money sell maple story money sell maple story money sell maplestory gold sell maplestory gold sell maplestory gold sell maplestory gold sell maplestory gold sell maplestory gold sell maplestory gold sell maplestory money sell maplestory money sell maplestory money sell maplestory money sell maplestory money sell maplestory money sell maplestory money maplestory trade maplestory trade maplestory trade maplestory trade maplestory trade maplestory trade maplestory trade maple story trade maple story trade maple story trade maple story trade maple story trade maple story trade maple story trade

Post a Comment
Name
Email
(optional)
Comment
RatingUnhelpful Helpful
Security Image* (this is just to prevent spam submissions)
Security Image