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
Uploading and Downloading files - changing names (Perl and PHP)

When you are uploading a file to a server via http (in Perl or PHP), where is the file saved, and what is it called? And when you download a file, where is it saved on your local disc, and what name is it given? These are not unusual questions, but it can be hard to find the answer, as I was reminded in my in-box overnight:

I am located in the USA and am working through your on line public CGI Perl upload and download scripts (up.pl ; down.cgi ; up.html).

I have everything working except for one problem - I can't figure out how to preserve the original filenames. I can hard code a filename in both modules and it works fine but I would like to preserve the original filenames after they have been uploaded or downloaded. All downloaded files have the name of the download module i.e. "down.cgi" and uploaded files have to have a hard coded name (e.g. $fnsave="LastUploadedFile") in order to to appear on the server directory although the file seems to be uploading to somewhere.

I also can't send the file to anything but the root directory - It just doesn't show up after uploading. It seems like it should not be that difficult but I can't figure it out. Could you assist please?

My answer is worth sharing:

On the download, (http://www.wellho.net/resources/ex.php4?item=p406/down.pl) you need to add a content-disposition header - I've illustrated that (in a PHP example) at:


and you'll find source code examples (all in PHP, I'm afraid) at:

On the upload (http://www.wellho.net/resources/ex.php4?item=p406/up.html and http://www.wellho.net/resources/ex.php4?item=p406/up.cgi), again I have some PHP examples and start at:
which however is NOT so clear as PHP does the decoding work for you ... so I have modified the Perl scripts that you referred to and uploaded them live onto my side - see
http://www.wellho.net/demo/upfile.html (form to run the demo, which runs)
and the source code is at:

On uploading, you'll find that it's common practise for files to be saved initially to a staging directory and then to be moved / copied - it relates to various issues such as the security implications of any old file that a user can upload being places into your directories straight away, with overwrite and overflow risks, and the ability of people to upload code which - saved to a directory with CGI enabled - could be run, and if the file was malicious code that's an injection attack!

Hopefully this is useful to you ... the comments and references could be useful to others too, and you're not the first one to struggle with this nor will you be the last, so I'm adding it to my blog at http://www.wellho.net/horse to help pull the various answers and resources together. Thanks for the inspiration for this morning!
(written 2009-08-04)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
A207 - Web Application Deployment - HTTP
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [2918] Downloading a report from the web for further local analysis - (2010-08-13)
  [2738] What is all this SESSION stuff about? (PHP) - (2010-04-25)
  [2596] Http protocol - what does a web server send - (2010-01-24)
  [1549] http, https and ajp - comparison and choice - (2008-02-22)
  [1503] Web page (http) error status 405 - (2008-01-12)
  [1378] Etag in http headers - what is it? - (2007-10-03)
  [484] Setting the file name for a downloaded document - (2005-11-03)

H307 - PHP - Web2 and caching
  [4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)
  [4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
  [4106] Web server efficiency - saving repetition through caches - (2013-05-30)
  [4075] Further recent PHP examples - (2013-04-28)
  [4055] Using web services to access you data - JSON and RESTful services - (2013-03-29)
  [3999] Handling failures / absences of your backend server nicely - (2013-02-08)
  [3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
  [3458] On this day ... one PHP script with three uses - (2011-09-26)
  [3186] How to add a customised twitter feed to your site - (2011-02-27)
  [3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [2545] Scraping content for your own page via PHP - (2009-12-21)
  [2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
  [1995] Automated server heartbeat and health check - (2009-01-16)
  [1926] Flash (client) to PHP (server) - example - (2008-12-06)
  [1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
  [1813] Ajax - going Asyncronous and what it means - (2008-09-28)
  [1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
  [1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
  [1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)

P406 - Perl - More CGI Programs and Facilities
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
  [641] Simple but rugged form handling demo - (2006-03-10)

Back to
Helping new arrivals find out about source code examples
Previous and next
Horse's mouth home
Forward to
Looking for a practical standards course
Some other Articles
Apache, Tomcat, mod_proxy
What search terms FAIL to bring visitors to our site, when they should?
Java Collection Objects in the java.util package
Looking for a practical standards course
Uploading and Downloading files - changing names (Perl and PHP)
Helping new arrivals find out about source code examples
Graphics in Lua - an example using the gd library
For Lua Programmers AND for Town Planners
How to make a Risotto (PHP build style)
Guadalajara - a special tour of a lovely city
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/2321_Upl ... -PHP-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat