move_uploaded_file-仅在直接加载该页面后更改文件

时间:2018-12-17 16:35:01

标签: php file

因此,我正在聊天室中,我想允许我的用户上传个人资料图片。我知道AJAX正在运行,并且文件正在上传并移至用户/ [其用户名],但是在其他帐户上,除非他们加载实际的个人资料图片,否则他们的个人资料图片不会更改为新的。 这有点令人困惑,所以我将其简化:

  1. 用户成功更改个人资料图片(文件已上传)
  2. 同一用户返回聊天室,其个人资料图像已更改
  3. 在上传新个人资料图像之前曾在聊天室中的新用户重新加载页面,以发现其他用户的个人资料图像仍然相同。

此外,我发现第二个用户是否转到了包含另一个用户的个人资料图像的文件,它没有更改,但是在重新加载图像文件时确实如此。这非常令人困惑,我不知道为什么这一切正在发生。也没有任何错误。这是我的代码: (我也尝试过复制而不是move_uploaded_file)

if(!isset($_FILES["profileImage"])){
    echo '<script>window.setTimeout(function(){window.location = "messageRoom.php";}, 3000);</script>';
    die("No image recieved.<br>Automatically redirecting in 3 seconds...");
}


$target_file = "users/" . $username;




$file = $_FILES["profileImage"];

if(!exif_imagetype($file["tmp_name"])){
    echo '<script>window.setTimeout(function(){window.location = "messageRoom.php";}, 3000);</script>';
    die("File type not an image.<br>Automatically redirecting in 3 seconds...");
}

if(filesize($file["tmp_name"]) > 2000000){
    echo '<script>window.setTimeout(function(){window.location = "messageRoom.php";}, 3000);</script>';
    die("Image size over 2MB (".(round(filesize($file["tmp_name"])/100000)/10)." MB)<br>Automatically redirecting in 3 seconds...");
}


/*Deletes file if it exists*/
if(file_exists($target_file)){
    unlink($target_file);
}


if(move_uploaded_file($file["tmp_name"], $target_file)){
    echo "The file '" . basename($file["name"]) . "' has been uploaded to " . $target_file . "<br>Automatically redirecting in 3 seconds...";
} else {
    die("Error in file upload.<br>Automatically redirecting in 3 seconds...");
}

此外,在代码中看起来好像未定义$ username和$ password,但它们是在较早之前定义的。请帮忙,我不知道发生了什么事。

最后,这需要能够覆盖现有文件,我希望用户能够更改其个人资料图像。

非常感谢您!

0 个答案:

没有答案
相关问题