如何实现Php图文混合上传?

tristones 2004-08-09 01:58:39
问题如标题。
实现php的图文混合上传!!!!
请给比较完成的例程。
...全文
155 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
tod204 2004-08-10
enctype="multipart/form-data"这个是关键
其他的就是通过post的方法传递..
然后获取就可以了.
回复
ekingelon 2004-08-10
<form enctypeaction="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>


其中的="multipart/form-data" 一定要有,获取就是通过post方法获取就可以!
回复
tristones 2004-08-10
juanjuan你的UploadFile有没有使用的代码,图片文件和文字输入框混合的时候,上传就有问题。不知道是不是二进制文件和文本不能同时作为参数传递的缘故。不好意思,我当然知道需要用enctype="multipart/form-data"指定传输的MIME类型。
在另外一个简单的例程中
<?
if ($_POST)
{
$uploaddir = 'upload/';
$uploadfile = $uploaddir.$_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
print "File is valid, and was successfully uploaded. Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
print "</pre>";
}
else
{?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
<?
}
?>
测试是正常的,但是只要加入<input name="mytext" type="text">这样的数据框体,传输的时候都会有问题,提示Possible file upload attack!。
其实应该有开源的gallery可以参考,可惜没有太多时间看代码。不知道juanjuan是否可以说得更加清楚一点。
谢谢
回复
tristones 2004-08-10
用juanjuan的例程测试了一下,没有问题。因为刚接触php,对php的机制不甚了解。

<?php
function UploadFile($srcName,$Size,$destName,$destPath)
{
//echo $srcName."<hr>";
//echo $destPath.$destName."<hr>";
//echo $Size."<hr>";
if (!is_uploaded_file($srcName))
{
$Result['result']="FAILDED";
$Result['info']="the file is not a upload file !";
return $Result;
}
//检查参数的合法性
if (!is_dir($destPath))
{
if (!mkdir($destPath,0777))
{
$Result['result']="FAILDED";
$Result['info']="the destination directory is not exist!";
return $Result;
}
}
if ( 1024000 < $Size )
{
$Result['result']="FAILDED";
$Result['info']="file is must be less than 1 MB(1024000 Bytes)!";
return $Result;
}
if ("" == $destPath)
{
$Result['result']="FAILDED";
$Result['info']="The destination of path is invalid!";
return $Result;
}
//输入了上载文件,但是上传未成功
if ( ($srcName == "none" || $srcName == "" ) and $destName!="")
{
$Result['result']="FAILDED";
$Result['info']="The file <font color=red>".$destName."</font> 's uploaded failed !";
return $Result;
}
//未输入上载文件
if (($srcName == "none" || $srcName == "" ) and $destName=="")
{
$Result['result']="FAILDED";
$Result['info']="no uploaded file!";
return $Result;
}
//文件重名处理
$i = 1;
while (file_exists($destPath.$destName))
{
$destName = "$i".$destName;
$i++;
}
//文件上载成功后,将文件拷贝到目的地
if (!move_uploaded_file($srcName,$destPath.$destName))
{
$Result['result']="FAILDED";
$Result['info']="the file <font color=red>".$destName."</font>'s uploaded failed!";
return $Result;
}
//返回成功信息
$Result['result']='SUCCESS';
$Result['info']=$destName;
return $Result;

}
if ($_POST)
{
$sourceName=$_FILES['userfile']['tmp_name'];
$size=$_FILES['userfile']['size'];
$uploadfile = $_FILES['userfile']['name'];
$uploaddir = 'upload/';
$myText=$_POST['myText'];
$Result=UploadFile($sourceName,$size,$uploadfile,$uploaddir);
echo $Result['result'].'<br>';
echo $Result['info'].'<br>';
echo $myText;
}
else
{?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1024000">
Send this file: <input name="userfile" type="file">
Show the Text:<input name="myText" type="text">
<input type="submit" value="Send File">
</form>
<?php
}
?>
回复
laker_tmj 2004-08-10
其中的="multipart/form-data" 一定要有,获取就是通过post方法获取就可以!
回复
pwtitle 2004-08-09
<form action="a.php" method="post" enctype="multipart/form-data" name="form1">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^关键是这里
回复
52juanjuan 2004-08-09
上传程序代码:
<? //上载文件
function UploadFile($srcName,$Size,$destName,$destPath)
{
echo $srcName."<hr>";
echo $destPath.$destName."<hr>";

if (!is_uploaded_file($srcName)) {
$Result['result']="FAILDED";
$Result['info']="the file is not a upload file !";
return $Result;
}
//检查参数的合法性
if (!is_dir($destPath)){
if (!mkdir($destPath,0777)){
$Result['result']="FAILDED";
$Result['info']="the destination directory is not exist!";
return $Result;
}
}
if ( 1024000 < $Size ){
$Result['result']="FAILDED";
$Result['info']="file is must be less than 1 MB(1024000 Bytes)!";
return $Result;
}
if ("" == $destPath){
$Result['result']="FAILDED";
$Result['info']="The destination of path is invalid!";
return $Result;
}
//输入了上载文件,但是上传未成功
if ( ($srcName == "none" || $srcName == "" ) and $destName!=""){
$Result['result']="FAILDED";
$Result['info']="The file <font color=red>".$destName."</font> 's uploaded failed !";
return $Result;
}
//未输入上载文件
if (($srcName == "none" || $srcName == "" ) and $destName==""){
$Result['result']="FAILDED";
$Result['info']="no uploaded file!";
return $Result;
}
//文件重名处理
$i = 1;
while (file_exists($destPath.$destName)){
$destName = "$i".$destName;
$i++;
}
//文件上载成功后,将文件拷贝到目的地
if (!move_uploaded_file($srcName,$destPath.$destName)){
$Result['result']="FAILDED";
$Result['info']="the file <font color=red>".$destName."</font>'s uploaded failed!";
return $Result;
}

//返回成功信息
$Result['result']='SUCCESS';
$Result['info']=$destName;
echo $destName;
return $Result;
}
?>

不知道楼主要实现什么样的功能.上面的这个上传程序没有什么不能上传的你可以试试看.
回复
okdw 2004-08-09
什么意思?

<form action="a.php" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="myfile">
<input type="text" name="mytext">
</form>

下一页,接收myfile 和mytext,不就行了
回复
相关推荐
发帖
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
帖子事件
创建了帖子
2004-08-09 01:58
社区公告
暂无公告