更改用户个人头像PHP

时间:2016-12-21 19:48:06

标签: javascript php html

我想在用户点击更改个人资料图片时更改用户的个人资料图片,但它无效。 Noe错误或任何不起作用的东西。我想在javascript或PHP中更改个人资料图片

profile.php:

<form id="form2" action="upload.php" method="post" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub1" type="submit" value="Change profile picture" name="submit"><br />
</form>

upload.php的:

<?php

$target_dir = "C:\wamp\www\TestSocialNetwork\uploads"; 
//This is the directry where the images will be uploaded, "server_path/uploads"

//Now since we have the target directory, let's define the actual path.
//Actual Path = Target Dir + File Name + File Extension

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

//By now, we have just madefined locations, (in the form of Variables)

//Now, lets do few checks, and make a success flag to keep a track
$uploadOk = 1;

// Check if file already exists
if (file_exists($target_file)) {
//If the file already exists
echo "Sorry, file already exists.";
$uploadOk = 0;
//Echo and change the flag to False
}  

// Check if image file is a actual image or fake image.
//If file is image, then only image size will be returned
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if(!($check)) {
//That is, the Check is false, the file is NOT AN IMAGE
echo "File is not an image.";
$uploadOk = 0;
//Echo, and change the Flag to false.
}

//Now check, if the flag is true, that means upload is OK, and NO ERRORS.
//Then upload the file.
if ($uploadOk) {
//If upload is OK, then go ahead
move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
// Move the uploaded file to the desired folder
}
?>

1 个答案:

答案 0 :(得分:0)

您需要在成功上传后重新加载页面以反映更改,您可以使用php header(),即:

if ($uploadOk) {
    //If upload is OK, then go ahead
    // Move the uploaded file to the desired folder
    move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
    header("Location: https://the user profile url");
}

您还可以使用htmlphp标头禁止用户个人资料页面上的缓存。

php no-cache示例

 header("Cache-Control: no-store, no-cache, must-revalidate"); 

html5 no-cache示例

<!DOCTYPE html>
<html manifest="manifest.appcache">

然后使用以下内容创建manifest.appcache

CACHE MANIFEST
# Cache manifest version 1.0
# no cache
NETWORK:
*

SRC

<强> PS

  

一个重要的补充是你永远不能强迫浏览器这样做   任何东西。你所能做的就是提出友好的建议。这取决于   浏览器和用户实际遵循这些建议。一个浏览器   可以自由忽略这一点,或者用户可以覆盖默认值。

SRC