Ankündigung

Einklappen
Keine Ankündigung bisher.

[Erledigt] Upload Script/Klasse

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • [Erledigt] Upload Script/Klasse

    Hallo liebe Gemeinde,

    Folgendes :

    Ich hab eine 'upload.php' & 'upload.class.php' sprich eine Upload Script .
    Bekomm mitlerweile keine Errors mehr aber das Problem ist , dass wenn ich auf 'Daten absenden' klicke nichts passiert!

    Hier einmal die 'upload.php':
    PHP-Code:
    <html>
    <body>
    <?php
    include("upload.class.php");

    if(
    $_FILES['upload']['tmp_name']) {
        
    $upload = new Upload();
        
    $upload->SetFileName($_FILES['upload']['name']);
        
    $upload->SetTempName($_FILES['upload']['tmp_name']);
        
    $upload->SetUploadDirectory("./upload"); //Upload directory, this should be writable
        
    $upload->SetValidExtensions(array('gif''jpg''jpeg''png')); //Extensions that are allowed if none are set all extensions will be allowed.
        //$upload->SetEmail("Sidewinder@codecall.net"); //If this is set, an email will be sent each time a file is uploaded.
        //$upload->SetIsImage(true); //If this is set to be true, you can make use of the MaximumWidth and MaximumHeight functions.
        //$upload->SetMaximumWidth(60); // Maximum width of images
        //$upload->SetMaximumHeight(400); //Maximum height of images
        
    $upload->SetMaximumFileSize(300000); //Maximum file size in bytes, if this is not set, the value in your php.ini file will be the maximum value
        
    echo $upload->UploadFile();

    }

    ?>
    <br><img scr="datei.jpg">
    <h2>Eine Datei hochladen</h2>
    <form action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="sent" value ="1">
    <p>Die Datei ausw&auml;hlen</p>
    <input type="file" name="neuedatei"><br><br>
    <input type="submit"><input type="Reset" value="Zur&uuml;cksetzen">
    </form>
    </body></html>
    Und hier die 'upload.class.php';
    PHP-Code:
    <?php
    /**
     * This class allows a user to upload and
     * validate their files.
     *
     * @author John Ciacia <Sidewinder@extreme-hq.com>
     * @version 1.0
     * @copyright Copyright (c) 2007, John Ciacia
     * @license [url=http://opensource.org/licenses/gpl-license.php]GNU General Public License Versions | Open Source Initiative[/url] GNU Public License
     */
    class Upload {

        
    /**
         *@var string contains the name of the file to be uploaded.
         */
        
    var $FileName;
        
    /**
         *@var string contains the temporary name of the file to be uploaded.
         */
        
    var $TempFileName;
        
    /**
         *@var string contains directory where the files should be uploaded.
         */
        
    var $UploadDirectory;
        
    /**
         *@var string contains an array of valid extensions which are allowed to be uploaded.
         */
        
    var $ValidExtensions;
        
    /**
         *@var string contains a message which can be used for debugging.
         */
        
    var $Message;
        
    /**
         *@var integer contains maximum size of fiels to be uploaded in bytes.
         */
        
    var $MaximumFileSize;
        
    /**
         *@var bool contains whether or not the files being uploaded are images.
         */
        
    var $IsImage;
        
    /**
         *@var string contains the email address of the recipient of upload logs.
         */
        
    var $Email;
        
    /**
         *@var integer contains maximum width of images to be uploaded.
         */
        
    var $MaximumWidth;
        
    /**
         *@var integer contains maximum height of images to be uploaded.
         */
        
    var $MaximumHeight;

        function 
    Upload()
        {

        }

        
    /**
         *@method bool ValidateExtension() returns whether the extension of file to be uploaded
         *    is allowable or not.
         *@return true the extension is valid.
         *@return false the extension is invalid.
         */
        
    function ValidateExtension()
        {

            
    $FileName trim($this->FileName);
            
    $FileParts pathinfo($FileName);
            
    $Extension strtolower($FileParts['extension']);
            
    $ValidExtensions $this->ValidExtensions;

            if (!
    $FileName) {
                
    $this->SetMessage("ERROR: File name is empty.");
                return 
    false;
            }

            if (!
    $ValidExtensions) {
                
    $this->SetMessage("WARNING: All extensions are valid.");
                return 
    true;
            }

            if (
    in_array($Extension$ValidExtensions)) {
                
    $this->SetMessage("MESSAGE: The extension '$Extension' appears to be valid.");
                return 
    true;
            } else {
                
    $this->SetMessage("Error: The extension '$Extension' is invalid.");
                return 
    false;
            }

        }

        
    /**
         *@method bool ValidateSize() returns whether the file size is acceptable.
         *@return true the size is smaller than the alloted value.
         *@return false the size is larger than the alloted value.
         */
        
    function ValidateSize()
        {
            
    $MaximumFileSize $this->MaximumFileSize;
            
    $TempFileName $this->GetTempName();
            
    $TempFileSize filesize($TempFileName);

            if(
    $MaximumFileSize == "") {
                
    $this->SetMessage("WARNING: There is no size restriction.");
                return 
    true;
            }

            if (
    $MaximumFileSize <= $TempFileSize) {
                
    $this->SetMessage("ERROR: The file is too big. It must be less than $MaximumFileSize and it is $TempFileSize.");
                return 
    false;
            }

            
    $this->SetMessage("Message: The file size is less than the MaximumFileSize.");
            return 
    true;
        }

        
    /**
         *@method bool ValidateExistance() determins whether the file already exists. If so, rename $FileName.
         *@return true can never be returned as all file names must be unique.
         *@return false the file name does not exist.
         */
        
    function ValidateExistance()
        {
            
    $FileName $this->FileName;
            
    $UploadDirectory $this->UploadDirectory;
            
    $File $UploadDirectory $FileName;

            if (
    file_exists($File)) {
                
    $this->SetMessage("Message: The file '$FileName' already exist.");
                
    $UniqueName rand() . $FileName;
                
    $this->SetFileName($UniqueName);
                
    $this->ValidateExistance();
            } else {
                
    $this->SetMessage("Message: The file name '$FileName' does not exist.");
                return 
    false;
            }
        }

        
    /**
         *@method bool ValidateDirectory()
         *@return true the UploadDirectory exists, writable, and has a traling slash.
         *@return false the directory was never set, does not exist, or is not writable.
         */
        
    function ValidateDirectory()
        {
            
    $UploadDirectory $this->UploadDirectory;

            if (!
    $UploadDirectory) {
                
    $this->SetMessage("ERROR: The directory variable is empty.");
                return 
    false;
            }

            if (!
    is_dir($UploadDirectory)) {
                
    $this->SetMessage("ERROR: The directory '$UploadDirectory' does not exist.");
                return 
    false;
            }

            if (!
    is_writable($UploadDirectory)) {
                
    $this->SetMessage("ERROR: The directory '$UploadDirectory' does not writable.");
                return 
    false;
            }

            if (
    substr($UploadDirectory, -1) != "/") {
                
    $this->SetMessage("ERROR: The traling slash does not exist.");
                
    $NewDirectory $UploadDirectory "/";
                
    $this->SetUploadDirectory($NewDirectory);
                
    $this->ValidateDirectory();
            } else {
                
    $this->SetMessage("MESSAGE: The traling slash exist.");
                return 
    true;
            }
        }

        
    /**
         *@method bool ValidateImage()
         *@return true the image is smaller than the alloted dimensions.
         *@return false the width and/or height is larger then the alloted dimensions.
         */
        
    function ValidateImage() {
            
    $MaximumWidth $this->MaximumWidth;
            
    $MaximumHeight $this->MaximumHeight;
            
    $TempFileName $this->TempFileName;

        if(
    $Size = @getimagesize($TempFileName)) {
            
    $Width $Size[0];   //$Width is the width in pixels of the image uploaded to the server.
            
    $Height $Size[1];  //$Height is the height in pixels of the image uploaded to the server.
        
    }

            if (
    $Width $MaximumWidth) {
                
    $this->SetMessage("The width of the image [$Width] exceeds the maximum amount [$MaximumWidth].");
                return 
    false;
            }

            if (
    $Height $MaximumHeight) {
                
    $this->SetMessage("The height of the image [$Height] exceeds the maximum amount [$MaximumHeight].");
                return 
    false;
            }

            
    $this->SetMessage("The image height [$Height] and width [$Width] are within their limitations.");
            return 
    true;
        }

        
    /**
         *@method bool SendMail() sends an email log to the administrator
         *@return true the email was sent.
         *@return false never.
         *@todo create a more information-friendly log.
         */
        
    function SendMail() {
            
    $To $this->Email;
            
    $Subject "File Uploaded";
            
    $From "From: Uploader";
            
    $Message "A file has been uploaded.";
            
    mail($To$Subject$Message$From);
            return 
    true;
        }


        
    /**
         *@method bool UploadFile() uploads the file to the server after passing all the validations.
         *@return true the file was uploaded.
         *@return false the upload failed.
         */
        
    function UploadFile()
        {

            if (!
    $this->ValidateExtension()) {
                die(
    $this->GetMessage());
            }

            else if (!
    $this->ValidateSize()) {
                die(
    $this->GetMessage());
            }

            else if (
    $this->ValidateExistance()) {
                die(
    $this->GetMessage());
            }

            else if (!
    $this->ValidateDirectory()) {
                die(
    $this->GetMessage());
            }

            else if (
    $this->IsImage && !$this->ValidateImage()) {
                die(
    $this->GetMessage());
            }

            else {

                
    $FileName $this->FileName;
                
    $TempFileName $this->TempFileName;
                
    $UploadDirectory $this->UploadDirectory;

                if (
    is_uploaded_file($TempFileName)) {
                    
    move_uploaded_file($TempFileName$UploadDirectory $FileName);
                    return 
    true;
                } else {
                    return 
    false;
                }

            }

        }

        
    #Accessors and Mutators beyond this point.
        #Siginificant documentation is not needed.
        
    function SetFileName($argv)
        {
            
    $this->FileName $argv;
        }

        function 
    SetUploadDirectory($argv)
        {
            
    $this->UploadDirectory $argv;
        }

        function 
    SetTempName($argv)
        {
            
    $this->TempFileName $argv;
        }

        function 
    SetValidExtensions($argv)
        {
            
    $this->ValidExtensions $argv;
        }

        function 
    SetMessage($argv)
        {
            
    $this->Message $argv;
        }

        function 
    SetMaximumFileSize($argv)
        {
            
    $this->MaximumFileSize $argv;
        }

        function 
    SetEmail($argv)
        {
            
    $this->Email $argv;
        }

        function 
    SetIsImage($argv)
        {
            
    $this->IsImage $argv;
        }

        function 
    SetMaximumWidth($argv)
        {
            
    $this->MaximumWidth $argv;
        }

        function 
    SetMaximumHeight($argv)
        {
            
    $this->MaximumHeight $argv;
        }
        function 
    GetFileName()
        {
            return 
    $this->FileName;
        }

        function 
    GetUploadDirectory()
        {
            return 
    $this->UploadDirectory;
        }

        function 
    GetTempName()
        {
            return 
    $this->TempFileName;
        }

        function 
    GetValidExtensions()
        {
            return 
    $this->ValidExtensions;
        }

        function 
    GetMessage()
        {
            if (!isset(
    $this->Message)) {
                
    $this->SetMessage("No Message");
            }

            return 
    $this->Message;
        }

        function 
    GetMaximumFileSize()
        {
            return 
    $this->MaximumFileSize;
        }

        function 
    GetEmail()
        {
            return 
    $this->Email;
        }

        function 
    GetIsImage()
        {
            return 
    $this->IsImage;
        }

        function 
    GetMaximumWidth()
        {
            return 
    $this->MaximumWidth;
        }

        function 
    GetMaximumHeight()
        {
            return 
    $this->MaximumHeight;
        }
    }


    ?>
    Er will die bilder auch nicht in mein Upload Ordner Speichern!
    Hoffe das mir jemand weiterhelfen kann ;]
    (<- Blutiger Anfänger also bitte so erklären das ich das auch verstehe ;])

  • #2
    Wenn was net geht: error_reporting !!!
    Vergleiche:
    Code:
    <input type="file" name="neuedatei"><br><br> 
    PHP-Code:
    if($_FILES['upload']['tmp_name']) { 
    Er will die bilder auch nicht in mein Upload Ordner Speichern!
    Dafür musst du gegebenenfalls auch die Schreibberechtingung ändern.

    Grüße
    Signatur:
    PHP-Code:
    $s '0048656c6c6f20576f726c64';
    while(
    $i=substr($s=substr($s,2),0,2))echo"&#x00$i;"

    Kommentar


    • #3
      Mhhh.. I-wie komm ich so nicht weiter ;?

      Also soll ich :
      PHP-Code:
      <input type="file" name="neuedatei"><br><br
      mit
      PHP-Code:
      if($_FILES['upload']['tmp_name']) { 
      Vergleichen =`?

      Grüße

      Kommentar


      • #4
        Wie heisst dein Upload Feld im HTML Formular, und welchen Namen prüfst du im Code?
        PHP-Code:
        print_r($_FILES); 
        Über 90% aller Gewaltverbrechen passieren innerhalb von 24 Stunden nach dem Konsum von Brot.

        Kommentar


        • #5
          Du lädst eine Datei hoch (name="neuedatei") und fragst ab ob die Datei (name="upload") hochgeladen wurde.
          Signatur:
          PHP-Code:
          $s '0048656c6c6f20576f726c64';
          while(
          $i=substr($s=substr($s,2),0,2))echo"&#x00$i;"

          Kommentar


          • #6
            Mh komm immer noch nicht weiter :P

            Kommentar


            • #7
              Dein Uploadfeld heisst "neuedatei" und nicht "upload"!
              Über 90% aller Gewaltverbrechen passieren innerhalb von 24 Stunden nach dem Konsum von Brot.

              Kommentar


              • #8
                Danke stimmt ich dussel hab das übersehen.
                Nun kommt wenn ich auf es hochlade folgender Error :

                MESSAGE: The traling slash exist.

                Kommentar


                • #9
                  Dann sieh in deinem Upload-Script nach wo die Message entsteht und was die bedeutet.
                  Signatur:
                  PHP-Code:
                  $s '0048656c6c6f20576f726c64';
                  while(
                  $i=substr($s=substr($s,2),0,2))echo"&#x00$i;"

                  Kommentar


                  • #10
                    MESSAGE: The traling slash exist.
                    Wieso gibst du das denn aus? Ist ziemlich unnötig. Weil wenn er nicht exestiert gibst du aus: "MESSAGE: The traling slash doenst exist."!

                    Was soll das dir bringen?

                    Kommentar


                    • #11
                      Hab ich ja nicht ausgegeben , das ist ja das witzige da dran! ;P
                      Nachdem ich : "neuedatei" in "upload" geändert habe , hab ich den Error bekommen.

                      Der ist mitlerweile verschwunden.

                      Ich hätt dann noch eine Frage.
                      Ich möchte das er die Bilder von Dateien unterscheidet und in 2 Verschiedene ordner speichert.
                      Momentan speichert er das ja alles im .../upload/ Ordner, aber wenn die User nun ihre Sachen hochladen dann mixt er ja Word Dokumente und Bilder usw zusammen. Deswegen hab ich 2 neue Ordner im Upload Ordner erstellt die : Bilder & Dateien heißen.

                      Hat da jemand eine Ahnung wie ich das machen soll?

                      Grüße

                      Kommentar


                      • #12
                        Du musst die Dateiendung überprüfen.
                        PHP-Code:
                        $extension end(explode("."$filename)); 
                        Und dann sehen ob das ganz eine Bild-Dateiendung hat.
                        PHP-Code:
                        $bilder = array('png''jpg''...');
                        if (
                        in_array($extension$bildertrue)) {
                            
                        //Bild
                        } else {
                            
                        //Kein Bild

                        Signatur:
                        PHP-Code:
                        $s '0048656c6c6f20576f726c64';
                        while(
                        $i=substr($s=substr($s,2),0,2))echo"&#x00$i;"

                        Kommentar


                        • #13
                          Das hab ich grad noch so verstanden.

                          Bloß
                          PHP-Code:
                          $bilder = array('png''jpg''...');
                          if (
                          in_array($extension$bildertrue)) {
                              
                          //Bild
                          } else {
                              
                          //Kein Bild

                          Ich muss ja noch definieren, wohin das bild muss und wohin die datei.. T_T

                          Kommentar


                          • #14
                            PHP-Code:

                            $bilder 
                            = array('png''jpg''...');
                            if (
                            in_array($extension$bildertrue)) {
                               
                            $upload->SetUploadDirectory("./bilder");
                            } else {
                                
                            $upload->SetUploadDirectory("./keinebilder");

                            PHP-Code:
                            echo "Hello World"

                            Kommentar


                            • #15
                              Naja um zu sagen wo hin das ganz soll musst du erstmal rausfinden ob es eine Bild oder ein "Nicht-Bild" ist.
                              Wenn du das gemacht hast, sagst du einfach, dass es in den einen oder anderen Ordner soll.
                              Für das festlegen des Upload-Ordners benutzt du einfach deine Upload-Klasse wie schon bei dem ersten Posting:
                              PHP-Code:
                              $upload->SetUploadDirectory("./upload"); //Upload directory, this should be writable 
                              Signatur:
                              PHP-Code:
                              $s '0048656c6c6f20576f726c64';
                              while(
                              $i=substr($s=substr($s,2),0,2))echo"&#x00$i;"

                              Kommentar

                              Lädt...
                              X