自己从网上搜了些代码整理的,大部分时候是可以的,但是有个别图片总是上传不了,也没有任何提示。(图片格式和大小都符合要求,也不出任何的提示文字),哪位能帮我看看问题出在哪里?非常感谢!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>上传图片</title>
</head><body><form action="" enctype="multipart/form-data" method="post" name="uploadfile" style="margin:0px;">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><input name="upfile" type="file" size="8" />
        <input type="submit" value="上传" />
        <br />
<?php
@header('Content-type: text/html;charset=UTF-8');
if (isset($_FILES['upfile']['size'])){ 
 
if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ 
$upfile=$_FILES["upfile"];  //获表单数据 
$imgname=$upfile["name"];//上传文件的文件名
$imgtype=$upfile["type"];//上传文件的类型 
$imgsize=$upfile["size"];//上传文件的大小
$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径  //验证(格式、大小)

if ($imgtype=='image/pjpeg' || $imgtype=='image/jpeg' || $imgtype=='image/jpg' || $imgtype=='image/gif' || $imgtype=='image/png'   ){
if ($imgsize<=2000000){
//-------------------------------------
$info = pathinfo($imgname);//取到文件后缀
$newname=date('YmdHis').rand(1000,9999).'.'.$info['extension'];//日期+随机数+后缀构成新名$newname
move_uploaded_file($tmp_name,'comimage/'.$newname); //执行上传操作


if(file_exists('comimage/'.$newname)){
unlink('comimage/'.$newname);//删除原图
}
//-------------------------------------
}else{
echo '上传失败,文件太大!不能超过2M';
echo '<script>window.location="addimg2.php"</script>';
}


}else{
echo '上传失败,格式错误。只支持JPG\GIF\PNG格式的图片!';
}
//删除功能
echo '<a href="imgdel.php?imgname='.$newname.'">删除</a>';

} }
?>        </td>
    </tr>
  </table>
</form> </body>
</html>

解决方案 »

  1.   

    检查应从 $_FILES['upfile']['error'],$_FILES['upfile']['error']不为 0 时就有错,你可查阅手册形成代码
      

  2.   

    检查是不是超过php.ini 中设置的最大上传大小了
      

  3.   

    检查一下php.ini , upload_max_filesize 
      

  4.   

    调试错误可以先打开display_errors,或者在可疑的地方逐行echo
    掌握调试方法很重要,发现问题其实已经算解决了大半了
      

  5.   

    上传成功的时候echo下上传成功 判断下你是成功了还是没成功,好调试
      

  6.   

    调试模式打开 error_reporting(7);  把file 输出  然后看有什么信息
      

  7.   

    检查一下php.ini , upload_max_filesize