Upload Textfiles

Hi ! How can I make it possible for my users to upload textfiles ?
Please post me a source here. No URL, please !

regards
ItsMe
ItsMeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

maneshrCommented:
here goes..........

==========upload.html=============
<form ENCTYPE="multipart/form-data" action="/cgi-bin/upload.pl" Method=POST>
Attach:<input name="doc" type="file">
<BR>
<P>
<INPUT TYPE=SUBMIT NAME=action VALUE="Upload Only!">
</form>

===========upload.pl========================

#!/usr/local/bin/perl

use CGI;

$query=new CGI;

$DocLocation=$query->param('doc');


$WebFileName = "UpdateGroupFile.htm";
print "Content-type: text/html\n\n";

`rm /tmp/$WebFileName`;
open(MYFILE,"> /tmp/$WebFileName") || die $!;
binmode MYFILE;
while($bytesread=read($DocLocation,$data,1024)){
  $size+=$bytesread;
  print MYFILE $data;
}
close(MYFILE);
close($DocLocation);

`chmod 777 /tmp/$WebFileName`;

print "<B>$DocLocation has been uploaded</B><br>\n";
print "<B>Size = ",$size," byte(s)</B><BR>\n";
0
ItsMeAuthor Commented:
Thanks ! Is it possible to just accept normal plain text files ?
0
maneshrCommented:
sure. what file needs to be uploaded is based on users decision.
so as long as the user knows that the file is textual, thats fine.

Alternately you can put down a policy saying that you will only allow upload of files with the extension of .txt. but then any one can rename a .exe file to a .txt file & circumvent your policy.

Bottomline, you cannot filter files based on their type using HTML.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ItsMeAuthor Commented:
Hmmm, perhaps its possible to set no binmode, so exe's will be destroyed ? How can I prevent the user from uploading files which are bigger than lets say 200 KB ?
I already thought of using textareas to transmit the text. that would be pretty cool, but how can I print out already transmitted textfiles in the textareas ? my problem is that they include html tags which confuse the browser ...

regards
ItsMe
0
ItsMeAuthor Commented:
oh, I want to upload the files from a local system to the server. Is it possible to open a file-open-dialog ?
0
maneshrCommented:
in order to restrict the size of the file, you can modify the code that i have provided. in that code i am counting the size of the file too. ($size+=$bytesread; ) so immediately after this you could say

if ($size > 200){
  close($DocLocation);
  `rm /tmp/$WebFileName`;
  print "The uploaded file exceded the specified limit<br>\n";
  exit;
}

you are right when you say "perhaps its possible to set no binmode, so exe's will be destroyed ?"

But think of this scenario, the file is less than 200K and is an exe file. it would be corrupted but still uploaded. do you want that corrupted file to eat unnecessary disk space??

in order to store the html tags too you can use a small "trick/cheat".
when the file is being uploaded, search for any html tags and just replace < with &lt; and > with &gt;(Eg. <body> would &lt;body&gt;).

Now when you want to display the file it would show it exactly as it should be!!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ItsMeAuthor Commented:
that it ! thanks. At my first try I only replaced the first "<" tag but not ">"...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.