如何检查是否没有选择图像,请不要更新


How to check if no image is selected don't update?

我目前正在使用PDO更新功能的项目上工作,更新功能工作正常,但我的问题是,当我尝试更新记录并保持原样时,当我看到数据库图像列为空……我如何检查文件是否为空,何时为空不更新?因为当我尝试用图像更新记录并点击更新按钮时,图像就消失了。

user.class.php

public function upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE,$username,$password,$province)
{
if(empty($_FILE['myImage']['name'])){
echo "error";
}
else
{
$stmt = $this->db->prepare("UPDATE tish_images SET  FILE_NAME=:FILE_NAME,FILE_SIZE=:FILE_SIZE,FILE_TYPE=:FILE_TYPE,username=:username,password=:password,province=:province WHERE id=:id");
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $error )
{
    if ($error != UPLOAD_ERR_OK)
    {
        $errors[] = $_FILES['files']['name'][$key] . ' was not uploaded.';
        //continue;
        return FALSE;
    }
    $file_name = $key.$_FILES['files']['name'][$key];
    $file_size = $_FILES['files']['size'][$key];
    $file_tmp  = $_FILES['files']['tmp_name'][$key];
    $file_type = $_FILES['files']['type'][$key];  
    if($file_size > 2097152)
    {
        $errors[] = 'File size must be less than 2 MB';
        //continue;
        return FALSE;
    }
    try
    {
        $stmt->bindParam( ":id", $id);
        $stmt->bindParam( ":FILE_NAME", $file_name, PDO::PARAM_STR );
        $stmt->bindParam( ":FILE_SIZE", $file_size, PDO::PARAM_STR );
        $stmt->bindParam( ":FILE_TYPE", $file_type, PDO::PARAM_STR );
        $stmt->bindParam( ":username", $username);
        $stmt->bindParam( ":password", $password);
        $stmt->bindParam( ":province", $province);
        $stmt->execute();
        $desired_dir="image_uploads";
        if(is_dir($desired_dir)==false)
        {
            mkdir($desired_dir, 0700);// Create directory if it does not exist
        }
        if(is_file($desired_dir.'/'.$file_name)==false)
        {
            move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
            return TRUE;
        }
        else
        {    //rename the file if another one exist
            $new_file=$desired_dir.'/'.$file_name.time();
            move_uploaded_file($file_tmp,$new_file);  
            return TRUE;             
        }
    }
    catch(PDOException $e)
    {
      echo $e->getMessage();
      return FALSE;
    }   
}
}

}

update.php

<?php
include_once 'DB.php';
$username = isset($_GET['username']) ? $_GET['username'] : '';
$password = isset($_GET['password']) ? $_GET['password'] : '';
$province = isset($_GET['province']) ? $_GET['province'] : '';
$FILE_NAME = isset($_GET['FILE_NAME']) ? $_GET['FILE_NAME'] : '';
$FILE_SIZE = isset($_GET['FILE_SIZE']) ? $_GET['FILE_SIZE'] : '';
$FILE_TYPE = isset($_GET['FILE_TYPE']) ? $_GET['FILE_TYPE'] : '';

if(isset($_FILES['files'])){
$id = $_GET['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$province = $_POST['province'];
if($crud->upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE,$username,$password,$province))
{
    echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
}
else
{
    echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
}
}
if(isset($_GET['id']))
    {
    $id = $_GET['id'];
    extract($crud->getID($id));
    }
?>

         <?php echo $FILE_NAME; ?>
         <br />
         <br />
         <input type="file" name="files[]"  value="" multiple/>
         <br />
         <br />
         <input type="text" name="username" value="<?php echo $username; ?>">
         <br />
         <br />
         <input type="text" name="password" value="<?php echo $password ?>">
         <br />
         <br />
         <input type="text" name="province" value="<?php echo $province ?>">
         <br />
         <input type="submit"/>

我假设您正在从<input type="file" />标记获取图像,并且您想检查是否选择了图像。您可以尝试使用empty()$_FILE来检查是否选择了映像。

的例子:

<?php
    if(empty($_FILE['myImage']['name'])):
        //don't update and display an error message.
    else:
        //update record.
    endif;
?>

*注意:如果你想只接受图像,你可以很容易地做到这一点与HTML <input type="file" name="imagefilename" accept="image/x-png, image/gif, image/jpeg" />。在插入新记录或将选定的文件上传到目录之前,不要忘记在PHP中验证这一点。