数据库MYSQL,结构id,name,url
<form action="post.php" method="post" class="form-ele">
<table width="100%" border="0" >
<tbody>
<tr >
<td ><label for="name">姓名:</label> </td>
<td><div class="divinput"><input id="name" type="text" name="name" value="<?php echo $name; ?>" disabled readonly="readonly" > </div>
</td>
</tr>
<tr >
<td >附件</td>
<td width="90%">
<body>
这里希望可以上传文件或图片到服务器
</body>
</td>
</tr>
<table>
</form>
我找过上传代码但是上传后能获取的只有文件名称而不是url
我希望各位能帮我写完或者提供可以用的代码,十分感谢
因为是自己在学习,所以抱歉了各位
PHP 上传图片后获取图片url存为url
和table内的表单一起post到mysql记录id
,name
,url
'1','张三','upload/202209201507.png'
代码 改成
<form action="post.php" method="post" class="form-ele" enctype="multipart/form-data">
<table width="100%" border="0" >
<tr>
<td><label for="name">姓名:</label></td>
<td><div class="divinput">
<input id="name" type="text" name="name" value="<?php echo $name; ?>" disabled readonly="readonly" >
</div></td>
</tr>
<tr>
<td >附件</td>
<td width="90%"><input type="file" name="fileimg"></td>
</tr>
<table>
</form>
然后php里就可以用
利用 copy($_FILES["fileimg"]["tmp_name"],‘上传保存路径’) 得到上传的后的地址,保存到url里,上传代码如下:
$oldname=$_FILES["fileimg"]["name"];
$fileextensionname=strtolower(preg_replace("/.+(\..+)/i","\\1",$oldname));
$filename = date('YmdHis').$fileextensionname;
$core_filetype='.jpg|.jpge|.gif|.png';
$img_type=array("image/gif","image/jpeg","image/jpg","image/pjpeg","image/x-png","image/png");
$core_filesize=200;
if($oldname!=""){
if($_FILES["fileimg"]["error"]>0){
echo '片上传失败';exit;
}elseif(!preg_match("/".$core_filetype."/i",$fileextensionname)){
echo 上传失败:上传的文件类型不';exit;
}elseif(!in_array($_FILES["fileimg"]["type"],$img_type)){
echo '上传失败:上传的文件类型不被容许!!';exit;
}elseif(($_FILES["fileimg"]["size"]/1024)>$core_filesize){
echo "上传失败:上传的文件大小超过".$core_filesize."KB限制!";exit;
}else{
$imgsetpath="F:/";
$directory='upload/';
if(!file_exists($imgsetpath.$directory)){
if (mkdir($imgsetpath.$directory, 0777, true)){
chmod($imgsetpath.$directory, 0777 );
}else{
echo '上传失败,创建文件夹失败';exit;
}
}
if(copy($_FILES["fileimg"]["tmp_name"],$imgsetpath.$directory."/".$filename)){
}else{
echo "文件大小:".(round($_FILES["fileimg"]["size"]/1024, 2))."KB上传失败!!!!";exit;
}
$imgurl=$directory."/".$filename;
}
}