在 PHP 中上传文本中的详细信息而不上传图片
Upload details in text without uploading picture in PHP
我遇到了一个问题,我无法在不更新个人资料图片的情况下更新我的用户的详细信息。如果我在没有上传任何图片的情况下单击提交按钮,则会出现如下错误。如果我同时更新了个人资料图片,我可以更新详细信息。
这是我的 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>
如果您没有上传图片,仍然会检查[不存在的] 图片 - 但会失败。您需要检查文件是否确实已上传,只有在上传后才继续检查其尺寸。
$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
文件名是否为空;没有这个你会检查是否存在空文件名,这会产生警告。
(如果解决了您的问题,请采纳答案,编码愉快!:))
我遇到了一个问题,我无法在不更新个人资料图片的情况下更新我的用户的详细信息。如果我在没有上传任何图片的情况下单击提交按钮,则会出现如下错误。如果我同时更新了个人资料图片,我可以更新详细信息。
这是我的 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>
如果您没有上传图片,仍然会检查[不存在的] 图片 - 但会失败。您需要检查文件是否确实已上传,只有在上传后才继续检查其尺寸。
$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
文件名是否为空;没有这个你会检查是否存在空文件名,这会产生警告。
(如果解决了您的问题,请采纳答案,编码愉快!:))