我这里有更新图像的代码,当我选择图像时更新工作正常。但是当我尝试在不更改图像的情况下进行更新时,它会成功更新。当我查看我的数据库时,该字段为空。每当我尝试更新它并将图像保留为原样时它只会保存一个空字段。当我尝试更新并选择图像来更改数据库中的当前图像时,它工作正常。我不知道它有什么问题,没有错误。有人可以帮帮我吗?
user.class.php
public function upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE)
{
$stmt = $this->db->prepare("UPDATE tish_images SET FILE_NAME=:FILE_NAME,FILE_SIZE=:FILE_SIZE,FILE_TYPE=:FILE_TYPE 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.';
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';
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->execute();
$desired_dir="image_uploads";
if(is_dir($desired_dir)==false)
{
mkdir($desired_dir, 0700);
}
if(is_file($desired_dir.'/'.$file_name)==false)
{
move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
return TRUE;
}
else
{
$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';
$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'];
if($crud->upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE))
{
echo "<script type='text/javascript'>alert('Successfully Updated!'); </script>";
}
else
{
echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
}
}
<body>
<div class="container">
<p><strong>Survey Update</strong></p>
<br />
<div id="Survey-Update">
<form method="POST" enctype="multipart/form-data" action="">
<table class='table table-bordered'>
<?php echo $FILE_NAME; ?>
<input type="file" name="files[]" value="" multiple/>
<br />
<br />
<input type="submit"/>
</table>
</form>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
也许这可以帮助你我们
if(!empty($ _ FILES [&#39; fieldname&#39;] [&#39; name&#39;])){// upload} else {// dont upload}