javascript - PHP更改个人资料图片

标签 javascript php html css

我正在使用 PHP、HTML 和 CSS 创建一个社交网络元素。用户拥有的功能之一是上传个人资料照片。我刚刚看了几个教程,我一直在关注它们,但它们非常令人困惑,我完全迷路了。这是我最后的希望。我的 upload.php 文件中有一些代码,当我转到该页面时它显示 File saved 这意味着图像已保存但我在任何地方都看不到图像。有人可以帮我将图像保存到我的文件夹中,当用户单击更改个人资料图片时,图片会更改并保存吗?请帮忙 。谢谢。

配置文件.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>

<!-- Trigger the Modal -->
<img id="myImg" src="default.png" width="200" height="150">

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- The Close Button -->
<span class="close" 
onclick="document.getElementById('myModal').style.display='none'">&times;</span>

<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">

<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a 
caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
modal.style.display = "none";
}
</script>

上传.php :

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include("connect.php"); 
include("auth_login.php"); 

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File saved - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}

最佳答案

您的目标文件永远不会移动到实际目录!它保留在临时目录中:

if 添加文件移动:

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

关于javascript - PHP更改个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577417/

相关文章:

javascript - 带有单个按钮的侧边栏导航切换

javascript - 如何从全局变量正确填充 javascript 对象

html - 外部 css 文件一半在 html 中工作

javascript - 如何将 fullcalendar 的名称月份更改为西类牙语或任何语言

javascript - try/catch 导致堆栈中的 <anonymous>

php - 添加分页代码后的结果在 PHP 中变得困惑

php - mysql 语句中的条件

javascript - 放大和缩小在 chrome 中不起作用

javascript - 在 javascript 中,图像必须随 var lvl 更改,但事实并非如此

php - 无效配置 – yii\base\InvalidConfigException