Avatar billede lyhr007 Nybegynder
02. januar 2001 - 14:24 Der er 10 kommentarer og
1 løsning

Billede upload til database og visning!

Jeg kan ikke vise billedet fra databasen igen, jeg er rimelig sikker på at data i DB er gode nok.

der er 2 filer:

index.php

har: <img src=\"showpic.php?id=2\" alt=\"Round corner picture\" height=\"30\" width=\"30\" />

showpic.php ser sådan ud:

<?

if($id) {

    @MYSQL_CONNECT(\"***\",\"***\",\"***\");

    @mysql_select_db(\"***\");

    $query = \"SELECT a_pictures.bin_data, a_pictures.filetype, a_pictures.filename FROM a_pictures WHERE id = $id\";
    $result = @MYSQL_QUERY($query);

    $data = @MYSQL_RESULT($result,0,\"bin_data\");
    $type = @MYSQL_RESULT($result,0,\"filetype\");
    $name = @MYSQL_RESULT($result,0,\"filename\");

    Header( \"Content-disposition: filename=$name\");
    Header( \"Content-type: $type\");
    echo $data;

} // if
?>

jeg kan er ikke 100% sikker på at data er i orden, der kan evt. være noget med magic-quotes. Jeg har lavet det her før hvor det virkede, men jeg er kommet tilat slette det. Det hele er taget mere eller mindre fra phpbuilder.com

Håber der er en der kan se min fejl, kan evt. oxo være et opsætningsproblem?
Avatar billede mrcorex Nybegynder
02. januar 2001 - 14:29 #1
Jeg lytter lige med!
Avatar billede pallepop Nybegynder
02. januar 2001 - 14:36 #2
lav  et tjek på resultatet med:
($result = mysql_db_query(\"DBnavn\",$query))||die ($sql.mysql_error());
og se om der er fejl i din sql forespørgsel
Avatar billede codebase Praktikant
02. januar 2001 - 14:39 #3
dette script virker for mig!
upload.php:

<html>
    <head>
        <title>Upload</title>
    </head>
<body>

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

#--------------------------------#
# 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 = \"\"; to
// upload to the current directory. Whatever directory
// you choose, please chmod 777 that directory.

    $PATH = \"upload/\";

// 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/txt/asp/html/htm/asp/php/php3/pl/class/doc/txt\";
    $ACCEPT = \"image/gif/tif/jpg/png\";
// 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 = 2;


#--------------------------------#
# 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(1000000);
// if($upload->upload(\"$FILENAME\", \"$ACCEPT\", \"$EXTENSION\")) {
if($upload->upload(\"$FILENAME\",\"\",\"\")) {
    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=\"1000000\">Send this file:
        <input name=\"userfile\" type=\"file\">
        <input type=\"submit\" value=\"Send File\">
    </form>
    <hr>
<?
//    if ($ACCEPT) {
//        print(\"This form only accepts <b>\" . $ACCEPT . \"</b> files\\n\");
//    }
?>

</body>
</html>




fileupload.php
<?
/*
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;
        }
    }
}
?>


readme.txt
#--------------------------------#
# ABOUT
#--------------------------------#

fileupload.class can be used to upload image and text files with a web
browser. The uploaded file\'s name will get cleaned up - special characters
will be deleted, and spaces get replaced with underscores, and moved to
a specified directory (on your server). fileupload.class also does its
best to determine the file\'s type (text, GIF, JPEG, etc). If the user
has named the file with the correct extension (.txt, .gif, etc), then
the class will use that, but if the user tries to upload an ex tensionless
file, PHP does can identify text, gif, jpeg, and png files for you. As
a last resort, if there is no specified extension, and PHP can not
determine the type, you can set a default extension to be added.

#--------------------------------#
# REQUIREMENTS
#--------------------------------#

To run and have fun with fileupload.class you will need access to a
Unix/Linux/*nix web server running Apache with the PHP module, a web
browser that supports uploading (like Netscape), and the 2 other files
that came with this.

#--------------------------------#
# QUICK SETUP
#--------------------------------#

1) Make a new directory within your with you web directory called \"fileupload\"
2) Upload the files \"fileupload.class\" and \"upload.php\" (depending on
how you\'ve got Apache configured, you may need to rename this \"upload.php3\")
3) Make another directory within the \"fileupload\" directory called \"uploads\"
and give it enough upload permissions for you web server to upload to it.
(usually, this means making it world writable)
- cd /your/web/dir/fileupload
- mkdir uploads
- chmod 777 uploads
4) Fire up Netscape of IE and hit test it out:
  http://www.yourdomain.com/fileupload

#--------------------------------#
# DETAILED INSTRUCTIONS
#--------------------------------#

You should have downloaded 3 files:
- README.txt (this file)
- fileupload.class
- upload.php

fileupload.class:
    This is the file that does all the work. You shouldn\'t have to
    change anything in this file, unless you\'re getting lots of errors.
    Lines 114 and 122 can be changed from $aok = copy(...); to
    $aok = rename(...);

upload.php
    Here\'s where all the variables reside (see below for an explaination of
    each). NOTE you may need to rename this file upload.php3, depending
    on how you\'ve got Apache configured.

VARIABLES in \"upload.php\":

// 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 = \"\"; 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;

#--------------------------------#
# LICENSE
#--------------------------------#

/*
///// fileupload.class /////
Copyright (c) 1999 David Fox, Angryrobot Productions
(http://www.angryrobot.com) All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. 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.
3. Neither the name of author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.

DISCLAIMER:
THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/

-codebase
Avatar billede repsac Nybegynder
02. januar 2001 - 14:44 #4
Jo tak mand ... det er jo også 50 sider langt ...
Avatar billede jd Nybegynder
02. januar 2001 - 15:55 #5
<?php
// files in mysql by Jonas Delfs
// jonas@delfs.dk
// http://php.delfs.dk

$connect=mysql_connect(\"MYSQLSERVER\",\"USERNAME\",\"PASSWORD\");
mysql_select_db(\"YOURDB\",$connect);

$q=mysql_query(\"select file_code,file_type from files where id=\'$id\'\") or die(mysql_error());
if(mysql_num_rows($q)){
extract(mysql_fetch_array($q));

header(\"Content-type: $file_type\");
echo $file_code;
}
else echo \"404 - file not found!\";
?>

-noget du kan bruge? ( http://delfs.dk/php/?p=articles&id=1 )

Mvh. Jonas Delfs
Avatar billede emileej Nybegynder
02. januar 2001 - 16:09 #6
Lytter med :)
Avatar billede glaede.dk Nybegynder
02. januar 2001 - 18:39 #7
ehm...I folk der mangler / har fejl i et Upload system...Kig dog i arkivet...Der er millarder af \"Upload spørgsmål\"...Sys der er ved at være nok af dem :o)
Avatar billede lyhr007 Nybegynder
02. januar 2001 - 23:00 #8
Jeg har kigget alle tidligere spg. her igennem, plus alt hvad der var på phpbuilder. Som skrevet tidligere har jeg haft det til at virke tidligere, men kom til at slette det. Jeg køre det på en IIS 5.0 men det kommer til at ligge på en *nix server senere efter udvikling, så det skulle helst kunne køre på begge platforme.

Jeg er ikke lige hjemme nu, så jeg prøver at indsætte \"or die\" i min sql for at se om fejlen er der.

Ellers må jeg jo bare vente på at erik jacobsen kommer online :)

codebase når jeg for tid kigger jeg lige din kode igennem....den er jo 50 sider lang =)
Avatar billede lyhr007 Nybegynder
30. marts 2001 - 15:41 #9
Lukker og slukker!
Avatar billede nikolajdu Nybegynder
30. marts 2001 - 16:09 #10
Lyhr007 - Du er da vist et kæmpe fjols. Du spørger og får hjælp - for kun at afvie alle svar??? Det koster dig sgu intet andet at spørge. Forvent ikke at folk gider hjælpe dig på de betingelser.

http://www.eksperten.dk/spm/48897
Avatar billede emileej Nybegynder
31. marts 2001 - 11:40 #11
>>Lyhr007
Hva faen er du for en skidt karl - man kan altså ikke bare stå og vifte med pointene, og derefter stikke dem tilbage i lommen efter, at have fået hjælp!

Såddan nogle som dig har eksperten.dk ikke brug for. Hvis du ikke stopper den stil omgående, må vi kontakte admin, med hendblik på sletning af din profil!

E-)mil
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