gpt4 book ai didi

php - 上传个人资料图片并显示

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:00 24 4
gpt4 key购买 nike

我希望用户能够上传个人资料图片(可以是 .jpg.png )并且我还希望将其显示在他们的个人资料中。我已经根据我在 Stackoverflow 和 Google 上找到的资源编写了一些代码。但是,它似乎不起作用,我找不到我的错误。

这是html

        <form action="account_settings.php" method="POST">
<input type="file" name="profilePicture"><br><br>
<input type="submit" value="Change!">
</form>

这是上传文件的处理方式。

<?php
include ('inc/header.inc.php');
if(isset($_FILES["profilePicture"]["tmp_name"]) && isset($_FILES["profilePicture"]["name"])) {
$ext = pathinfo($_FILES['profilePicture']['name'], PATHINFO_EXTENSION);
$name = $_SESSION['user_login'];
$tmp_name = $_FILES["profilePicture"]["tmp_name"];
if($ext == 'png' || $ext == 'jpg') {
if (isset($tmp_name)) {
if(!empty($tmp_name)) {
$location = '../profielfotos/';
$full_name = $name.'.'.$ext;
if(move_uploaded_file($tmp_name, $location.$full_name)) {
echo 'Photo uploaded!';
}
Down here are just some else statements with error reports.

下面的代码用于显示图像。我已经通过将图像放入个人资料图片文件夹对其进行了测试,它确实显示了该图像。但是,仍然存在问题。允许人们上传.jpg.png ,如何让网站显示图片(找到带有正确扩展名的个人资料图片)。

我已将此代码放入 src<img> 的属性标签。

<?php if ($handle = opendir('profielfotos/')) {
$file = mysql_real_escape_string($_GET['u']);
echo 'profielfotos/'.$file.'.png';
}
closedir($handle);

希望有人能帮忙,在此先感谢!附言。这是我关于堆栈溢出的第一篇文章:-D!

最佳答案

由于您没有存储有关上传文件的任何信息,您只需使用 file_exists() 方法检查哪个文件存在。看这里:

http://php.net/manual/en/function.file-exists.php

所以你的代码会变成这样(未测试):

<?php if ($handle = opendir('profielfotos/')) {
$file = mysql_real_escape_string($_GET['u']);
if (file_exists('profielfotos/'.$file.'.png')) {
echo 'profielfotos/'.$file.'.png';
} else if (file_exists('profielfotos/'.$file.'.jpg')) {
echo 'profielfotos/'.$file.'.jpg';
}
}
closedir($handle);

关于php - 上传个人资料图片并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33898834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com