SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Apr 2007
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to install a Php Class script?

    I've downloaded a copy of Easy PHP Upload, I thought it would come with install instructions, but as I'm learning a Class file is different. Can someone give me some basics on how/where to install such a program? Thanks.

    <?php
    /*
    Easy PHP Upload - version 2.31
    A easy to use class for your (multiple) file uploads

    Copyright (c) 2004 - 2006, Olaf Lederer
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the finalwebsites.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    ______________________________________________________________________
    available at http://www.finalwebsites.com/snippets.php
    Comments & suggestions: http://www.webdigity.com/index.php/b....html,ref.olaf
    */

    class file_upload {

    var $the_file;
    var $the_temp_file;
    var $upload_dir;
    var $replace;
    var $do_filename_check;
    var $max_length_filename = 100;
    var $extensions;
    var $ext_string;
    var $language;
    var $http_error;
    var $rename_file; // if this var is true the file copy get a new name
    var $file_copy; // the new name
    var $message = array();
    var $create_directory = true;

    function file_upload() {
    $this->language = "en"; // choice of en, nl, es
    $this->rename_file = false;
    $this->ext_string = "";
    }
    function show_error_string() {
    $msg_string = "";
    foreach ($this->message as $value) {
    $msg_string .= $value."<br />\n";
    }
    return $msg_string;
    }
    function set_file_name($new_name = "") { // this "conversion" is used for unique/new filenames
    if ($this->rename_file) {
    if ($this->the_file == "") return;
    $name = ($new_name == "") ? strtotime("now") : $new_name;
    sleep(3);
    $name = $name.$this->get_extension($this->the_file);
    } else {
    $name = str_replace(" ", "_", $this->the_file); // space will result in problems on linux systems
    }
    return $name;
    }
    function upload($to_name = "") {
    $new_name = $this->set_file_name($to_name);
    if ($this->check_file_name($new_name)) {
    if ($this->validateExtension()) {
    if (is_uploaded_file($this->the_temp_file)) {
    $this->file_copy = $new_name;
    if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
    $this->message[] = $this->error_text($this->http_error);
    if ($this->rename_file) $this->message[] = $this->error_text(16);
    return true;
    }
    } else {
    $this->message[] = $this->error_text($this->http_error);
    return false;
    }
    } else {
    $this->show_extensions();
    $this->message[] = $this->error_text(11);
    return false;
    }
    } else {
    return false;
    }
    }
    function check_file_name($the_name) {
    if ($the_name != "") {
    if (strlen($the_name) > $this->max_length_filename) {
    $this->message[] = $this->error_text(13);
    return false;
    } else {
    if ($this->do_filename_check == "y") {
    if (preg_match("/^[a-z0-9_]*\.(.){1,5}$/i", $the_name)) {
    return true;
    } else {
    $this->message[] = $this->error_text(12);
    return false;
    }
    } else {
    return true;
    }
    }
    } else {
    $this->message[] = $this->error_text(10);
    return false;
    }
    }
    function get_extension($from_file) {
    $ext = strtolower(strrchr($from_file,"."));
    return $ext;
    }
    function validateExtension() {
    $extension = $this->get_extension($this->the_file);
    $ext_array = $this->extensions;
    if (in_array($extension, $ext_array)) {
    // check mime type hier too against allowed/restricted mime types (boolean check mimetype)
    return true;
    } else {
    return false;
    }
    }
    // this method is only used for detailed error reporting
    function show_extensions() {
    $this->ext_string = implode(" ", $this->extensions);
    }
    function move_upload($tmp_file, $new_file) {
    if ($this->existing_file($new_file)) {
    $newfile = $this->upload_dir.$new_file;
    if ($this->check_dir($this->upload_dir)) {
    if (move_uploaded_file($tmp_file, $newfile)) {
    umask(0);
    chmod($newfile , 0644);
    return true;
    } else {
    return false;
    }
    } else {
    $this->message[] = $this->error_text(14);
    return false;
    }
    } else {
    $this->message[] = $this->error_text(15);
    return false;
    }
    }
    function check_dir($directory) {
    if (!is_dir($directory)) {
    if ($this->create_directory) {
    umask(0);
    mkdir($directory, 0777);
    return true;
    } else {
    return false;
    }
    } else {
    return true;
    }
    }
    function existing_file($file_name) {
    if ($this->replace == "y") {
    return true;
    } else {
    if (file_exists($this->upload_dir.$file_name)) {
    return false;
    } else {
    return true;
    }
    }
    }
    function get_uploaded_file_info($name) {
    $str = "File name: ".basename($name)."\n";
    $str .= "File size: ".filesize($name)." bytes\n";
    if (function_exists("mime_content_type")) {
    $str .= "Mime type: ".mime_content_type($name)."\n";
    }
    if ($img_dim = getimagesize($name)) {
    $str .= "Image dimensions: x = ".$img_dim[0]."px, y = ".$img_dim[1]."px\n";
    }
    return $str;
    }
    // this method was first located inside the foto_upload extension
    function del_temp_file($file) {
    $delete = @unlink($file);
    clearstatcache();
    if (@file_exists($file)) {
    $filesys = eregi_replace("/","\\",$file);
    $delete = @system("del $filesys");
    clearstatcache();
    if (@file_exists($file)) {
    $delete = @chmod ($file, 0644);
    $delete = @unlink($file);
    $delete = @system("del $filesys");
    }
    }
    }
    // this function creates a file field and if $show_alternate is true it will show a text field if the given file already exists
    // there is also a submit button to remove the text field value
    function create_file_field($element, $label = "", $length = 25, $show_replace = true, $replace_label = "Replace old file?", $file_path = "", $file_name = "", $show_alternate = false, $alt_length = 30, $alt_btn_label = "Delete image") {
    $field = ($label != "") ? "<label>".$label."</label>\n" : "";
    $file_field = "<input type=\"file\" name=\"".$element."\" size=\"".$length."\" />\n";
    $file_field .= ($show_replace) ? "<span>".$replace_label."</span><input type=\"checkbox\" name=\"replace\" value=\"y\" />" : "";
    if ($file_name != "" && $show_alternate) {
    $field .= "<input type=\"text\" name=\"".$element."\" size=\"".$alt_length."\" value=\"".$file_name."\" readonly=\"readonly\"";
    $field .= (!@file_exists($file_path.$file_name)) ? " title=\"".sprintf($this->error_text(17), $file_name)."\" />\n" : " />\n";
    $field .= "<input type=\"checkbox\" name=\"del_img\" value=\"y\" /><span>".$alt_btn_label."</span>\n";
    } else {
    $field .= $file_field;
    }
    return $field;
    }
    // some error (HTTP)reporting, change the messages or remove options if you like.
    function error_text($err_num) {
    switch ($this->language) {
    case "nl":
    $error[0] = "Foto succesvol kopieert.";
    $error[1] = "Het bestand is te groot, controlleer de max. toegelaten bestandsgrootte.";
    $error[2] = "Het bestand is te groot, controlleer de max. toegelaten bestandsgrootte.";
    $error[3] = "Fout bij het uploaden, probeer het nog een keer.";
    $error[4] = "Fout bij het uploaden, probeer het nog een keer.";
    $error[10] = "Selecteer een bestand.";
    $error[11] = "Het zijn alleen bestanden van dit type toegestaan: <b>".$this->ext_string."</b>";
    $error[12] = "Sorry, de bestandsnaam bevat tekens die niet zijn toegestaan. Gebruik alleen nummer, letters en het underscore teken. <br>Een geldige naam eindigt met een punt en de extensie.";
    $error[13] = "De bestandsnaam is te lang, het maximum is: ".$this->max_length_filename." teken.";
    $error[14] = "Sorry, het opgegeven directory bestaat niet!";
    $error[15] = "Uploading <b>".$this->the_file."...Fout!</b> Sorry, er is al een bestand met deze naam aanwezig.";
    $error[16] = "Het gekopieerde bestand is hernoemd naar <b>".$this->file_copy."</b>.";
    $error[17] = "Het bestand %s bestaat niet.";
    break;
    case "de":
    $error[0] = "Die Datei: <b>".$this->the_file."</b> wurde hochgeladen!";
    $error[1] = "Die hochzuladende Datei ist gr&ouml;&szlig;er als der Wert in der Server-Konfiguration!";
    $error[2] = "Die hochzuladende Datei ist gr&ouml;&szlig;er als der Wert in der Klassen-Konfiguration!";
    $error[3] = "Die hochzuladende Datei wurde nur teilweise &uuml;bertragen";
    $error[4] = "Es wurde keine Datei hochgeladen";
    $error[10] = "W&auml;hlen Sie eine Datei aus!.";
    $error[11] = "Es sind nur Dateien mit folgenden Endungen erlaubt: <b>".$this->ext_string."</b>";
    $error[12] = "Der Dateiname enth&auml;lt ung&uuml;ltige Zeichen. Benutzen Sie nur alphanumerische Zeichen f&uuml;r den Dateinamen mit Unterstrich. <br>Ein g&uuml;ltiger Dateiname endet mit einem Punkt, gefolgt von der Endung.";
    $error[13] = "Der Dateiname &uuml;berschreitet die maximale Anzahl von ".$this->max_length_filename." Zeichen.";
    $error[14] = "Das Upload-Verzeichnis existiert nicht!";
    $error[15] = "Upload <b>".$this->the_file."...Fehler!</b> Eine Datei mit gleichem Dateinamen existiert bereits.";
    $error[16] = "Die hochgeladene Datei ist umbenannt in <b>".$this->file_copy."</b>.";
    $error[17] = "Die Datei %s existiert nicht.";
    break;
    //
    // place here the translations (if you need) from the directory "add_translations"
    //
    default:
    // start http errors
    $error[0] = "File: <b>".$this->the_file."</b> successfully uploaded!";
    $error[1] = "The uploaded file exceeds the max. upload filesize directive in the server configuration.";
    $error[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form.";
    $error[3] = "The uploaded file was only partially uploaded";
    $error[4] = "No file was uploaded";
    // end http errors
    $error[10] = "Please select a file for upload.";
    $error[11] = "Only files with the following extensions are allowed: <b>".$this->ext_string."</b>";
    $error[12] = "Sorry, the filename contains invalid characters. Use only alphanumerical chars and separate parts of the name (if needed) with an underscore. <br>A valid filename ends with one dot followed by the extension.";
    $error[13] = "The filename exceeds the maximum length of ".$this->max_length_filename." characters.";
    $error[14] = "Sorry, the upload directory doesn't exist!";
    $error[15] = "Uploading <b>".$this->the_file."...Error!</b> Sorry, a file with this name already exitst.";
    $error[16] = "The uploaded file is renamed to <b>".$this->file_copy."</b>.";
    $error[17] = "The file %s does not exist.";
    }
    return $error[$err_num];
    }
    }
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    http://www.phpclasses.org/browse/package/1841.html

    go to phpclasses.org, and there is an example page link on there

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    OK, theres a documentation.html and a list of methods and what they do, but I remember the first class I used, blo0dy confusing it was too ...

    Basically:
    1 Stick ALL that text in a php file - dont forget <?php and ?> tags fore and aft.
    2 call the file easyupload.class.php (you dont HAVE to call it that, but stick with me)
    3 stick that file in a folder on your local machine - somewhere you can have a play with it, inside your html public folder for now ... (be that /inetpub/wwwroot OR var/html/html or similar)
    4 make a php file in that same folder called say, upload.php
    5 upload.php
    PHP Code:
    <?php
    include('easyupload.class.php');

    $up = new file_upload;

    var_dump($up);


    ?>
    Now if you get this far without errors then that means you have the file included OK, and its ready to roll, NOW you need to read the documentation.html to find out how it works.

    All those functions (now termed methods as the function are in a class) will carry out a role.

    Try and add this into your new upload.php file:
    PHP Code:
    echo $up->error_text(10); 
    And you should see some text which looks slightly rude, but I am sure is not.

  4. #4
    Non-Member
    Join Date
    Apr 2007
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    Thanks for the help in layman's terms, but still very confusing.

    I put the text in a file called EZupload.php, put the code you suggested into Upload.php and put both files into a folder called Classes.

    Then I ftped the other files into /Classes/: upload_db_example.php and documentation.html.

    When I go to website.com/classes/upload.php I see:

    Fatal error: Call to undefined function: phpinclude() in /home/public_html/classes/upload.php on line 2

    I don't even know if that was the right file to access.

    Any help would be appreciated.

    Here's documentation.html:

    <?php
    include ($_SERVER['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 = $_SERVER['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)
    $my_upload->rename_file = true;

    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
    if ($my_upload->upload($new_name)) { // new name is an additional filename information, use this to rename the uploaded file
    $full_path = $my_upload->upload_dir.$my_upload->file_copy;
    $info = $my_upload->get_uploaded_file_info($full_path); // just some information about the uploaded file
    // ... or do something like insert the filename to the database
    }
    }
    ?>

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Couple of things here.

    1) from what you are saying you don't have a test rig on your own computer.

    If true, Get one. google "xamp" comes with windows flavours.

    (or - learn a lot about PHP, download and install it "manually")

    2) On your live server I have no way of knowing where your include files are being served from, though most installations will allow includes from the same directory as where the file is being called - in your case /classes

    3) if you insist on only checking/working on a public facing webserver, at least put directory protection on that folder - so you have to enter a user/password to use your "playground"

    4) you would not generally put your classes in public_html !

    I said do that because I imagined (wrongly) that you had a webserver / testrig on your local machine - my bad.

    Trying to get file uploading without understanding how many things can go wrong - without having a test rig on your own machine - your bad.

    Either search this forum for XAMP (or maybe WAMP), download it and come back if you have install problems, or even better - just go to www.php.net/download

    I'd del those files off your server till you get this issue sorted out, its far more comfortable, reliable to have your own server working in front of you.

    Good luck


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •