国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

通過PHP的File函數庫來完成上傳圖像文件并讓其顯示

2024-05-04 23:04:44
字體:
來源:轉載
供稿:網友
注冊會員,創建你的web開發資料庫,

通過file文件函數來操作上傳的圖片,下面是轉自zend.com上的一篇文章,有許多可取之處,但是卻感覺到眾多的目錄很古怪,大家看了后可以相互討論一下:

// file 1: display and process entry form and upload picture file to server
<?php

// full directory path
$filepath = "/home/httpd/html/tut/upload";

// 200k is the maximum (picture) file size to be accepted
define("max_file_size", 200*1024);

function print_error ($err) {
    echo "<h1>$err</h1><hr>";
}

do {
    // check if picture name variable has a value; if not, skip to the
    // "while(false)" section of "do" statement
    if(isset($picture)) {
        // here is where the server transparently checks that the client picture file
        // doesn't exceed maximum allowable size
        if(getenv("content_length") > max_file_size) {
            print_error("file too large: $picture_name");
            break;
        }

        // open client picture file for read only; "@" prefix tells fopen not to print
        // message if there is an error, since function print_error does that

        // if there is an error, break out of "do" loop and continue at "while(false)"

        $fp = @fopen($picture,"r");
        if(!$fp) {
            print_error("cannot open file: $picture_name");
            break;
        }

        // generate unique name for session, use it to generate unique server
        // directory name, and create the directory

        srand((double) microtime() * 1000000);
        $id = md5(uniqid(rand()));
        $dirname = "$filepath/$id";
        mkdir($dirname,0700);

        // create the server picture file in the newly created server directory
        $filename = $dirname . "/picture";

        // open server picture file for write only; "@" prefix tells fopen not to
        // print message if there is an error, since function print_error does that

        // if there is an error, break out of "do" loop and continue at "while(false)"
        $out = @fopen($filename,"w");
        if(!$out) {
            print_error("cannot open file: $filename");
            break;
        }

        // copy client picture file to server picture file
        while($buffer = fread($fp,8192)) {
            fwrite($out,$buffer);
        }

        // close client picture file and server picture file
        fclose($fp);
        fclose($out);

        // create server name file in picture file directory; this file will hold the
        // name of the picture file
        $filename = $dirname . "/name";

        // open server name file for write only; "@" prefix tells fopen not to print
        // message if there is an error, since function print_error does that

        // if there is an error, break out of "do" loop and continue at "while(false)"
        $out = @fopen($filename,"w");
        if(!$out) {
            print_error("cannot open file: $filename");
            break;
        }

        // write the server picture name to the server name file, and close the server
        // name file
        fputs($out,$name);
        fclose($out);

        // display message that client picture file was successfully copied to the
        // server, display a prompt to look at updated server photo gallery, and supply
        // the html link

?>

        picture added. thanks.<br>
        <a href="upload_display.php">continue to the gallery</a>

<?php

        // exit to the server photo gallery
        exit();
    }
} while(false);

// you get to here only when "if(isset($picture))" is false, which means that
// no picture name has been submitted, therefore go display the input form where
// the necessary information can be entered

?>

<!-- start upload form -->
<!doctype html public "-//ietf//dtd html//en">
<html>
<head>
    <title>photo gallery - add</title>
</head>

<body bgcolor="white">
<h1>photo gallery add</h1>

<?php

// start of segment of code for displaying input form

// using $php_self for value of "form action" causes form to refer to itself
// when "submit" button is clicked

?>

<form action="<? echo $php_self ?>" method=post enctype="multipart/form-data">

<?php

// pass the php constant max_file_size to the html maximum file size
// constant max_file_size

?>

<input type="hidden" name="max_file_size" value="<? echo max_file_size ?>">

<?php

// display the text boxes for entering user name and picture name, and store
// the entered values in php variables; browsing is enabled

?>

your name is: <input name="name"><br>
your picture: <input name="picture" type="file"><br>

<?php

// display the "submit" button

?>

<input type="submit" value="add picture" name="send">

</form>
</body>
</html>


// ------------------------------------------------------
// file 2: display the server photo gallery

<!doctype html public "-//ietf//dtd html//en">
<html>
<head>
    <title>photo gallery</title>
</head>

<body>
<h1>photo gallery</h1>

<?php

// full directory path
$filepath = "/home/httpd/html/tut/upload";

// user's path in browser -- same as full directory path
$url_path = "/tut/upload";

// get unique server directory used for this user session
$dir = dir($filepath);

// loop through all server subdirectories for this user session
while($entry=$dir->read()) {
    // if entry is system file (doesn't have picture files), go to next entry in
    // "while" loop
    if($entry == "." || $entry == "..") {
        continue;
    }

    // open server name file for read only; "@" prefix tells fopen not to print
    // message if there is an error,  since function print_error does that

    // if there is an error, go to next entry in "while" loop
    $fp = @fopen("$filepath/$entry/name","r");
    if(!$fp) {
        print "bad entry: $entry<br>";
        continue;
    }

    // get name of the server picture file and close the server name file
    $name = fgets($fp,4096);
    fclose($fp);

    // display each picture and its file name; in addition, "alt=" causes the file
    // name to be displayed as tooltip text when mouse points to picture

?>


    <img src="<? echo "$url_path/$entry/picture" ?>"
     alt="<? echo $name ?>"> <b><? echo $name ?></b><br>

<?
}
?>

</body>
</html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 稷山县| 武平县| 祁阳县| 松潘县| 青阳县| 密云县| SHOW| 双流县| 瓦房店市| 崇礼县| 登封市| 米泉市| 长泰县| 五原县| 金溪县| 阳春市| 兴宁市| 大埔县| 栾城县| 盐池县| 辰溪县| 安康市| 浮梁县| 稷山县| 三门峡市| 溧阳市| 彭山县| 建昌县| 张家界市| 遵义县| 廊坊市| 宣城市| 青田县| 商丘市| 南通市| 安福县| 稷山县| 霸州市| 无极县| 诸暨市| 平昌县|