Avatar billede demaweb Nybegynder
01. maj 2001 - 15:07 Der er 12 kommentarer

PHP

Er der en der gider vise mig den kode der skal til for at, uploade billeder (GIF og JPG)?
Avatar billede gizmo-gizmo Nybegynder
01. maj 2001 - 15:14 #1
i upload.php:




<?
require(\"fileupload.class\");

#--------------------------------#
# Variables
#--------------------------------#

// The path to the directory where you want the
// uploaded files to be saved. This MUST end with a
// trailing slash unless you use $PATH = \"uploads/\"; to
// upload to the current directory. Whatever directory
// you choose, please chmod 777 that directory.

    $PATH = \"uploads/\";

// The name of the file field in your form.

    $FILENAME = \"userfile\";

// ACCEPT mode - if you only want to accept
// a certain type of file.
// possible file types that PHP recognizes includes:
//
// OPTIONS INCLUDE:
//  text/plain
//  image/gif
//  image/jpeg
//  image/png

    $ACCEPT = \"image/gif\";

// If no extension is supplied, and the browser or PHP
// can not figure out what type of file it is, you can
// add a default extension - like \".jpg\" or \".txt\"

    $EXTENSION = \"\";

// SAVE_MODE: if your are attempting to upload
// a file with the same name as another file in the
// $PATH directory
//
// OPTIONS:
//  1 = overwrite mode
//  2 = create new with incremental extention
//  3= do nothing if exists, highest protection

    $SAVE_MODE = 1;


#--------------------------------#
# PHP
#--------------------------------#

function print_file($file, $type, $mode) {
    if($file) {
        if(ereg(\"image\", $type)) {
            echo \"<img src=\\\"\" . $file . \"\\\" border=\\\"0\\\" alt=\\\"\\\">\";
        }
        else {
            $userfile = fopen($file, \"r\");
            while(!feof($userfile)) {
                $line = fgets($userfile, 255);
                switch($mode){
                    case 1:
                        echo $line;
                        break;
                    case 2:
                        echo nl2br(ereg_replace(\"\\t\", \"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\", htmlentities($line)));
                        break;
                }   
            }
        }
    }
}

$upload = new uploader;
$upload->max_filesize(30000);

if($upload->upload(\"$FILENAME\", \"$ACCEPT\", \"$EXTENSION\")) {
    while(list($key, $var) = each($upload->file)){
        echo $key . \" = \" . $var . \"<br>\";
    }
    if($upload->save_file(\"$PATH\", $SAVE_MODE)) {
        print(\"<p>Saved as: \" . $upload->new_file . \"<p>\");
        print_file($upload->new_file, $upload->file[\"type\"], 2);
    }
}

if($upload->errors) {
    while(list($key, $var) = each($upload->errors)){
        echo \"<p>\" . $var . \"<br>\";
    }
}

if ($NEW_NAME) {
    print(\"<p>Name of image save: <b>$NEW_NAME</b></p>\");
}

#--------------------------------#
# HTML FORM
#--------------------------------#
?>

    <form enctype=\"multipart/form-data\" action=\"<?print($PHP_SELF);?>\" method=\"POST\">
        <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"100000\">Send dette logo
        <input name=\"userfile\" type=\"file\">
        <input type=\"submit\" value=\"Send logo\">
    </form>
    <hr>
<?
    if ($ACCEPT) {
        print(\"Denne uplaoder acceptere kun <b>\" . $ACCEPT . \"</b> files\\n\");
    }
?>



Og i filen filupload.inc:
\'




<?
/*
Error codes:
    0 - \"No file was uploaded\"
    1 - \"Maximum file size exceeded\"
    2 - \"Maximum image size exceeded\"
    3 - \"Only specified file type may be uploaded\"
    4 - \"File already exists\" (save only)
*/

class uploader {

    var $file;
    var $errors;
    var $accepted;
    var $new_file;
    var $max_filesize;
    var $max_image_width;
    var $max_image_height;

    function max_filesize($size){
        $this->max_filesize = $size;
    }

    function max_image_size($width, $height){
        $this->max_image_width = $width;
        $this->max_image_height = $height;
    }

    function upload($filename, $accept_type, $extention) {
        // get all the properties of the file
        $index = array(\"file\", \"name\", \"size\", \"type\");
        for($i = 0; $i < 4; $i++) {
            $file_var = \'$\' . $filename . (($index[$i] != \"file\") ? \"_\" . $index[$i] : \"\");
            eval(\'global \' . $file_var . \';\');
            eval(\'$this->file[$index[$i]] = \' . $file_var . \';\');
        }
   
        if($this->file[\"file\"] && $this->file[\"file\"] != \"none\") {
            //test max size
            if($this->max_filesize && $this->file[\"size\"] > $this->max_filesize) {
                $this->errors[1] = \"Maximum file size exceeded. File may be no larger than \" . $this->max_filesize/1000 . \"KB.\";
                return False;
            }
            if(ereg(\"image\", $this->file[\"type\"])) {
                $image = getimagesize($this->file[\"file\"]);
                $this->file[\"width\"] = $image[0];
                $this->file[\"height\"] = $image[1];
           
                // test max image size
                if(($this->max_image_width || $this->max_image_height) && (($this->file[\"width\"] > $this->max_image_width) || ($this->file[\"height\"] > $this->max_image_height))) {
                    $this->errors[2] = \"Maximum image size exceeded. Image may be no more than \" . $this->max_image_width . \" x \" . $this->max_image_height . \" pixels\";
                    return False;
                }   
                switch($image[2]) {
                    case 1:
                        $this->file[\"extention\"] = \".gif\";
                        break;
                    case 2:
                        $this->file[\"extention\"] = \".jpg\";
                        break;
                    case 3:
                        $this->file[\"extention\"] = \".png\";
                        break;
                    default:
                        $this->file[\"extention\"] = $extention;
                        break;
                }
            }
            else if(!ereg(\"(\\.)([a-z0-9]{3,5})$\", $this->file[\"name\"]) && !$extention) {
                // add new mime types here
                switch($this->file[\"type\"]) {
                    case \"text/plain\":
                        $this->file[\"extention\"] = \".txt\";
                        break;
                    default:
                        break;
                }           
            }
            else {
                $this->file[\"extention\"] = $extention;
            }
       
            // check to see if the file is of type specified
            if($accept_type) {
                if(ereg($accept_type, $this->file[\"type\"])) { $this->accepted = True; }
                else { $this->errors[3] = \"Only \" . ereg_replace(\"\\|\", \" or \", $accept_type) . \" files may be uploaded\"; }
            }
            else { $this->accepted = True; }
        }
        else { $this->errors[0] = \"No file was uploaded\"; }
        return $this->accepted;
    }

    function save_file($path, $mode){
        global $NEW_NAME;
       
        if($this->accepted) {
            // very strict naming of file.. only lowercase letters, numbers and underscores
            $new_name = ereg_replace(\"[^a-z0-9._]\", \"\", ereg_replace(\" \", \"_\", ereg_replace(\"%20\", \"_\", strtolower($this->file[\"name\"]))));

            // check for extention and remove
            if(ereg(\"(\\.)([a-z0-9]{3,5})$\", $new_name)) {
                $pos = strrpos($new_name, \".\");
                if(!$this->file[\"extention\"]) { $this->file[\"extention\"] = substr($new_name, $pos, strlen($new_name)); }
                $new_name = substr($new_name, 0, $pos);
               
            }
            $this->new_file = $path . $new_name . $this->file[\"extention\"];
            $NEW_NAME = $new_name . $this->file[\"extention\"];
           
            switch($mode) {
                case 1: // overwrite mode
                    $aok = copy($this->file[\"file\"], $this->new_file);
                    break;
                case 2: // create new with incremental extention
                    while(file_exists($path . $new_name . $copy . $this->file[\"extention\"])) {
                        $copy = \"_copy\" . $n;
                        $n++;
                    }
                    $this->new_file = $path . $new_name . $copy . $this->file[\"extention\"];
                    $aok = copy($this->file[\"file\"], $this->new_file);
                    break;
                case 3: // do nothing if exists, highest protection
                    if(file_exists($this->new_file)){
                        $this->errors[4] = \"File &quot\" . $this->new_file . \"&quot already exists\";
                    }
                    else {
                        $aok = rename($this->file[\"file\"], $this->new_file);
                    }
                    break;
                default:
                    break;
            }
            if(!$aok) { unset($this->new_file); }
            return $aok;
        }
    }
}
?>








og i filen fileupload.class:





<?
/*
Error codes:
    0 - \"ingen filer er uploadet\"
    1 - \"Maximum file size exceeded\"
    2 - \"Maximum image size exceeded\"
    3 - \"Only specified file type may be uploaded\"
    4 - \"File already exists\" (save only)
*/

class uploader {

    var $file;
    var $errors;
    var $accepted;
    var $new_file;
    var $max_filesize;
    var $max_image_width;
    var $max_image_height;

    function max_filesize($size){
        $this->max_filesize = $size;
    }

    function max_image_size($width, $height){
        $this->max_image_width = $width;
        $this->max_image_height = $height;
    }

    function upload($filename, $accept_type, $extention) {
        // get all the properties of the file
        $index = array(\"file\", \"name\", \"size\", \"type\");
        for($i = 0; $i < 4; $i++) {
            $file_var = \'$\' . $filename . (($index[$i] != \"file\") ? \"_\" . $index[$i] : \"\");
            eval(\'global \' . $file_var . \';\');
            eval(\'$this->file[$index[$i]] = \' . $file_var . \';\');
        }
   
        if($this->file[\"file\"] && $this->file[\"file\"] != \"none\") {
            //test max size
            if($this->max_filesize && $this->file[\"size\"] > $this->max_filesize) {
                $this->errors[1] = \"Maximum file size exceeded. File may be no larger than \" . $this->max_filesize/1000 . \"KB.\";
                return False;
            }
            if(ereg(\"image\", $this->file[\"type\"])) {
                $image = getimagesize($this->file[\"file\"]);
                $this->file[\"width\"] = $image[0];
                $this->file[\"height\"] = $image[1];
           
                // test max image size
                if(($this->max_image_width || $this->max_image_height) && (($this->file[\"width\"] > $this->max_image_width) || ($this->file[\"height\"] > $this->max_image_height))) {
                    $this->errors[2] = \"Maximum image size exceeded. Image may be no more than \" . $this->max_image_width . \" x \" . $this->max_image_height . \" pixels\";
                    return False;
                }   
                switch($image[2]) {
                    case 1:
                        $this->file[\"extention\"] = \".gif\";
                        break;
                    case 2:
                        $this->file[\"extention\"] = \".jpg\";
                        break;
                    case 3:
                        $this->file[\"extention\"] = \".png\";
                        break;
                    default:
                        $this->file[\"extention\"] = $extention;
                        break;
                }
            }
            else if(!ereg(\"(\\.)([a-z0-9]{3,5})$\", $this->file[\"name\"]) && !$extention) {
                // add new mime types here
                switch($this->file[\"type\"]) {
                    case \"text/plain\":
                        $this->file[\"extention\"] = \".txt\";
                        break;
                    default:
                        break;
                }           
            }
            else {
                $this->file[\"extention\"] = $extention;
            }
       
            // check to see if the file is of type specified
            if($accept_type) {
                if(ereg($accept_type, $this->file[\"type\"])) { $this->accepted = True; }
                else { $this->errors[3] = \"Only \" . ereg_replace(\"\\|\", \" or \", $accept_type) . \" files may be uploaded\"; }
            }
            else { $this->accepted = True; }
        }
        else { $this->errors[0] = \"ingen filer er uploadet\"; }
        return $this->accepted;
    }

    function save_file($path, $mode){
        global $NEW_NAME;
       
        if($this->accepted) {
            // very strict naming of file.. only lowercase letters, numbers and underscores
            $new_name = ereg_replace(\"[^a-z0-9._]\", \"\", ereg_replace(\" \", \"_\", ereg_replace(\"%20\", \"_\", strtolower($this->file[\"name\"]))));

            // check for extention and remove
            if(ereg(\"(\\.)([a-z0-9]{3,5})$\", $new_name)) {
                $pos = strrpos($new_name, \".\");
                if(!$this->file[\"extention\"]) { $this->file[\"extention\"] = substr($new_name, $pos, strlen($new_name)); }
                $new_name = substr($new_name, 0, $pos);
               
            }
            $this->new_file = $path . $new_name . $this->file[\"extention\"];
            $NEW_NAME = $new_name . $this->file[\"extention\"];
           
            switch($mode) {
                case 1: // overwrite mode
                    $aok = copy($this->file[\"file\"], $this->new_file);
                    break;
                case 2: // create new with incremental extention
                    while(file_exists($path . $new_name . $copy . $this->file[\"extention\"])) {
                        $copy = \"_copy\" . $n;
                        $n++;
                    }
                    $this->new_file = $path . $new_name . $copy . $this->file[\"extention\"];
                    $aok = copy($this->file[\"file\"], $this->new_file);
                    break;
                case 3: // do nothing if exists, highest protection
                    if(file_exists($this->new_file)){
                        $this->errors[4] = \"File &quot\" . $this->new_file . \"&quot already exists\";
                    }
                    else {
                        $aok = rename($this->file[\"file\"], $this->new_file);
                    }
                    break;
                default:
                    break;
            }
            if(!$aok) { unset($this->new_file); }
            return $aok;
        }
    }
}
?>
Avatar billede demaweb Nybegynder
01. maj 2001 - 15:16 #2
Jeg har prøvet dette script før (uden held), men jeg kan da lige prøve det igen!
Avatar billede demaweb Nybegynder
01. maj 2001 - 15:17 #3
Du kan se at der i upload.php står følgende:

// ACCEPT mode - if you only want to accept
// a certain type of file.
// possible file types that PHP recognizes includes:
//
// OPTIONS INCLUDE:
//  text/plain
//  image/gif
//  image/jpeg
//  image/png

    $ACCEPT = \"image/gif\";

Men hvad skal jeg skrive hvis jeg både vil have gif og JPG ?
Avatar billede gizmo-gizmo Nybegynder
01. maj 2001 - 15:18 #4
--> demaweb

Jeg har brugt det før.
det virker meget fint her
Avatar billede demaweb Nybegynder
01. maj 2001 - 15:26 #5
Jeg kan ikke!

Kan du både uploade gif og jpg?
Avatar billede gizmo-gizmo Nybegynder
01. maj 2001 - 15:30 #6
ja, jeg kan...
øhh..var det ikke dig med det store talent??
Avatar billede demaweb Nybegynder
01. maj 2001 - 15:33 #7
Jo, det var mig!
hvad syntes du om designet på www.fodboldsiden.dk ?

Gider du ikke lige tjekke efter om du både kan uploade Gig og Jpg ?
Avatar billede gizmo-gizmo Nybegynder
01. maj 2001 - 15:35 #8
hvad er gig? mener du gif? ja, jeg HAR testet den. Brugte den på min gamle hjemmeside, til upload af operatør logoer.

---> fodboldsiden.dk
tjaa...jeg synes den er meget tom i det.
Bestemt ikke noget speicelt...!!
Avatar billede demaweb Nybegynder
01. maj 2001 - 15:38 #9
Hvorfor kan jeg så ikke?
Avatar billede gizmo-gizmo Nybegynder
01. maj 2001 - 15:40 #10
det veeeeeeed jeg da virkelig ikke
Avatar billede demaweb Nybegynder
01. maj 2001 - 15:42 #11
Når nu du ikke kan hjælpe må jeg gide pointene til mig selv!
Avatar billede gizmo-gizmo Nybegynder
01. maj 2001 - 15:44 #12
ja, gør du det.
eller også er det dig der ikke kan finde ud af at tilpasse det...
men tak for ulejligheden
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester