【问题标题】:PHP change profile picturePHP更改个人资料图片
【发布时间】:2018-01-16 13:27:15
【问题描述】:

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

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'">&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;
}
}

【问题讨论】:

  • 这里是基础教程你应该关注php.net/file-upload
  • 我建议你查看我对这个问题的回答:stackoverflow.com/questions/38509334/… 它会教你很多关于图片上传和安全的知识。您还可以在答案底部下载一个非常易于使用的库。
  • 嗯,默认情况下,如果您的上传代码有效,它会将文件保存在临时文件夹中,您需要从中选择该文件并将其放置在任何公共文件夹中。按照任何分步教程进行操作。我推荐关于 php 的 Alex 视频,#newboston

标签: javascript php html css


【解决方案1】:

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

if添加文件移动:

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

【讨论】:

  • 如果?我有两个
  • 现在可以保存了,但是怎么换图片呢?
猜你喜欢
  • 1970-01-01
  • 2019-02-16
  • 2019-09-14
  • 2021-09-25
  • 2011-03-03
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 2019-05-24
相关资源
最近更新 更多