An example of an ad-carousel system (the method of uploading files)

Source: Internet
Author: User
Tags format copy file size file upload include log variables php and
Internet advertising has become a popular learning on the internet. And 468x60 became the size of the advertiser's brains.
In the process of advertising, if you can directly use the browser to the 468x60 map files to deal with ads in the server, I believe it is a very comfortable thing, do not have to open the FTP program, big Half-day only to upload.

This problem is the pain of all Web CGI programs, including ASP, Prel ... And so on, all need to be added to the system components to achieve. The most powerful Web CGI program: PHP, in this respect, did not disappoint, or even disdain for other CGI tools.

The file Upload feature is described in detail in RFC 1867 files, using a special file format (content-type) multipart/form-data. It is worth noting that browsers must use more than 3.0 of Netscape or MS Internet Explorer more than 4.0 version to upload files.

First look at the following HTML source code


<form enctype= "Multipart/form-data" action= "next.php"
Method=post>
Your name: <input type=text name=user><br>
File name: <input name= "myfile" type= "file" ><br>
<input type= "Submit" value= "Send Out" >
</form>


In the form label, to join the enctype= "Multipart/form-data" string, indicating that the user input data on the file upload, and method must use POST and can not use get.

In the above code, if the user's name is filled in the Wilson Peng, and select the C:\myphoto.gif file, the user presses the send out key, the browser will send the following post data.


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 content slightly ...
--bbc04y--
--aab03x--


In the above, boundary=aab03x is a message that separates different field data, where the aab03x encoding method varies depending on the browser's version, usually generated by a browser hash. Then you can
See Using--aab03x to separate different fields.

For example, the action program to handle the form next.php, will actively generate four variables, see the following table

Description of variable name
$myfile the contents of the file uploaded
$myfile _name The name of the uploaded file on the user side
The size of the file uploaded $myfile _size
$myfile _type upload file format, such as "Image/gif"


In the next.php program to do the most important action is to use these four variables, or the end of the program, the user uploaded files disappeared. Therefore, to first copy the $myfile to the directory where the advertisement map is stored

Copy ($banner, "/home1/biglobe3/ad/". $banner _name);

This line of procedure is to put the file in the/home/htdocs/ad directory, in the above example, to save the file to/home/htdocs/ad/myphoto.gif. It is important that the stored directory cannot be a directory that WebServer cannot read, but should be placed in the homepage directory of the site before you can see it on the Internet.

Perhaps the program needs to be handled more detail, such as whether the file size is the same as the system return ..., and so on, you can use $myfile _size variables.

If you set the name of the input file in the form to change, the variables in the Upload are also changed together, as

<input name= "upfile" type= "File" >

The variables are changed into $upfile, $upfile _name, $upfile _size, and $upfile _type.

As a result, the following example uses the file Upload and Oracle 7.x back-end repositories to place files in the Web homepage directory and related information in Oracle. Of course, plus user authentication, so that users can upload the account number of images, can avoid executioner (cracker), such as indecent or inappropriate advertising upload. For example, the settings for the database are the same as the 5.4 message version.


<?php
adadd.php
if ($banner = = "") and ($url = = "")) {
?>
<title> New Ads </title>
<body>
The larger the number of weighted values, the higher the probability of the picture appearing, the default value is 1.
<form enctype= "Multipart/form-data" action= "adadd.php"
Method=post>
<table border=0>
&LT;TR&GT;&LT;TD align=right> advertising Banner: </td><td><input Name=banner
Type= "File" ></td></tr>
&LT;TR&GT;&LT;TD align=right> Advertising URL: </td><td><input name=url
Type=text size=30></td></tr>
&LT;TR&GT;&LT;TD align=right> Auxiliary string ALT: </td><td><input name=alt
Type=text size=30></td></tr>
&LT;TR&GT;&LT;TD align=right> Advertisement Description: </td><td><input name=descript
Type=text size=30></td></tr>
&LT;TR&GT;&LT;TD align=right> display weighting: </td><td><input name=priority
Type=text size=5 value=1></td></tr>
&LT;TR&GT;&LT;TD colspan=2 align=right><input type= "Submit" value= "OK
"></td></tr>
</table>
</FORM>
?
} else {
if (file_exists ("/home/htdocs/ad/". $banner _name)) {
Commonheader ("Archives", $banner _name. "Existing");
echo "<p><br><br> advertising file already exists
\n<p><br><br></body>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 "<title> Advertising new completion </title>";
echo "echo "<body>";
echo "<a href=". $url. " >Alt=\ "". $alt. " \ "Border=0></a><p>";
echo "<ul type=disc>";
echo "<li> advertising URL:". $url;
echo "<li> auxiliary string:". $alt;
echo "<li> advertisement Description:". $descript;
echo "<li> Display weighted:". $priority;
echo "</ul>";
}

?>
</body>


Of course, before using the above program, don't forget to add the ad table, SQL and fields as follows


CREATE TABLE AD (
URL varchar2 (1024) NOT NULL,
Banner VARCHAR2 (1024) NOT NULL,
Alt VARCHAR2 (255) NULL,
Descript VARCHAR2 (255) NULL,
Priority number (4) NOT NULL default 1
);

Ordinal field name Data shape Data Length field description
0 Advertising URL varchar2 1024
1 Picture path Banner VARCHAR2 1024
2 string display alt VARCHAR2 255
3 Advertising Description Descript VARCHAR2 255
4 Display weighted priority number 4 1 as the default, 0 table deactivate


It is worth mentioning that the weighted function is added, if an advertisement is to increase the exposure rate, you can display
The weighted field number increases, for example 5, and it is five times times more likely to appear than just 1.


<?php
ad.php
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 = "Select URL, banner, ALT, priority from AD where priority
> 0 ";
Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);
$i = $pricount = 0;
while (Ora_fetch ($cursor)) {
$ad [$i][0] = ora_getcolumn ($cursor, 0);
$ad [$i][1] = Ora_getcolumn ($cursor, 1);
$ad [$i][2] = Ora_getcolumn ($cursor, 2);
$ad [$i][3] = Ora_getcolumn ($cursor, 3);
$pricount + + $ad [$i][3];
$i + +;
};

Ora_close ($cursor);
Ora_logoff ($handle);

Srand (Double) microtime () *1000000);
$pri = rand (1, $pricount);
$pricount = 0;
for ($i =0; $i <count ($ad); $i + +) {
$pricount + + $ad [$i][3];
if ($pri <= $pricount) {
$ad 1[]= "<a href=". $ad [$i][0]. "Target=new>src=/ad/". $ad [$i][1]." width=468 height=60 border=0
Alt=\ "". $ad [$i][2]. " \ "></a>";
}
}
echo $ad 1[0];

?>


The above program is the public advertisement display program, in which the $pricount variables for all advertising priority add up and. The program first reads all the advertising information into the array variable $ad and closes the database. Then the random number of seeds in time, and then from 1 to $pricount randomly take a number.

The Web page to use the advertising program, as long as the need to advertise the place plus; Include ("ad.php");?> is OK, of course the path to include (in httpd.conf) must be set first.

The above program also has the improvement space, may add the Advertisement Click log function, or displays the log function, changes displays the weighted program .... Wait, no paradigm, after all, here is to introduce the actual application of PHP and program development, rather than suite development. Really need off-the-shelf advertising kits, as well as to Http://www.phpwizard.net/phpAds, this is a set of PHP developed with the advertising program. (from the CCU newsgroup, the original source may be a book on PHP published in Taiwan: PHP treasure)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.