我遇到的问题是,如果不更新个人资料图片,则无法更新用户的详细信息。如果我单击提交按钮而不上传任何图片,则会出现如下错误。如果同时更新个人资料图片,我可以更新详细信息。
这是我的edit-profile-upload.php代码。对不起,长代码。我认为此文件中的所有内容都同等重要:
<?php
$target_dir = "../include/img/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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)
{
$uploadOk = 1;
$profile_pic = $_FILES["fileToUpload"]["tmp_name"];
$imgContent = addslashes(file_get_contents($profile_pic));
$sql = "UPDATE users SET email = :email, phone = :phone, address = :address, postal_code = :postal_code, state = :state, city = :city, country = :country,
profile_pic = '" . $imgContent . "' WHERE username = '" . $_SESSION['username'] . "'";
$insert_statement = $db->prepare($sql);
$insert_statement->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
$insert_statement->bindParam(':phone', $_POST['phone'], PDO::PARAM_STR);
$insert_statement->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
$insert_statement->bindParam(':postal_code', $_POST['postal_code'], PDO::PARAM_STR);
$insert_statement->bindParam(':state', $_POST['state_id'], PDO::PARAM_STR);
$insert_statement->bindParam(':city', $_POST['city_id'], PDO::PARAM_STR);
$insert_statement->bindParam(':country', $_POST['country_id'], PDO::PARAM_STR);
$insert_statement->execute();
if ($insert_statement)
{
echo "<script>alert('Your profile picture " . basename($_FILES["fileToUpload"]["name"]) . " has been changed.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
else
{
echo "<script>alert('Your profile picture failed to upload. Please try again.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
}
else
{
echo "<script>alert('File is not an image.');";
echo 'window.location= "../include/edit-profile.php"';
echo "</script>";
$uploadOk = 0;
}
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo "<script>alert('Sorry your file is too big.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif")
{
echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
echo "<script>alert('Sorry your file was not uploaded.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "<script>alert('The file " . basename($_FILES["fileToUpload"]["name"]) . "has been uploaded.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
}
?>
这是表格的一部分。
<form class="form-horizontal" onSubmit="return formValidation();" method="POST" enctype="multipart/form-data" action="edit-profile-upload.php">
这是上传图片的一部分:
<input type="file" class="btn btn-default" name="fileToUpload" id="fileToUpload">
这是“提交”按钮部分:
<button type="submit" name="submit" class="btn btn-success pull-right">Submit</button>
答案 0 :(得分:1)
如果您不上传图片,则该[无意义]图片的检查仍然会发生-并且失败。您需要检查文件是否实际上已上传,只有在文件上传后,您才继续检查其尺寸。
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
需要成为
$check = true; // assume everything is OK
if ( strlen( $_FILES["fileToUpload"]["tmp_name"] ) && file_exists( $_FILES["fileToUpload"]["tmp_name"] ) ) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); // if the file exists but you can't get its dimensions -> it must not be a picture
}
strlen()
仅用于检查文件名是否为file_exists
是否为空;没有这个,您将检查是否存在空文件名,这会产生警告。
(如果解决了您的问题,请接受答案,并祝您编码愉快!:))