上传文本中的详细信息而无需在PHP中上传图片

问题描述 投票:1回答:1

我遇到的问题是,如果不更新个人资料图片,我无法更新用户的详细信息。如果我点击提交按钮而不上传任何图片,则会出现如下错误。如果我还同时更新了个人资料图片,我可以更新详细信息。

以下是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>
php file-upload image-uploading
1个回答
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文件名是否为空;如果没有这个,你将检查是否存在空文件名,这会产生警告。

(如果它已经解决了您的问题,请接受答案,并且编码愉快!:))

© www.soinside.com 2019 - 2024. All rights reserved.