Page 1 of 1

The File That Shags Me.... a file upload film

Posted: Mon Aug 15, 2005 1:14 am
by harrisonad
Hi, I don't know if there's a thread similar to my problem but to make it fresh i would ask it anyway.

I have been working on a file upload script and the form goes like this

Code: Select all

$str = "
<font class=blue>FILE UPLOAD</font><p>
".$this->message."
<form name=uploadform method=post action='' enctype='multipart/form-data'>
".Hidden('MAX_FILE_SIZE','200000')."
Enter the Path to ".$this->documents[$this->cur_doc]->getTitle()." file<p>
<input type=file name=file size=50><p>
".Button('upload','',"if(file.value) submit()")."					
</form>";

echo $str;
*note: some functions are user-defined

The form will let users to select any file then a script will filter and process it according to what is passed to $_FILES['file'].

Now what I want to happen is to restrict the selection of file to only one extension before submission, for example, only MS Excel files. Is this possible? Do I have to put more parameters to my form to do this? or another hidden field, just like MAX_FILE_SIZE?

Thanks in advance.

Posted: Mon Aug 15, 2005 1:31 am
by s.dot
an easy way of doing it would be just to read the info after the last .

Code: Select all

$file = $_FILES['file']['tmp_name'];
$type = strstr($file,".");

// type is now the extention

if($type != "MS Excel Extention")
{
  die();
}
or a more secure way would be to check mime types. But, I understand that's rather tricky.. so I don't know how to do that one ;)

Re: The File That Shags Me.... a file upload film

Posted: Mon Aug 15, 2005 1:39 am
by harrisonad
Thanks, but i mean before submission
harrisonad wrote:... restrict the selection of file to only one extension before submission...
Can it be done in PHP or Just in client-side?

Posted: Mon Aug 15, 2005 1:52 am
by s.dot
o0o before submission. I suppose you could use a javascript to check the filename before submitted. If it is an invalid file, alert them, else continue.

Perhaps something along the lines of

Code: Select all

<script type="text/javascript">
<!--
function extract(what)
{
  if (what.indexOf('/') > -1)
  answer = what.substring(what.lastIndexOf('/')+1,what.length);
  else
  answer = what.substring(what.lastIndexOf('\\')+1,what.length);

  // the above will output the filename
}
//-->

  // HTML form
<form>
<input type="file" name="file">
<input type="submit" id="submit" onClick="extract(this.form.file.value);">
</form>
I don't know where to go from there.. but it's a start.
That code currently will extract the name of the file and submit it to the javascript when the submit button is clicked. Getting the extension shouldn't be much harder.

Posted: Mon Aug 15, 2005 2:05 am
by harrisonad
Thank you very much. You have been helpfull.