我正在尝试创建一个社交网站,我希望用户能够上传图片。我之前得到过这方面的帮助但是(就像一个白痴)我错误地删除了代码。我试图让用户上传图片,但这就是我得到的:
关于更改个人资料图片按钮的这个方块是我想要看到的图片,但我不是。当用户注册时,他们有一个名为default.jpg的默认个人资料图片,但没有显示。我把它作为我数据库中userPic的默认值。
profile.php:
<form id="form2" 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="change"><br />
</form>
<center><h5><?php echo $_SESSION['username']; ?></h5></center>
<?php
$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";
$info = date('Y-m-d_H-i-s');
if (!empty($username)) {
if (isset($_FILES['change'])) {
$errors = array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$width = 500;
$height = 500;
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$tmp = explode('.', $_FILES['image']['name']);
$file_ext = strtolower(end($tmp));
$extensions = array("jpeg", "jpg", "png");
if (in_array($file_ext, $extensions) === false) {
$errors[] = "extension not allowed, please choose a JPEG or PNG file.";
}
if ($file_size > 2097152) {
$errors[] = 'File size must be 2 MB';
}
if ($width > 500 || $height > 500) {
// Cancel upload
}
if (empty($errors) == true) {
move_uploaded_file($file_tmp, "uploads/" . date('Y-m-d_H-i-s') . $file_name);
$store = "UPDATE users SET userPic='$file_name' AND date_time='" . $info . "' WHERE username='$username'";
if (mysqli_query($conn, $store)) {
} else {
echo "Update failed!";
}
} else {
print_r($errors);
echo"Couldn't upload picture";
}
}
?>
<?php
$getimg = mysqli_query($conn, "SELECT userPic & date_time FROM users WHERE
username='$username'");
$rows = mysqli_fetch_array($getimg);
$img = $rows['userPic'] ['date_time'];
?>
<?php
} else {
echo "Invalid Username";
}
?>
<img id="myImg" src="uploads/<?php echo $img ?>" alt="<?php echo $img ?>" width="200" height="150">
更新!! :
<form id="form2" 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="change"><br />
</form>
<center><h5><?php echo $_SESSION['username']; ?></h5></center>
<?php
$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";
$info = date('Y-m-d_H-i-s');
if(!empty($username))
{
if (isset($_FILES['fileToUpload'])) {
$errors= array();
$file_name = $_FILES['fileToUpload']['name'];
$file_size = $_FILES['fileToUpload']['size'];
$width = 500;
$height = 500;
$file_tmp = $_FILES['fileToUpload']['tmp_name'];
$file_type = $_FILES['fileToUpload']['type'];
$tmp = explode('.',$_FILES['fileToUpload']['name']); $file_ext=strtolower (end ($tmp));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be 2 MB';
}
if ($width > 500 || $height > 500) {
// Cancel upload
}
if(empty($errors)==true)
{
move_uploaded_file($file_tmp,"uploads/".date('Y-m-d_H-i-s').$file_name);
$store = "UPDATE users SET userPic='$file_name', date_time='" . $info . "' WHERE username='$username'";
if(mysqli_query($conn, $store))
{
}
else
{
echo "Update failed!";
}
}else{
print_r($errors);
echo"Couldn't upload picture";
}
}}
else
{
echo "Invalid Username";
}
?>
<?php
$getimg = mysqli_query($conn,"SELECT userPic, date_time FROM users WHERE
username='$username'");
$rows=mysqli_fetch_array($getimg);
$img = $rows['date_time'].$rows['userPic'];
?>
<?php
?>
<img id="myImg" src="uploads/<?php echo $img?>" alt="<?php echo $img ?>" width="200" height="150">
答案 0 :(得分:0)
&
语法错误。删除了该内容并替换为,
发件人:强>
"SELECT userPic & date_time FROM users WHERE
username='$username'"
to:
"SELECT userPic, date_time FROM users WHERE
username='$username'"
无效变量。
发件人:强>
$img = $rows['userPic'] ['date_time'];
要强>
$img = $rows['date_time'].$rows['userPic']; // how should this be formatted?
使用了错误的$_FILES
密钥。
发件人:强>
isset($_FILES['change'])
to:
isset($_FILES['fileToUpload'])
用于检索值的错误$_FILES
密钥。
发件人:强>
$_FILES['image']
要强>
$_FILES['fileToUpload']
AND operator
发件人:强>
$store = "UPDATE users SET userPic='$file_name' AND date_time='" . $info . "' WHERE username='$username'";
要强>
$store = "UPDATE users SET userPic='$file_name', date_time='" . $info . "' WHERE username='$username'";
增加文件上传大小。
在PHP文件中:
ini_set('upload_max_filesize', '40M');
ini_set('post_max_size', '40M');
或PHP.ini
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M