Home php教程 php手册 一个广告轮播系统的例子(内含文件上传的方法)

一个广告轮播系统的例子(内含文件上传的方法)

Jun 21, 2016 am 09:02 AM
banner gt lt oracle

网路广告,变成了 Internet 上的热门学问。而 468x60 更变成了广告人员绞尽脑汁的尺寸。
在处理广告时,若能直接使用浏览器将广告的 468x60 图档送到处理广告的伺服器中,相信是件很舒服的事,不用再开 FTP 程式,搞大半天只为了 upload。

这个问题,是所有 Web CGI 程式的痛,包括 ASP、Prel....等等,都需要再经过系统元件的增加才能达成。号称最强的 Web CGI 程式: PHP,在这方面的表现没有令人失望,甚至傲视其它的 CGI 工具。

File Upload 功能在 RFC 1867 文件有有详细的说明,是利用特殊的文件格式(content-type) multipart/form-data。值得注意的是浏览器一定要用 Netscape 3.0以上或 MS Internet Explorer 4.0 以上的版本才能将档案上传。

先看下面的 HTML 原始码


method=post>
您的大名:

档案名称:





在 form 的标签中,要加入 enctype="multipart/form-data" 的字串,表示使用者输入的资料上有档案上传,同时 method 一定要用 POST 而不能用 GET。

在上面的码中,若使用者姓名填入 Wilson Peng,并选 c:\myphoto.gif 的档案,在使用者按下送出键后,浏览器则传送出下面的 POST 资料。


Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="user"

Wilson Peng
--AaB03x
content-disposition: form-data; name="myfile"
Content-type: multipart/mixed, boundary=BbC04y

--BbC04y
Content-disposition: attachment; filename="myphoto.gif"
Content-type: image/gif
Content-Transfer-Encoding: binary

...myphoto.gif 内容略...
--BbC04y--
--AaB03x--


看到上面的资料中,boundary=AaB03x 即为分开不同栏位资料的讯息,其中的AaB03x 编码方法,视浏览器的版本不同而异,通常是浏览器杂凑产生的。之后就可以
看到用 --AaB03x 来隔开不同的栏位。

以上面为例,处理 form 的 action 程式 next.php,会主动产生四个变数,见下表

变数名 说明
$myfile 即上传的档案内容
$myfile_name 上传档案在使用者端的名称
$myfile_size 上传档案的大小
$myfile_type 上传档案的格式,如 "image/gif"


在 next.php 程式要做的最重要动作,就是好好的使用这四个变数,否则程式一结束,使用者上传的档案就消失了。因此,要先将 $myfile 复制到存放广告图的目录中

copy($banner,"/home1/biglobe3/ad/".$banner_name);

这行程式就是将档案存在 /home/htdocs/ad 的目录中,就上面的例子而言,就将档案存到 /home/htdocs/ad/myphoto.gif。重要的是,存放的目录不能是 WebServer 无法读到的目录,而应放在网站的 Homepage 所在目录中,才可以在网路上看到。

或许程式要更细部的处理,例如比对取得的档案大小与系统回报的是否相同...,等等,就可以用 $myfile_size 变数了。

若在 form 中设定 input file 的名称改掉,则在 Upload 的变数也一起改,如



则变数就改成 $upfile、$upfile_name、$upfile_size、与 $upfile_type。

因此,下面的例子就利用 File Upload 及 Oracle 7.x 后端资料库,将档案放在 Web Homepage 目录中,相关资讯则存在 Oracle 中。当然,加上使用者认证,让有帐号的使用者才能上传图片,可避免刽客 (cracker) 等将不雅或不适当的广告上传。例中有关资料库的设定和 5.4 留言版的设定相同。




// adadd.php
if (($banner=="") and ($url=="")) {
?>
新增广告


加权值数字愈大,图片出现的机率就愈高,内定值为 1。
METHOD=POST>







广告 Banner: TYPE="file">
广告网址 URL: type=text size=30>
辅助字串 ALT: type=text size=30>
广告说明: type=text size=30>
显示加权: type=text size=5 value=1>



} else {
if (file_exists("/home/htdocs/ad/".$banner_name)) {
CommonHeader("档案 ".$banner_name." 已存在");
echo "



广告档案已经存在
\n



";
exit;
};

copy($banner,"/home1/biglobe3/ad/".$banner_name);

putenv("ORACLE_SID=WWW");
putenv("NLS_LANG=american_taiwan.zht16big5");
putenv("ORACLE_HOME=/home/oracle/product/7.3.2");
putenv("LD_LIBRARY_PATH=/home/oracle/product/7.3.2/lib");

putenv("ORA_NLS=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

putenv("ORA_NLS32=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

$handle=ora_logon("user38@WWW","iam3849") or die;
$cursor=ora_open($handle);
ora_commitoff($handle);

$query="insert into ad(url, banner, alt, descript, priority)
values('$url', '$banner_name', '$alt', '$descript', $priority)";
ora_parse($cursor, $query) or die;
ora_exec($cursor);
ora_close($cursor);
ora_logoff($handle);

echo "

广告新增完成";
echo "";
echo "";
echo "一个广告轮播系统的例子(内含文件上传的方法)alt=\"".$alt."\" border=0>

";
echo "

    ";
    echo "
  • 广告网址: ".$url;
    echo "
  • 辅助字串: ".$alt;
    echo "
  • 广告说明: ".$descript;
    echo "
  • 显示加权: ".$priority;
    echo "
";
}

?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

Function to calculate the number of days between two dates in oracle Function to calculate the number of days between two dates in oracle May 08, 2024 pm 07:45 PM

The function in Oracle to calculate the number of days between two dates is DATEDIFF(). The specific usage is as follows: Specify the time interval unit: interval (such as day, month, year) Specify two date values: date1 and date2DATEDIFF(interval, date1, date2) Return the difference in days

The order of the oracle database startup steps is The order of the oracle database startup steps is May 10, 2024 am 01:48 AM

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

How to use interval in oracle How to use interval in oracle May 08, 2024 pm 07:54 PM

The INTERVAL data type in Oracle is used to represent time intervals. The syntax is INTERVAL <precision> <unit>. You can use addition, subtraction, multiplication and division operations to operate INTERVAL, which is suitable for scenarios such as storing time data and calculating date differences.

How to see the number of occurrences of a certain character in Oracle How to see the number of occurrences of a certain character in Oracle May 09, 2024 pm 09:33 PM

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

Oracle database server hardware configuration requirements Oracle database server hardware configuration requirements May 10, 2024 am 04:00 AM

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

How to replace string in oracle How to replace string in oracle May 08, 2024 pm 07:24 PM

The method of replacing strings in Oracle is to use the REPLACE function. The syntax of this function is: REPLACE(string, search_string, replace_string). Usage steps: 1. Identify the substring to be replaced; 2. Determine the new string to replace the substring; 3. Use the REPLACE function to replace. Advanced usage includes: multiple replacements, case sensitivity, special character replacement, etc.

See all articles