Flash Games

 FAQ   Search   Members   Groups   User Control Panel      Login 

It is currently Thu Jan 08, 2009 4:18 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: PHP upload file function
PostPosted: Tue May 31, 2005 12:54 am 
Offline

Joined: Tue Aug 17, 2004 12:51 am
Posts: 12
Location: Czech Republic
For all that have problems with file uploading using POST form (with INPUT TYPE=FILE) here is my example:

Code:
// function for uploading - input is named "userfile" - by LamiCZE
function upload_file($path, $max_size, $allowed_extension1, $allowed_extension2, $allowed_extension3){
 
  // get temp filename
  $tmp = $_FILES['userfile']['tmp_name'];
  // get real filename
  $file_name = $_FILES['userfile']['name'];
  // get file size
  $file_size = $_FILES['userfile']['size'];
  // use func parameters to construct save path
  $place_file = "$path/$file_name";
  // get file type according to its extension
  $file_type = substr($file_name, -4);
  // is extension all right? If true, DOT has to be in the extension
  $file_has_extension = strpos($file_type, ".");
 
  // do this if file is grater than func parameter
  if($file_size > $max_size){ header("Location: $script_name?post_action=too_big&size=$file_size"); exit(); }
 
  // do if no file selected
  if(!is_file($tmp)){ header("Location: $script_name?post_action=no_file"); exit(); }
 
  // do if everything seems OK, but check if DOT is at first place in our extension (for PHP is 0)
  if((($file_type == $allowed_extension1) || ($file_type == $allowed_extension2) || ($file_type == $allowed_extension3)) && ($file_has_extension == 0)){
    move_uploaded_file($tmp, $place_file);
    header("Location: $script_name?post_action=upload_done&filename=$file_name");
 
  // something gets bad with extension...
  }else{
    // Ooops - we have no extension...
    if($file_has_extension === false){ header("Location: $script_name?post_action=no_extension"); exit(); }
    // We have extension, but not as expected in func parameters
    if($file_has_extension !== false){ header("Location: $script_name?post_action=bad_file&extension=$file_type"); exit(); }
  }
}
// **************** end ******************


Example:
Code:
// dir is "configs", max size 50kB, allowed ext. TAR, RAR, ZIP (some could be empty with "")
upload_file("./configs","50000",".tar",".rar",".zip");

------
Form page header for errors:

Code:
if($post_action == no_extension){ echo "<H2><font color=\"#FF0000\">Sry, no extension</font></H2>"; }

etc...

_________________
::: PHP/CSS webmaster - beginner - as Czech ET clan ::: http://3nigma.php5.cz


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 9:59 am 
Offline
Site Admin
User avatar

Joined: Tue Aug 17, 2004 2:07 pm
Posts: 2505
Location: Sweden
What is the problem, do you get any error messages?

_________________
Make sure to check out our TNX Review and Link Vault Review


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 11:47 am 
Offline

Joined: Tue Aug 17, 2004 12:51 am
Posts: 12
Location: Czech Republic
No, it is completely functional ;) Sry for posting here, pls move it to script repository, I didn`t notice it :(

_________________
::: PHP/CSS webmaster - beginner - as Czech ET clan ::: http://3nigma.php5.cz


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:52 pm 
Offline
100+ Club
User avatar

Joined: Thu Oct 07, 2004 10:53 pm
Posts: 198
Location: Ontario, Canada
Moved :)

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 11:07 pm 
Offline
500+ Club
User avatar

Joined: Sun Nov 21, 2004 5:12 am
Posts: 826
Location: 127.0.0.1
nice script!

I use one that malcolm wrote for me that it kind of similar

_________________
Web-Developing since '03
Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group - Flash Games - TNX Invitation Code - TNX Review

Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User:
Pass:
Log me on automatically each visit: