我正在尝试为论坛开发用户页面,但我在上传图片时遇到了一些困难。问题是我想限制用户只能上传一张图片,但可以随时更改。所以基本上,我想要么覆盖现有文件,要么删除旧图片并添加新图片。在这一点上,我有一段代码在文件末尾添加了一个时间戳(我实际上并不需要)。
代码:
if(isset($_POST['upload']))
{
$extension=strstr($_FILES['uploadedfile']['name'], ".");
$filename = "_/userfiles/userpics/".basename($_FILES['uploadedfile']['name'],
$extension);
$target = "_/userfiles/userpics/".basename($_FILES['uploadedfile']['name']);
$valid = true;
if(file_exists($target))
{
$filename = $filename . time();
$target = $filename . $extension;
}
if($valid)
{
// move the file into the folder of our choise
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target);
$img_sql = "INSERT INTO sp_userimage (imageid, path, id) value ('', '".$target."', '".$_SESSION['userid']."')";
$img_result = mysql_query($img_sql);
echo "upload sucessfull";
}