Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: PHP file upload class    Marked Cool    (Review this resource)
Author: olaf
Posted On: 2004-11-22
Category: Home > PHP Classes

Popularity: 1 points out of 10    

Description: This package contain PHP file upload class. The features in the first release are: file-extension check, maximum file- size limitation and a flexible error reporting system. The class is extremely easy to use and comes together with an full working example. In the second version it is possible to upload multiple files. Check the class file for all update information. New! image handling. (requires the GD-library)

Total Hits: 1193     Total Votes: 0     Total Points: 0 (0 reviews)        [ Download ]   

Page Navigation:  [1]


PHP upload_class


Easy to use upload functions for (multiple) file upload with validation.

Example (of use):
php:
<?php

include ($DOCUMENT_ROOT."/classes/upload/upload_class.php"); //classes is the map where the class file is stored (one above the root)
$max_size = 1024*100; // the max. size for uploading
$my_upload = new file_upload;
$my_upload->upload_dir = $DOCUMENT_ROOT."/files/"; // "files" is the folder for the uploaded files (you have to create this folder)
$my_upload->extensions = array(".png", ".zip"); // specify the allowed extensions here
// $my_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
$my_upload->max_length_filename = 50; // change this value to fit your field length in your database (standard 100)

if(isset($Submit)) {
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
$my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename
$my_upload->upload();
// use the following if clause to do something if the upload is true
/* if ($my_upload->upload()) {
do something ... like insert the filename to the database
} */

}

?>

Variables (upload_class.php):

  • $the_file = The name of the original (uploaded) file
  • $the_temp_file = The temporay name of the uploaded file
  • $upload_dir = The directory where the file will be uploaded
  • $replace = Set this var to true if an existing file should be replaced (default = false)
  • $do_filename_check = This var is used as a switch, possible setting y/n. In position 'y' the name is validated while using regular expressions.
  • $max_length_filename = Set this value to take care of the maximum length of a filename (default = 100)
  • $extensions = This array holds all file extensions, you can add so much as you want.
  • $ext_string = This var is used for output (to show which extensions are allowed)
  • $language = You need this var if your messages should be in an other language then English
  • $http_error = This variable is required, this var holds the error reported in by $_FILES array.
  • $rename_file = Set this boolean if you want to (auto)rename a file (i use a timestamp is name)
  • $file_copy = This var holds the new name (if you rename/copy a file)
  • $message = This array is a container for all messages generated during the upload process.
Methodes (upload_class.php)
  • file_upload() = The constructor, sets the language, the rename boolean and empties the file extension string.
  • error_text($err_num) = This method is a multilanguage error selector, the property holds the error number
  • show_error_string() = This method is used to show all strings inside the message array
  • upload() = This method is used to handle the upload process
  • check_file_name() = Checks the "spelling" of a filename the length and that the name is not empty.
  • get_extension($from_file) = Isolate the files extension with this method
  • validateExtension() = This method checks for a valid file extension
  • show_extensions() = Use this method to build the extension string
  • move_upload($tmp_file, $new_file) = This method is a group of functions which move the temporary file to his upload direction.
  • check_dir() = Check if the upload directory exists or not
  • existing_file() = This method checks if a file exists

Vars and methodes for a attached extensions will follow on a later moment.

updated in november 2004



Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.