我创建了一个个人资料表单,用户还可以上传个人资料图片。我正在使用此更改文件的名称并上传它:
<?php
/*image upload part*/
if (isset($_SERVER['QUERY_STRING'])) {
$target_dir = "subsites/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// changing file name to unique name
$temp = explode(".",$_FILES["fileToUpload"]["name"]);
$newfilename = uniqid('profilepic_').'.'.end($temp);
$target = "subsites/uploads/" .$newfilename;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$fileuploadcheck = 1;
}
else {
echo "Sorry, there was an error uploading your file.";
$fileuploadcheck = 0;
}
}
}
/*image upload part end*/
?>
然后我想在表单中将$ target插入隐藏字段。
<input type="hidden" name="FormPicName" id="FormFileName" value=
<?php
if ($fileuploadcheck = 1)
{
echo $target;
}
else
{ NULL; }
?>
>
问题是$ target值已经不同了!所以我最终在数据库中使用不同的文件名和不同的值。
我该如何克服这个问题?
(编辑:我已从文件上传部分删除了安全检查选项)