<?php
//include ("admin.add.php");
include ("../include/fckeditor/fckeditor.php");
//$typeid = $_POST["typeid"];
//echo "当前删除的ID:".$typeid;//上传文件类型列表
 
$uptypes=array(
    'image/jpg', 
    'image/jpeg',
    'image/png',
    'image/pjpeg',
    'image/gif',
    'image/bmp',
    'image/x-png'
);
 
 
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
$destination_folder="uploadimg/"; //上传文件路径
$water=1;      //是否附加水印(1为加水印,其他为不加水印);
$watertype=1;      //水印类型(1为文字,2为图片)
$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="http://www.xplore.cn/";  //水印字符串
$waterimg="xplore.gif";    //水印图片
$imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;    //缩略图比例
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title>
<link href="images/css.css" rel="stylesheet" type="text/css">
<script src="../include/js/jquery.js" type="text/javascript"></script>
</head><body>
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
  <tr>
    <td valign="top" style="padding:10px;"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table_head">
        <tr>
          <td height="30">要闻添加 
            &nbsp;&nbsp;&nbsp;</td>
         
        </tr>
      </table>
<form action="ywsarticle.do.php?action=addyws" enctype="multipart/form-data" method="post" name="upform" id="upform">
<table width="769" border="0">
  <tr>
    <td width="142"><div align="right">要闻标题:</div></td>
    <td height="72" colspan="2">
        <input id="yw_title" type="text" name="yw_title" />  </td>
    </tr>
  <tr>
    <td><div align="right">要闻图片:</div></td>
    <td height="58" colspan="2"><input name="yw_pic" id="yw_pic" type="file" />
      <label>
  
      </label></td>
    </tr>
  <tr>
    <td><div align="right">要闻内容:</div></td>
    <td height="156" colspan="2">      <?php
        $oFCKeditor = new FCKeditor ( 'yw_content' );
        $oFCKeditor->BasePath = "../include/fckeditor/";
        $oFCKeditor->ToolbarSet = 'MyToolbar';
        $oFCKeditor->Value = $article ['yw_content'];
        $oFCKeditor->Height = 350;
        $oFCKeditor->Create ();
    ?> </td>
    </tr>
  <tr>
    <td align="right">&nbsp;</td>
    <td width="68" height="39" align="right"><input type="submit" name="addnewsform" value="提交" />        </td>
    <td width="545"><input type="reset" name="Submit2" value="重置" /></td>
  </tr>
