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:
http://www.wellho.net/mouth/484_Setting-the-file-name-for-a-downloaded-document.html
and you'll find source code examples (all in PHP, I'm afraid) at:
http://www.wellho.net/resources/ex.php4?item=h109/savepeople.php4
http://www.wellho.net/resources/ex.php4?item=h307/sendimage.php
and
http://www.wellho.net/resources/ex.php4?item=s156/mkexcel.php
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:
http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retrieve.html
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)
http://www.wellho.net/cgi-bin/demo/upsend.cgi
and the source code is at:
http://www.wellho.net/resources/ex.php4?item=p406/upfile.html
http://www.wellho.net/resources/ex.php4?item=p406/upsend.cgi
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
P406 - Perl - More CGI Programs and Facilities [641] Simple but rugged form handling demo - (2006-03-10)
[1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
H307 - PHP - Web2 and caching [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)
[1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
[1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
[1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
[1926] Flash (client) to PHP (server) - example - (2008-12-06)
[1995] Automated server heartbeat and health check - (2009-01-16)
[2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
[2545] Scraping content for your own page via PHP - (2009-12-21)
[3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
[3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
[3186] How to add a customised twitter feed to your site - (2011-02-27)
[3458] On this day ... one PHP script with three uses - (2011-09-26)
[3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
[3999] Handling failures / absences of your backend server nicely - (2013-02-08)
[4055] Using web services to access you data - JSON and RESTful services - (2013-03-29)
[4075] Further recent PHP examples - (2013-04-28)
[4106] Web server efficiency - saving repetition through caches - (2013-05-30)
[4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
[4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)
A207 - Web Application Deployment - HTTP [484] Setting the file name for a downloaded document - (2005-11-03)
[1378] Etag in http headers - what is it? - (2007-10-03)
[1503] Web page (http) error status 405 - (2008-01-12)
[1549] http, https and ajp - comparison and choice - (2008-02-22)
[2596] Http protocol - what does a web server send - (2010-01-24)
[2738] What is all this SESSION stuff about? (PHP) - (2010-04-25)
[2918] Downloading a report from the web for further local analysis - (2010-08-13)
[3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
Some other Articles
Apache, Tomcat, mod_proxyWhat search terms FAIL to bring visitors to our site, when they should?Java Collection Objects in the java.util packageLooking for a practical standards courseUploading and Downloading files - changing names (Perl and PHP)Helping new arrivals find out about source code examplesGraphics in Lua - an example using the gd libraryFor Lua Programmers AND for Town PlannersHow to make a Risotto (PHP build style)Guadalajara - a special tour of a lovely city