设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: IT公社 IT community >> 网络编程 >> PHP >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
PHP安全之:重燃你的Php…
PHP+MySQL应用中使用XOR…
PHP中使用crypt()实现用…
详细介绍:Apache+PHP+M…
建立Apache+PHP3+MySQL驱…
使用C#开发SmartPhone程…
通过反射填充泛型集合Li…
PHP自动更新新闻DIY
PHP中文函数连载(一)
PHP中文函数连载(二)
  通过PHP的File函数库来完成上传图像文件并让其显示           
通过PHP的File函数库来完成上传图像文件并让其显示
 

通过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 $na

[1] [2] 下一页  

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2006-11-9
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:admin    责任编辑:admin 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持