主页面a.php,点击其中按钮后调取up.php页面,上传图片后自动关闭,并将路径信息返回给a.php上的输入框。单独调测up.php,能够上传文件并自动关闭。但是被a.php调取后,只能获取路径信息、自动关闭。但不能正确上传文件,请给与帮助,谢谢!
a.php代码如下:
<html>
<head >
<title></title>
<script type="text/javascript">
function gg(openwindow, aa, bb) {
var str = window.showModalDialog('up.php', window, "dialogWidth=500px;dialogHeight=70px;center=yes;help=no;resizable=no;status=no");
if (!str)
return;
document.getElementById(aa).value = str[0];
document.getElementById(bb).value = str[1];
}
</script>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="form1">
<div id="fc" style="width:160px;font-size:12px;"><ul></ul></div>
<div>
<input id="txt"/>
<input type="button" id="sel" onclick="javascript:gg('up.php','hf','txt')" value="上传图片" />
<input type="hidden" id="hf"/>
</div>
</form>
</body>
</html> up.php代码如下:
<?php
$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/"; //上传文件路径
?>
<html>
<head>
<title>图片上传</title>
<style type="text/css">
<!--
body
{
     font-size: 9pt;
}
input
{
     background-color: #66CCFF;
     border: 1px inset #CCCCCC;
}
-->
</style>
</head><body>
<script type="text/javascript" language="javascript">
function dd(strid, strvalue) {
var str = new Array(strid, strvalue);
window.returnValue = str;
window.close();
}
</script>
<form enctype="multipart/form-data" method="post" name="upform">
  <input type ="hidden" name="MAX_FILE_SIZE" value="2000000" />
  <label for="upfile">上传文件</label>
  <input name="upfile" type="file">
  <input type="submit" value="上传" onclick="dd( '1', document.getElementById('upfile').value)"><br>
  允许上传图片类型*.jpg *.jpeg *.gif *.png
</form><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (!is_uploaded_file($_FILES["upfile"]['tmp_name']))
    //是否存在文件
    {
         echo "图片不存在!";
         exit;
    }    $file = $_FILES["upfile"];
    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;
    }
}
?>
</body>
JavaScriptPHP

解决方案 »

  1.   

    问题有俩,一个是调用子页面传值后还自动弹出一个IE窗口,这个窗口是up.php页面,我不想让他弹出,于是在up.php head标签内加上了<base target"_self"> 代码,可一添上去,就不能上传了。。于是我在a.php中加了个框架,想让这个自动弹出的页面在这个指定框架中弹出,可不熟悉怎样制定。。拜托版主给帮帮忙,,提了几个问题没注意没积分了