PHP更改个人资料图片
问题描述:
我正在用PHP,HTML和CSS创建一个社交网络项目。用户具有的功能之一是上传个人资料图片。我只是看了几个教程,我一直在关注他们,但他们很混乱,我完全迷失了。这是我最后的希望。我在我的upload.php文件中有一些代码,当我去那个页面它说File saved
这意味着图像已保存,但我没有看到图像的任何地方。有人可以帮我将图像保存到我的文件夹中,当用户单击更改配置文件图片时,图片会被更改并保存吗?请帮忙 。谢谢 。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>
<!-- 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'">×</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>
upload.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);
+0
哪如果?我现在有两个 –
+0
,现在可以节省,但我该如何更改图片? –
这里的tutorial__你应该遵循http://php.net/file-upload –
的__basic我建议你检查我回答这个问题:https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921它会教你很多关于图像上传和安全。您也可以在答案的底部下载一个非常易于使用的库。 – icecub
umm,默认情况下,如果您的上传代码有效,它会将文件保存在临时文件夹中,您需要从中选择该文件并将其放在任何公用文件夹中。按照任何一步一步的教程。我建议亚历克斯视频上的PHP,#新波士顿 –