仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1330|回复: 20
打印 上一主题 下一主题

[学习教程] PHP网页编程之经由过程PHP的File函数库来完成上传图象文件...

[复制链接]
老尸 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:56:53 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
讲了这么多,无非是想说:学习PHP不仅要掌握方法,更多的是付出汗水,我不希望看到中途放弃的人,相信自己,相信自己的选择,更要相信自己的能力,如果自己想放弃,暴力一点的话,就自己抽自己一个嘴巴。函数|上传|显示   经由过程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>
那么接下来,这就算学会啦?NO,NO,NO,还早呢,你至尽还没碰过OOP之类的吧?模板呢?
再见西城 该用户已被删除
21#
发表于 2015-6-24 16:56:39 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
兰色精灵 该用户已被删除
20#
发表于 2015-6-21 22:00:55 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
19#
发表于 2015-5-12 15:14:15 | 只看该作者
如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,
小女巫 该用户已被删除
18#
发表于 2015-5-12 08:55:58 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
爱飞 该用户已被删除
17#
发表于 2015-4-25 15:14:51 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
飘灵儿 该用户已被删除
16#
发表于 2015-4-22 11:56:02 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
分手快乐 该用户已被删除
15#
发表于 2015-4-9 20:55:23 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
老尸 该用户已被删除
14#
 楼主| 发表于 2015-4-4 04:44:11 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
变相怪杰 该用户已被删除
13#
发表于 2015-3-26 23:40:52 | 只看该作者
首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。
柔情似水 该用户已被删除
12#
发表于 2015-3-24 13:27:06 | 只看该作者
其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
海妖 该用户已被删除
11#
发表于 2015-3-21 23:14:25 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
因胸联盟 该用户已被删除
10#
发表于 2015-3-15 09:38:10 | 只看该作者
其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
活着的死人 该用户已被删除
9#
发表于 2015-3-11 14:07:02 | 只看该作者
做为1门年轻的语言,php一直很努力。
admin 该用户已被删除
8#
发表于 2015-3-10 00:57:16 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
深爱那片海 该用户已被删除
7#
发表于 2015-3-6 08:22:48 | 只看该作者
真正的方向了,如果将来要去开发团队,你一定要学好smarty ,phplib这样的模板引擎,
灵魂腐蚀 该用户已被删除
6#
发表于 2015-3-4 05:29:34 | 只看该作者
最后介绍一个代码出错,但是老找不到错误方法,就是 go to wc (囧),出去换换气没准回来就找到错误啦。
若相依 该用户已被删除
5#
发表于 2015-2-14 09:48:38 | 只看该作者
爱上php,他也会爱上你。
山那边是海 该用户已被删除
地板
发表于 2015-2-6 21:55:28 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
小妖女 该用户已被删除
板凳
发表于 2015-2-4 10:04:17 | 只看该作者
建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-21 05:46

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表