PHP代码修改
我的代码能运行、可就是页面显示很多错误。该上传的还是能上传。该水印的还是能水印。就是打开的时候有显示些错误。看起来不美观。还有上传完后,他出现HTML代码。我想上传完后显示上传成功。帮忙修改下。谢谢
<?php
if (is_uploaded_file($_FILES['upfile']['tmp_name'])){
$upfile=$_FILES["upfile"];
$name = $upfile["name"];
$type = $upfile["type"];
$size = $upfile["size"];
$tmp_name = $upfile["tmp_name"];
$error = $upfile["error"];
switch ($type) {
case 'image/pjpeg' : $ok=1;
break;
case 'image/jpeg' : $ok=1;
break;
case 'image/gif' : $ok=1;
break;
case 'image/png' : $ok=1;
break;
}
}
$img=getimagesize($tmp_name);
switch($img[2])
{
case 1:
$img=@imagecreatefromGIF($tmp_name);
break;
case 2:
$img=@imagecreatefromJPEG($tmp_name);
break;
case 3:
$img=@imagecreatefromPNG($tmp_name);
break;
}
$te=@imagecolorallocate($img,255,255,255);
$str=iconv("GBK","UTF-8","我会登上世界的巅峰,成为你们仰视的存在");
imagettftext($img,12,0,20,20,$te,'text.ttf',$str);
header("content-type: image/jpeg");
imagejpeg($img,"11.jpg");
?>
<form action="" enctype="multipart/form-data" method="post" name="upform">
上传文件:
<input name="upfile" type="file">
<input type="submit" value="上传"><br>
</form>
追问:我看了、可没有加到水印啊.能不能再看下怎么回事
答案:请确保你有“text.ttf”这个文件,你的程序用到了这个文件,不然还有错误。
下面是我改好的代码:
<?php if(!isset($_FILES['upfile'])): ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>wnh3的改造</title>
</head>
<body>
<form action="" enctype="multipart/form-data" method="post" name="upform">
上传文件:
<input name="upfile" type="file">
<input type="submit" value="上传"><br>
</form>
</body>
</html>
<?php else:
if (is_uploaded_file($_FILES['upfile']['tmp_name'])){
$upfile=$_FILES["upfile"];
$name = $upfile["name"];
$type = $upfile["type"];
$size = $upfile["size"];
$tmp_name = $upfile["tmp_name"];
$error = $upfile["error"];
switch ($type) {
case 'image/pjpeg' :
$ok=1;
break;
case 'image/jpeg' :
$ok=1;
break;
case 'image/gif' :
$ok=1;
break;
case 'image/png' :
$ok=1;
break;
}
}
$img=getimagesize($tmp_name);
switch($img[2]){
case 1:
$img=@imagecreatefromGIF($tmp_name);
break;
case 2:
$img=@imagecreatefromJPEG($tmp_name);
break;
case 3:
$img=@imagecreatefromPNG($tmp_name);
break;
}
$te=@imagecolorallocate($img,255,255,255);
$str=iconv("GBK","UTF-8","我会登上世界的巅峰,成为你们仰视的存在");
imagettftext($img,12,0,20,20,$te,'text.ttf',$str);
// header("content-type: image/jpeg");
imagejpeg($img,"11.jpg");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>wnh3的改造</title>
</head>
<body>
<h1> 上传成功 </h1>
</body>
</html>
<?php endif; ?>
其他:我也在学,等我学会了再回答你吧 不想麻烦就在代码的第一行加上:
error_reporting(0);
或
ini_set('display_errors','Off');
不怕麻烦就规范一下代码:
如这行:
if (is_uploaded_file($_FILES['upfile']['tmp_name']))
改为
if (isset($_FILES['upfile']) && is_uploaded_file($_FILES['upfile']['tmp_name']))
其它的你就看着来改了。建议你这样,要有规范代码的习惯,以免以后再次出现同样的问题
上一个:iis服务器 php伪静态后不能获取$_get['id']的问题
下一个:我想学PHP,请问应该用哪些编程工具的?