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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

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

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

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

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Thank you very much. You have been helpfull.
Post Reply