无法在php中获取文件的值
我无法获取图像的名称。无法在php中获取文件的值,php,Php,我无法获取图像的名称。 我已使用以下代码更新上载文件夹下的图像。 我正在用$image=time().“u”.strtolower(str_replace($tokan,“,$file))更新映像路径 时间函数用于在图像名称之前添加时间戳。 但问题是,只有当路径更新时,我才没有得到其中的文件名 例如: 如果我使用像sunset.jpg这样的文件类型浏览图像 我只得到“时间”而不是日落 <?php $host="localhost"; $username="root";
我已使用以下代码更新上载文件夹下的图像。
我正在用
$image=time().“u”.strtolower(str_replace($tokan,“,$file))更新映像路径代码>
时间函数用于在图像名称之前添加时间戳。
但问题是,只有当路径更新时,我才没有得到其中的文件名
例如:
如果我使用像sunset.jpg这样的文件类型浏览图像
我只得到“时间”而不是日落
<?php
$host="localhost";
$username="root";
$password="";
$db="training_swapnil";
$tbl="gallary";
$tb="album";
$img_id=$_GET['id'];
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db")or die("cannot select DB");
$q="SELECT * FROM $tbl WHERE eid='$img_id'";
$r=mysql_query($q);
$rw= mysql_fetch_assoc($r);
echo $img=stripslashes($rw['image']);?>
<form name="update_img">
<table border="0"><tr>
<td><label>Uploaded Image:</label></td>
<td><?php echo "<img src=\"uploads/$img\" width=\"150\" height=\"150\" />";?></td></tr>
<tr><td><label>Upload New Image:</label></td><td> <input type="file" name="file" id="file"/></td></tr>
<tr><td><input type="submit" name="submit" value="upload"></td></tr>
<?php
echo"here in post";
$file=$_POST['file'];
//$str=$HTTP_POST_FILES['file']['name'];
echo "str file =$str";
$tokan = array(" ", "'","`","’");
//$image= time().".$file.";
$image= time()."_".strtolower(str_replace($tokan, "" ,$file));
$path= "uploads/".$image;
echo "path using file=$image";
$sql="UPDATE gallary set image='$image', path='$path' WHERE eid='$img_id'";
echo $sql;
$res=mysql_query($sql);
$res=mysql_query($s);
if($res>0)
{
if($file !=none)
{ if(copy($HTTP_POST_FILES['file']['tmp_name'], $path)) { //header("location:viewalbum.php");
}
else
{
echo "Error";
}
}
}
?>
</table></form>
用此标签替换表单标签
<form name="update_img" action="action.php" method="post" enctype="multipart/form-data">
您的表单中缺少enctype=“multipart/form data”属性。将$file=$\u POST['file']
替换为$file=$\u FILES['file']['name']
这里有更多详细信息:您的代码绝对不准备在任何地方上线,存在巨大的安全问题。例如,$img_id未被消毒,等等
无论如何,首先将表单设置为实际接受文件上载,将其设置为多部分数据,如下所示:
<form enctype="multipart/form-data" ...
您会受到文件遍历攻击和SQL注入攻击。