</table>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')

    if (!is_uploaded_file($_FILES["yw_pic"][tmp_name]))
    //是否存在文件
    { 
         echo "图片不存在!";
         exit;
    }
    $file = $_FILES["yw_pic"];
 
    if($max_file_size < $file["size"])
    //检查文件大小
    { 
        echo "文件太大!";
        exit;
    }
 
    if(!in_array($file["type"], $uptypes))
    //检查文件类型
    { 
        echo "文件类型不符!".$file["type"];
        exit;
    }
 
    if(!file_exists($destination_folder))
    { 
        mkdir($destination_folder);
    }
 
    $filename=$file["tmp_name"];
    $image_size = getimagesize($filename);
    $pinfo=pathinfo($file["name"]);
    $ftype=$pinfo['extension'];
    $destination = $destination_folder.time().".".$ftype;
 
    if (file_exists($destination) && $overwrite != true)
    { 
        echo "同名文件已经存在了";
        exit;
    }
 
    if(!move_uploaded_file ($filename, $destination))
    { 
        echo "移动文件出错";
        exit;
    }
 
    $pinfo=pathinfo($destination);
    $fname=$pinfo[basename];
 
    echo " < font color=red>已经成功上传</font><br>文件名:  <font color= blue>".$destination_folder.$fname."</font><br>";
    echo " 宽度:".$image_size[0];
    echo " 长度:".$image_size[1];
    echo "<br> 大小:".$file["size"]." bytes";
 
    if($water==1)
    { 
        $iinfo=getimagesize($destination,$iinfo);
        $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
        $white=imagecolorallocate($nimage,255,255,255);
        $black=imagecolorallocate($nimage,0,0,0);
        $red=imagecolorallocate($nimage,255,0,0);
        imagefill($nimage,0,0,$white);
        switch ($iinfo[2])
        { 
            case 1:
            $simage =imagecreatefromgif($destination);
            break;
 
            case 2:
            $simage =imagecreatefromjpeg($destination);
            break;
 
            case 3:
            $simage =imagecreatefrompng($destination);
            break;
 
            case 6:
            $simage =imagecreatefromwbmp($destination);
            break;
 
            default:
            die("不支持的文件类型");
            exit;
         }
 
        imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
        imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
 
        switch($watertype)
        { 
            case 1:   //加水印字符串
            imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
            break;
 
            case 2:   //加水印图片
            $simage1 =imagecreatefromgif("xplore.gif");
            imagecopy($nimage,$simage1,0,0,0,0,85,15);
            imagedestroy($simage1);
            break;
         }
 
        switch ($iinfo[2])
        { 
            case 1:
            //imagegif($nimage, $destination);
            imagejpeg($nimage, $destination);
            break;
 
            case 2:
            imagejpeg($nimage, $destination);
            break;
 
            case 3:
            imagepng($nimage, $destination);
            break;
 
            case 6:
            imagewbmp($nimage, $destination);
            //imagejpeg($nimage, $destination);
            break;
         }
 
        //覆盖原上传文件
        imagedestroy($nimage);
        imagedestroy($simage);
    }
 
    if($imgpreview==1)
    { 
        echo "<br>图片预览:<br>";
        echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
        echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";
    }
 
}
 
?>
</body>
</html>
添加失败,是图片上传的问题 希望各位大大能帮帮小弟解决,

解决方案 »

  1.   

    给你一个代码你看一下,<?php 
    header("Content-type:text/html;Charset=utf-8");
    $uploaddir = dirname(__FILE__)."\\upload\\"; //dirname获得当前文件的路径
    $uploadname =time().basename($_FILES['upfile']['name']);
    $uploadfile = $uploaddir.$uploadname;
    $type= $_FILES['upfile']['type'];
    //echo preg_match("/\\.(jpg|gif|png)$/i",$uploadfile);
    if($_FILES["upfile"]["error"]>0){
    echo "error files";
    }else
    if (getimagesize($_FILES['upfile']['tmp_name'])){ 
    //if(strcmp($type,"image/bmp") == 0){
        if($_FILES["upfile"]["size"]<1000000){
      if(is_uploaded_file($_FILES['upfile']['tmp_name'])){
       if(move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile)){
                         echo "<script language='javascript'>document.URL='manager/message.php?id=$uploadname';</script>";
      }
    else{
      echo "上传失败";
      }
          }
        }
        else{
         echo "<script language='javascript'>document.URL='manager/message.php?id=0';</script>";
        }
    }
    else{
    echo "<script language='javascript'>document.URL='manager/message.php?id=1';</script>";
    }
    ?> 
      

  2.   

    把这句is_uploaded_file($_FILES["yw_pic"][tmp_name]
    改成is_uploaded_file($_FILES['yw_pic']['tmp_name']

      

  3.   


    上面注释不是很清楚吗?
    $destination_folder="uploadimg/"; //上传文件路径
     if (!is_uploaded_file($_FILES["yw_pic"][tmp_name]))
    改成 if (!is_uploaded_file($_FILES[yw_pic][tmp_name]))上传后的图片 在 $destination = $destination_folder.time().".".$ftype;
    也 就是uploadimg/当前时间.图片文件后缀名 
    这样写有缺陷 当相同时间上传就会有重名文件 改成
    $str=time().rand(4,4);
    $destination = $destination_folder.md5($str).".".$ftype;
    哥们,真怕丢了工作好好看看PHP函数手册