Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
PHP Image upload script

"How do I upload an image to my server?" That's a common question and I have provided many answers before ... but today I wrote a very simple script that shows just the pertinent points of uploading an image.

<?php
if ($_REQUEST[completed] == 1) {
$newname = uniqid("whc").".jpg";
move_uploaded_file($_FILES['mailfile']['tmp_name'],
"../robdemo/$newname");
} ?>
<html>
<head><title>Upload page</title></head>
<body><h1>Image Uploader</h1>
<?php if ($_REQUEST[completed] != 1) { ?>
<b>Please upload an image</b><br>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=1500000>
<input type=hidden name=completed value=1>
Choose file to send: <input type=file name=mailfile> and
<input type=submit></form>
<?php } else { ?>
<b>Yum, Yum. I enjoyed that</b>
<?php } ?>
<hr>
Copyright, etc
</body></html>


What do you need to note?

1. The upload directory needs to exist and have appropriate write permissions
2. The form needs an enctype tag
3. You may need to set up the temporary directory if your server is running Windows

Also note that I've simply given each uploaded file a unique name - I haven't in this example provided any way to actually get at the data that's been placed on the server. And I've doing nothing to stop my user from uploading pictures that are too large or inappropriate for my site either. It's just a simple script to show you the principles.
(written 2007-03-21)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
G912 - Well House Consultants - Photography for the Web
  [3402] That spec is a kingfisher ... - (2011-08-21)
  [3170] How far is something pictured from the camera? - (2011-02-15)
  [3104] Catering in Syracuse, the Saigon Cafe, stolen images and Christmas - (2010-12-25)
  [2884] Hotlinked images onto adult material sites - (2010-07-23)
  [2592] Re-using our pictures - (2010-01-21)
  [2252] Leaping dog, Leaping horse, copyright of old masters - (2009-06-20)
  [2224] Trowbridge - a missed opportunity? Melksham - into the breach? - (2009-06-08)
  [1895] Comparison - with and without flash - (2008-11-20)
  [1732] Old pictures and comparisons - (2008-08-01)
  [1568] What colour is the season? - (2008-03-08)
  [1506] Ongoing Image Copyright Issues, PHP and MySQL solutions - (2008-01-14)
  [1396] Using PHP to upload images / Store on MySQL database - security questions - (2007-10-19)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1188] What shape is your shake? - (2007-05-15)
  [1185] Themes for the web site - (2007-05-13)
  [1087] Telling a story in different ways - (2007-02-20)
  [937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
  [819] My projector has a photo-id - (2006-07-31)
  [665] PHP Image viewing application - (2006-04-01)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [553] Keep that image small - (2006-01-03)
  [546] The relevance of the hairy woodpecker - (2005-12-28)
  [194] Published Photographer - (2005-01-30)
  [97] What makes a professional photographer? - (2004-10-23)

H112 - PHP - Further Web Page and Network Handling
  [4483] Moving from mysql to mysqli - simple worked example - (2015-05-03)
  [4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)
  [3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
  [3568] Telling which ServerAlias your visitor used - useful during merging domains - (2012-01-04)
  [3540] Easy session example in PHP - keeping each customers data apart - (2011-12-06)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
  [2918] Downloading a report from the web for further local analysis - (2010-08-13)
  [2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [2632] Shipping a test harness with your class in PHP - (2010-02-12)
  [1549] http, https and ajp - comparison and choice - (2008-02-22)
  [1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
  [1515] Keeping staff up to date on hotel room status - (2008-01-22)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1496] PHP / Web 2 logging - (2008-01-06)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1379] Simple page password protection - PHP - (2007-10-04)
  [1355] .php or .html extension? Morally Static Pages - (2007-09-17)
  [1210] PHP header() function - uses and new restrictions - (2007-05-30)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1183] Improving searches - from OR to AND? - (2007-05-11)
  [1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [904] Of course I'll tell you by email - (2006-10-25)
  [847] Image maps for navigation - a straightforward example - (2006-08-28)
  [789] Hot answers in PHP - (2006-07-02)
  [767] Finding the language preference of a web site visitor - (2006-06-18)
  [675] Adding PHP tags to an old cgi program - (2006-04-08)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [565] Using PHP to output images, XML, Style sheets, etc - (2006-01-15)
  [542] Morning image, afternoon image - (2005-12-26)
  [537] Daily Image Santafied - (2005-12-22)
  [484] Setting the file name for a downloaded document - (2005-11-03)
  [451] Accessing a page via POST from within a PHP script - (2005-09-26)
  [443] Server side scripting of styles to suit the browser - (2005-09-12)
  [425] Caching an XML feed - (2005-08-26)
  [410] Reading a news or blog feed (RSS) in your PHP page - (2005-08-12)
  [376] What brings people to my web site? - (2005-07-13)
  [372] Time calculation in PHP - (2005-07-08)
  [356] Sudoku helper or sudoku cheat - (2005-06-23)
  [345] Spotting a denial of service attack - (2005-06-12)
  [314] What language is this written in? - (2005-05-17)
  [220] When to use Frames - (2005-02-19)


Back to
File and URL reading in PHP
Previous and next
or
Horse's mouth home
Forward to
Unexpected visitors to our site
Some other Articles
Origin of Jack and Jill and little Jack Horner
Newport Male Voice Choir
PHP adding arrays / summing arrays
Unexpected visitors to our site
PHP Image upload script
File and URL reading in PHP
Bank Holiday country breaks in Melksham, Wiltshire
Training in Lua
Python - two different splits
False imprisonment - a contrast from the news
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1114_PHP ... cript.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat