PHP上传图片并把图片地址写入表单中!
高手,请教同一问题:PHP上传图片并把图片地址写入表单中!悬赏分:30-离问题结束还有14天23小时在后台添加文章的时候,有一个地方要用到图片上传,我想加一个表单,在上传...
高手,请教同一问题:PHP上传图片并把图片地址写入表单中! 悬赏分:30 - 离问题结束还有 14 天 23 小时 在后台添加文章的时候,有一个地方要用到图片上传,我想加一个表单,在上传图片的时候,点击上传后,图片地址写入到表单中,当页面其它表单(文章的标题, 内容。。。)填写好后,点提交 这时能把图片的地址也提交到数据库中。。。
展开
2013-08-06
展开全部
index.php
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
$uploadDir = 'temp/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include "connect.php";
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br>Files uploaded<br>";
}
?>
uploadclass.php
<?php
$uploaddir = "temp1/";//设置文件生涯目次 留意包孕 /
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的范例
$patch="upload/";//措施地点路径
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//天生随机文件名函数
function random($length)
{
$hash = 'CR-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$a=strtolower(fileext($_FILES['file']['name']));
//确定 文件范例
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
{
$text=implode(",",$type);
echo "您只能上传以下范例文件: ",$text,"<br>";
}
//天生方针文件的文件名
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10); //设置随机数长度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
echo "上传失败!";
}
else
{//输出图片预览
echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='upload.htm'>连续 上传</a></center>";
}
}
}
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
$uploadDir = 'temp/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include "connect.php";
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br>Files uploaded<br>";
}
?>
uploadclass.php
<?php
$uploaddir = "temp1/";//设置文件生涯目次 留意包孕 /
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的范例
$patch="upload/";//措施地点路径
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//天生随机文件名函数
function random($length)
{
$hash = 'CR-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$a=strtolower(fileext($_FILES['file']['name']));
//确定 文件范例
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
{
$text=implode(",",$type);
echo "您只能上传以下范例文件: ",$text,"<br>";
}
//天生方针文件的文件名
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10); //设置随机数长度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
echo "上传失败!";
}
else
{//输出图片预览
echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='upload.htm'>连续 上传</a></center>";
}
}
}
2013-08-06
展开全部
不可能这么做呢,你的思路首先就出现了错误。应该是将图片上传到web站点的某文件夹下,将图片路径和图片名保存到数据库中,然后再表单中使用时则读取数据库内容。你可以查下PHP的示例,我记得有一个图片上传的比较详细的上传。至于读取就不用多说了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-06
展开全部
PHP可以直接上传图片到文件夹 在数据库中保存图片路径即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询