php图片文件上传详细分析
upload_err_ok no error occurred.
上传成功
upload_err_ini_size the uploaded file exceeds the maximum value specified in the php教程.ini file.
超出最大上传尺寸
upload_err_form_size the uploaded file exceeds the maximum value specified by the max_file_size hidden widget.
超出form设置最大上传尺寸
upload_err_partial the file upload was canceled and only part of the file was uploaded.
upload_err_nofile no file was uploaded.
未上传文件
<html>
<head>
<title>a simple file upload form</title>
</head>
<body>
<form enctype="multipart/form-data"
action="<?print $_server['php_self']?>" method="post">
<p>
<input type="hidden" name="max_file_size" value="102400" />
<input type="file" name="fupload" /><br/>
<input type="submit" value="upload!" />
</p>
</form>
</body>
</html>
实例一
]<html>
<head>
<title>a file upload script</title>
</head>
<body>
<div>
<?php
if ( isset( $_files['fupload'] ) ) {print "name: ". $_files['fupload']['name'] ."<br />";
print "size: ". $_files['fupload']['size'] ." bytes<br />";
print "temp name: ".$_files['fupload']['tmp_name'] ."<br />";
print "type: ". $_files['fupload']['type'] ."<br />";
print "error: ". $_files['fupload']['error'] ."<br />";if ( $_files['fupload']['type'] == "image/gif" ) {
$source = $_files['fupload']['tmp_name'];
$target = "upload/".$_files['fupload']['name'];
move_uploaded_file( $source, $target );// or die ("couldn't copy");
$size = getimagesize( $target );$imgstr = "<p><img width="$size[0]" height="$size[1]" ";
$imgstr .= "src="$target" alt="uploaded image" /></p>";print $imgstr;
}
}
?>
</div>
<form enctype="multipart/form-data"
action="<?php print $_server['php_self']?>" method="post">
<p>
<input type="hidden" name="max_file_size" value="102400" />
<input type="file" name="fupload" /><br/>
<input type="submit" value="upload!" />
</p>
</form>
</body>
</html>
文件上传实例二
<?php
$maxsize=28480;
if (!$http_post_vars['submit']) {
$error=" ";
}
if (!is_uploaded_file($http_post_files['upload_file']['tmp_name']) and !isset($error)) {
$error = "<b>you must upload a file!</b><br /><br />";
unlink($http_post_files['upload_file']['tmp_name']);
}
if ($http_post_files['upload_file']['size'] > $maxsize and !isset($error)) {
$error = "<b>error, file must be less than $maxsize bytes.</b><br /><br />";
unlink($http_post_files['upload_file']['tmp_name']);
}
if (!isset($error)) {
move_uploaded_file($http_post_files['upload_file']['tmp_name'],
"uploads/".$http_post_files['upload_file']['name']);
print "thank you for your upload.";
exit;
}
else
{
echo ("$error");
}
?><html>
<head></head>
<body>
<form action="<?php echo(htmlspecialchars($_server['php_self']))?>"
method="post" enctype="multipart/form-data">
choose a file to upload:<br />
<input type="file" name="upload_file" size="80">
<br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
php教程利用cookie自动登录方法
<html>
<head>
<title>enter password</title>
</head>
<body>
<form name="forml" method="post" action="cookiebasedpasswordlogin.php">
<table>
<tr>
<td colspan="2" >
<div align="center"><b>please specify the password</b></div>
</td>
</tr>
<tr>>
<td>
<div align="right">customer id</div>
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
<div align="right">password</div>
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="submit" name="submit" value="login">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>
<!-- cookiebasedpasswordlogin.php
<?php
$now = getdate();
$storetime= $now["weekday"] . " " . $now["month"] ." " . $now["year"] ;
$storetime.=" time : ";if ($now["hours"] < 10) {
$storetime.= "0" . $now["hours"];
} else {
$storetime.= $now["hours"];
}
$storetime.= ":";
if ($now["minutes"]<10) {
$storetime.= "0" . $now["minutes"];
} else {
$storetime.= $now["minutes"];
}
$storetime.= ": ";
if ($now["seconds"] <10) {
$storetime.= "0" . $now["seconds"];
} else {
$storetime.= $now["seconds"];
}
if (isset($data)) {
$counter=++$data[l];
setcookie("data[0]",$storetime,time() + (60*60*24));
setcookie("data[l]", $counter,time() + (60*60*24)); setcookie("data[2]",$username,time() + (60*60*24));
echo "<b><center>hi " . $data[2] . " ! !</center></b><br>n";
echo "<b><center>last login time :" .$data[0] . "</center></b><br>n";
echo "<b><center>current date :" .$storetime. "</center></b><br>n";
echo "<b><center>page view count :" . $data[l]. "</center></b><br>n";
echo "<b><center>you have successfully logged in!</center></b>";
echo ("<b><contor>you can access this area without entering a password for the next 24 hours!</center></b>");
} else {
if (isset($username) && isset($password)) {
if ($password=="superpass") {
$counter=0;
setcookie("data[0]",$storetime,time() + (60*60*24));
setcookie("data[l]",$counter,time() + (60*60*24));
setcookie("data[2]",$username,time() + (60*60*24));
$url="location: cookieimp.php";
header($url);
}else{
echo "<hl><center>invalid password!!!</center></hl>";
}
}
}
?>
php教程5中,变量的类型是不确定的,一个变量可以指向任何类型的数值、字符串、对象、资源等。我们无法说php5中多态的是变量。
我们只能说在php5中,多态应用在方法参数的类型提示位置。
一个类的任何子类对象都可以满足以当前类型作为类型提示的类型要求。所有实现这个接口的类,都可以满足以接口类型作为类型提示的方法参数要求。简单的说,一个类拥有其父类、和已实现接口的身份
通过实现接口实现多态
下面的例子中,useradmin类的静态方法,要求一个user类型的参数。
在后面的使用中,传递了一个实现了user接口的类normaluser的实例。代码成功运行。
<?
interface user{ // user接口
public function getname();
public function setname($_name);
}class normaluser implements user { // 实现接口的类.
private $name;
public function getname(){
return $this->name;
}
public function setname($_name){
$this->name = $_name;
}
}class useradmin{ //操作.
public static function changeusername(user $_user,$_username){
$_user->setname($_username);
}
}$normaluser = new normaluser();
useradmin::changeusername($normaluser,"tom");//这里传入的是 normaluser的实例.
echo $normaluser->getname();
?>
php 接口类:interface
其实他们的作用很简单,当有很多人一起开发一个项目时,可能都会去调用别人写的一些类,那你就会问,我怎么知道他的某个功能的实现方法是怎么命名的呢,这个时候php接口类就起到作用了,当我们定义了一个接口类时,它里面的方式是下面的子类必须实现的,比如 :
代码如下:
interface shop
{
public function buy($gid);
public function sell($gid);
public function view($gid);
}
我声明一个shop接口类,定义了三个方法:买(buy),卖(sell),看(view),那么继承此类的所有子类都必须实现这3个方法少一个都不行,如果子类没有实现这些话,就无法运行。实际上接口类说白了,就是一个类的模板,一个类的规定,如果你属于这类,你就必须遵循我的规定,少一个都不行,但是具体你怎么去做,我不管,那是你的事,如:
代码如下:
class baseshop implements shop
{
public function buy($gid)
{
echo('你购买了id为 :'.$gid.'的商品');
}
public function sell($gid)
{
echo('你卖了id为 :'.$gid.'的商品');
}
public function view($gid)
{
echo('你查看了id为 :'.$gid.'的商品');
}
}
下面缩一下方法
<?php
interface myusbkou
{
function type();//类型
function action();//执行的操作
}
class zip implements myusbkou
{ //继承接口
function type()
{
echo "usb的2.0接口";
}
function action()
{
echo "--->需要usb 2.0驱动";
}
}
class mp3 implements myusbkou
{
function type()
{
echo "mp3的1.0接口";
}
function action()
{
echo "--->需要mp3 1.0驱动<br/>";
}
}
class mypc
{
function usbthing($thing)
{
$thing->type();
$thing->action();
}
}
$p=new mypc();
$mp3=new mp3();
$zip=new zip();
$p->usbthing($mp3);
$p->usbthing($zip);
?>
如果你看到的话,那么你需要设置你的php教程并开启这个库。如果你是在windows平台下,那么非常简单,你需要改一改你的php.ini文件的设置,找到php_curl.dll,并取消前面的分号注释就行了。如下所示:
//取消下在的注释
extension=php_curl.dll
如果你是在linux下面,那么,google排名你需要重新编译你的php了,编辑时,你需要打开编译参数——在configure命令上加上“–with-curl” 参数。
一个小示例
如果一切就绪,下面是一个小例程:
复制代码 代码如下:
<?php
// 初始化一个 curl 对象
$curl = curl_init();
// 设置你需要抓取的url
curl_setopt($curl, curlopt_url, 'http://111cn.net');
// 设置header
curl_setopt($curl, curlopt_header, 1);
// 设置curl 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, curlopt_returntransfer, 1);
// 运行curl,请求网页
$data = curl_exec($curl);
// 关闭url请求
curl_close($curl);
// 显示获得的数据
var_dump($data);
如何post数据
上面是抓取网页的代码,下面则是向某个网页post数据。假设我们有一个处理表单的网址http://www.example.com/sendsms.php,其可以接受两个表单域,一个是电话号码,一个是短信内容。
复制代码 代码如下:
<?php
$phonenumber = '13912345678';
$message = 'this message was generated by curl and php';
$curlpost = 'pnumber=' . urlencode($phonenumber) . '&message=' . urlencode($message) . '&submit=send';
$ch = curl_init();chain link fencing
curl_setopt($ch, curlopt_url, 'http://www.example.com/sendsms.php');
curl_setopt($ch, curlopt_header, 1);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $curlpost);
$data = curl_exec();
curl_close($ch);
?>
从上面的程序我们可以看到,使用curlopt_post设置http协议的post方法,而不是get方法,然后以curlopt_postfields设置post的数据。
关于代理服务器
下面是一个如何使用代理服务器的示例。请注意其中高亮的代码,代码很简单,我就不用多说了。
复制代码 代码如下:
<?php
$ch = curl_init();
curl_setopt($ch, curlopt_url, 'http://www.example.com');
curl_setopt($ch, curlopt_header, 1);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_httpproxytunnel, 1);
curl_setopt($ch, curlopt_proxy, 'fakeproxy.com:1080');
curl_setopt($ch, curlopt_proxyuserpwd, 'user:password');
$data = curl_exec();
curl_close($ch);
?>
关于ssl和cookie
关于ssl也就是https教程协议,煤气发生炉你只需要把curlopt_url连接中的http://变成https://就可以了。当然,还有一个参数叫curlopt_ssl_verifyhost可以设置为验证站点。
关于cookie,你需要了解下面三个参数:
curlopt_cookie,在当面的会话中设置一个cookie
curlopt_cookiejar,当会话结束的时候保存一个cookie
curlopt_cookiefile,cookie的文件。
http服务器认证
最后,我们来看一看http服务器认证的情况。
复制代码 代码如下:
<?php
$ch = curl_init();
curl_setopt($ch, curlopt_url, 'http://www.example.com');
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_httpauth, curlauth_basic);
curl_setopt(curlopt_userpwd, '[username]:[password]')
$data = curl_exec();
curl_close($ch);
?>
看一个利用curl抓取163邮箱地址列表代码
curl技术说白了就是模拟浏览器的动作实现页面抓取或表单提交,通过此技术可以实现许多有去的功能。
复制代码 代码如下:
<?php
error_reporting(0);
//邮箱用户名(不带@163.com后缀的)
$user = 'papatata_test';
//邮箱密码
$pass = '000000';
//目标邮箱
//$mail_addr = uenucom@163.com';
//登陆
$url = 'http://reg.163.com/logins.jsp教程?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3d1%26verifycookie%3d1%26language%3d-1%26style%3d-1';
$ch = curl_init($url);
//创建一个用于存放cookie信息的临时文件
$cookie = tempnam('.','~');
$referer_login = 'http://mail.163.com';
//返回结果存放在变量中,而不是默认的直接输出
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_header, true);
curl_setopt($ch, curlopt_connecttimeout, 120);
curl_setopt($ch, curlopt_post, true);
curl_setopt($ch, curlopt_referer, $referer_login);
$fields_post = array(
'username'=> $user,
'password'=> $pass,
'verifycookie'=>1,
'style'=>-1,
'product'=> 'mail163',
'seltype'=>-1,
'secure'=>'on'
);
$headers_login = array(
'user-agent' => 'mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9) gecko/2008052906 firefox/3.0',
'referer' => 'http://www.163.com'
);
$fields_string = '';
foreach($fields_post as $key => $value)
{
$fields_string .= $key . '=' . $value . '&';
}
$fields_string = rtrim($fields_string , '&');
curl_setopt($ch, curlopt_cookiesession, true);
//关闭连接时,将服务器端返回的cookie保存在以下文件中
curl_setopt($ch, curlopt_cookiejar, $cookie);
curl_setopt($ch, curlopt_httpheader, $headers_login);
curl_setopt($ch, curlopt_post, count($fields));
curl_setopt($ch, curlopt_postfields, $fields_string);
$result= curl_exec($ch);
curl_close($ch);
//跳转
$url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=loki_wuxi';
$ch = curl_init($url);
$headers = array(
'user-agent' => 'mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9) gecko/2008052906 firefox/3.0'
);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_header, true);
curl_setopt($ch, curlopt_connecttimeout, 120);
curl_setopt($ch, curlopt_post, true);
curl_setopt($ch, curlopt_httpheader, $headers);
//将之前保存的cookie信息,一起发送到服务器端
curl_setopt($ch, curlopt_cookiefile, $cookie);
curl_setopt($ch, curlopt_cookiejar, $cookie);
$result = curl_exec($ch);
curl_close($ch);
//取得sid
preg_match('/sid=[^"].*/', $result, $location);
$sid = substr($location[0], 4, -1);
//file_put_contents('./result.txt', $sid);
//通讯录地址
$url='http://g4a30.mail.163.com/jy3/address/addrlist.jsp?sid='.$sid.'&gid=all';
$ch = curl_init($url);
$headers = array(
'user-agent' => 'mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9) gecko/2008052906 firefox/3.0'
);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_header, true);
curl_setopt($ch, curlopt_connecttimeout, 120);
curl_setopt($ch, curlopt_post, true);
curl_setopt($ch, curlopt_httpheader, $headers);
curl_setopt($ch, curlopt_cookiefile, $cookie);
curl_setopt($ch, curlopt_cookiejar, $cookie);
$result = curl_exec($ch);
curl_close($ch);
//file_put_contents('./result.txt', $result);
unlink($cookie);
//开始抓取内容
preg_match_all('/<td class="ibx_td_addrname"><a[^>]*>(.*?)</a></td><td class="ibx_td_addremail"><a[^>]*>(.*?)</a></td>/i', $result,$infos,preg_set_order);
//1:姓名2:邮箱
print_r($infos);
?>
php教程读取flash文件高宽帧数背景颜色代码
<?php
/*
示例:
$file = '/data/ad_files/5/5.swf';
$flash = new flash();
$flash = $flash->getswfinfo($file);
echo "
文件的宽高是:".$flash["width"].":".$info["height"];
echo "
文件版本是".$flash["version"];
echo "
文件帧数量是".$flash["framecount"];
echo "
文件帧速率是".$flash["framerate"];
echo "
文件背景颜色是".$flash["bgcolor"];
*/
class flash
{
//是否返回背景色
public $need_back_color = false ;
//是否返回版本
public $need_version = false ;
//是否返回帧速率
public $need_framerate = false ;
//是否返回帧数量
public $need_framecount = false ;public function __construct()
{}
public function getswfinfo( $filename )
{
if ( file_exists($filename) ) {
//echo "文件的修改时间:".date("m d y h:i:s.", filemtime($filename))."
";
} else {
//echo "目标文件不存在!";
return array( "error" => $filename ) ;
}//打开文件
$rs = fopen($filename,"r");
//读取文件的数据
$str = fread( $rs , filesize( $filename ) ) ;
///
if($str[0] == "f")
{
//echo "
文件已是解压缩的文件:";
} else {
$first = substr($str,0,8);
$last = substr($str,8);
//
$last = gzuncompress($last);
//
$str = $first . $last ;
$str[0] = "f";
//echo "
解压缩后的文件信息:";
}$info = $this->getinfo( $str );
fclose ( $rs ) ;
return $info;
}private function mydecbin($str,$index)
{
$fbin = decbin(ord($str[$index]));
while(strlen($fbin)<8)$fbin="0".$fbin;
return $fbin;
}private function colorhex($data)
{
$tmp = dechex($data);
if ( strlen($tmp)<2 ) {
$tmp='0' . $tmp ;
}
return $tmp;
}private function getinfo( $str )
{
//换算成二进制
$fbin = $this->mydecbin( $str , 8 ) ;
//计算rec的单位长度
$slen = bindec( substr( $fbin , 0 , 5 ) );
//计算rec所在的字节
$recsize = $slen * 4 + 5 ;
$recsize = ceil( $recsize / 8 ) ;//rec的二进制
$recbin = $fbin ;
for( $i = 9 ; $i < $recsize + 8 ; $i++ )
{
$recbin .= $this->mydecbin( $str ,$i );
}//rec数据
$rec = array();
for( $i = 0 ; $i < 4 ; $i++ )
{
$rec[] = bindec( substr( $recbin , 5 + $i * $slen , $slen ) ) / 20 ;
}
if ( $this->need_back_color ) {
//背景颜色
for( $i = $recsize + 12 ; $i < strlen ( $str ) ; $i ++ )
{
if ( ord( $str[$i] ) == 67 && ord( $str[$i+1] ) == 2 )
{
$bgcolor = $this->colorhex(ord($str[$i+2])).$this->colorhex(ord($str[$i+3])).$this->colorhex(ord($str[$i+4]));
break;
}
}
}
if ( $this->need_version ) {
//版本
$version = ord( $str[3] );
}
if ( $this->need_framerate ) {
//帧速率
$framerate = ord( $str[$recsize + 8] ) / 256 + ord( $str[$recsize + 9] ) ;
}if ( $this->need_framecount ) {
//帧数量
$framecount = ord( $str[$recsize + 11] ) * 256 + ord( $str[$recsize + 10] );
}
return array ( "bgcolor" => $bgcolor ,
"version" => $version ,
"framerate" => $framerate ,
"framecount" => $framecount ,
'width'=>$rec[1],
'height'=>$rec[3]
);
}
}?>
相关文章
- 下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19Jupyter Notebook读取csv文件出现的问题及解决
这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- 有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
- 这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
php无刷新利用iframe实现页面无刷新上传文件(1/2)
利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25- 本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
- 这篇文章主要介绍了C#使用StreamWriter写入文件的方法,涉及C#中StreamWriter类操作文件的相关技巧,需要的朋友可以参考下...2020-06-25
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25