我有一个网站,该网站向用户提供上传图片的功能,但显示错误,仅jpg,jpeg,png类型的格式可用,这是我的PHP代码
<?php require "config.php";?>
<?php
session_start();
if($_SESSION['is_login']){
$email = $_SESSION["email"];
}else{
header("Location:dashboard.php");
}?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<style type="text/css">
.modal-backdrop.in {
position: relative;
opacity: .5;
}
.modal-title{
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
img{
width: 100%
}
.btn{
color: #87c5db;
border-color: #87c5db;
float: right;
}
.btn:hover{
background-color: #87c5db;
color: white;
}
</style>
</head>
<body>
<?php
$query = "SELECT * FROM photos";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
$users = mysqli_fetch_assoc($fire);
$pic_id = $users['id'];
}
?>
<?php
$query = "SELECT * FROM users WHERE email='$email'or username =
'$email'or mobile='$email'";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
$row = mysqli_fetch_assoc($fire);
$id = $row['id'];
$email2 = $row['email'];
$username = $row['username'];
$avatarpath = $row['avatar_path'];
if (isset($_POST['uploadpic'])) {
$avatar = $_FILES['avatar'];
$avatar_name = $_FILES['avatar']['name'];
$avatar_tmpname = $_FILES['avatar']['tmp_name'];
$avatar_size = $_FILES['avatar']['size'];
$avatar_type = $_FILES['avatar']['type'];
$avatar_ext = pathinfo($avatar_name, PATHINFO_EXTENSION);
if (!empty($avatar_name)) {
if ($avatar_size <= 25000000) {
if ($avatar_ext == "jpg" || $avatar_ext == "jpeg" || $avatar_ext == "gif" ||$avatar_ext == "png" ) {
$chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name=substr(str_shuffle($chars),0,15);
mkdir("userdata/user_photos/$rand_dir_name");
$final_file= "userdata/user_photos/$rand_dir_name/$avatar_name";
$upload = move_uploaded_file($avatar_tmpname, $final_file);
if ($upload) {
$msg = "file uploaded successfully ";
$query = "INSERT INTO photos(uid,image_url,email,date_posted,username,avatar_path,time2) VALUES ('$id','userdata/user_photos/$rand_dir_name/$avatar_name','$email2',NOW(),'$username','$avatarpath',NOW())";
$fire = mysqli_query($con,$query) or die("can not insert file path into database".mysqli_error($con));
if ($fire) {
$msg .=" and also inserted into database";
header("Location:filters.php");
}
# code...
}else{ echo "only jpg,jpeg,png, type format allowed";}
}else{echo "file size is too large";}
}else{echo "please select an image to upload";}
}
}
}
?>
<div class="container">
<!-- Trigger the modal with a button -->
<!-- Modal -->
<div class="modal show" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button onclick="javascript:location.href='dashboard.php'" type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Upload your pic </h4>
</div>
<div class="modal-body">
<img src="images/gallery.png">
<form action="#" method="POST" enctype="multipart/form-data">
<input type="file" class="form-control" name="avatar" >
<button type="submit" class="btn btn-default" name="uploadpic" > upload</button>
<button type="button" onclick="javascript:location.href='dashboard.php'" class="btn btn-default" data-dismiss="modal" >Cancel</button>
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</div>
</body>
</html>
,我只选择png,jpeg或jpg格式的图片,但我不知道为什么会显示此错误,并尝试删除包含扩展名类型的if语句,但脚本在实时服务器中无法正常工作虽然它可以在本地主机上工作
答案 0 :(得分:0)
我得到了答案,实际上我的userdata文件夹中没有一个名为user_photos的文件夹,但我在代码中定义了它,现在我将其命名为userdata了,现在已经解决了thanx,以寻求您的帮助