我正在设计一个带有PHP PDO的CMS,供用户更新那里的信息,这部分我让用户更改那里的业务图像,它将通过PHPMyAdmin上传到服务器和数据库。
有了这个,我可以上传图像,它也会很好地替换数据库中的值。我遇到的问题是,当用户只想更新其他信息(而不是图像)而不更新其他信息时,系统会提交图像值,并将数据库中的行替换为相关行中没有值(空白上载)?因为我希望它不会影响这一行,如果没有上传图像的话。
我一直在看if ($_FILES['image']['error'] === UPLOAD_ERR_NO_FILE)
,但不是运气,但我认为我在正确的领域。
HTML
<form action='edit_account.php' method='post' enctype='multipart/form-data'>
<h4>Business Image</h4>
<input type='file' name='image' id='fileToUpload' >
<input type='submit' value='Update Account' />
</form>
PHP
if(isset($_FILES['image'])) {
$username = $_POST['username'];
$target_dir = "images/users/$username/";
$target_file = $target_dir.basename($_FILES['image']['name']);
$upload0K = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if(isset($_POST['submit'])) {
$check = getimagesize($_FILES['image']['tmp_name']);
if($check !== false) {
echo "<p>File is an image - ".$check["mime"]."</p>";
$upload0K = 1;
} else {
echo "<p>File is not an image</p>";
$upload0K = 0;
}
}
//if (file_exists($target_file)) {
//echo "<p>Sorry, file already exists.</p>";
//$upload0K = 0;
//}
if ($_FILES['image']['size'] > 5000000) {
echo "<p>Sorry, your file is too large.</p>";
$upload0K = 0;
}
///
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "<p>Sorry, only JPG, JPEG, PNG and GIF files are allowed.</p>";
$upload0K = 0;
}
//if(!isset($_FILES['image']) || $_FILES['image']['error'] == UPLOAD_ERR_NO_FILE) {
//echo "Error no file selected";
//}
///
if ($upload0K == 0) {
echo "Sorry, your file was not uploaded.";
}
else {
if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
echo "<p>The file ". basename( $_FILES["image"]["name"])."has been uploaded.</p>";
} else {
echo "<p>Sorry, there was an error uploading your file.</p>";
}
}
}
SQL插入
$query_params = array(
':email' => $_POST['email'],
':username' => $_POST['username'],
':name' => $_POST['name'],
':image' => $_FILES['image']['name'],
':description' => $_POST['description'],
':moderatedcheck' => $moderatedcheck,
':user_id' => $_SESSION['user']['id']
);
$query = " UPDATE users SET email = :email, username = :username, name = :name, image = :image, description = :description, moderatedcheck = :moderatedcheck";
最好正确的方法是判断图像错误值,错误值为4表示没有上传文件。对文件名或其他属性的其他测试有些不精确。
$imageFile = $_FILES['image'];
if ($imageFile['error'] != 4) {
///this code only runs if a file has been given.
}
更新
我一直在看($_FILES['image']['error']===UPLOAD_ERR_NO_FILE),但运气不好,但我认为我在正确的领域。
$_FILES['image']['error']
是要检查的正确值,但该值是一个数字值,而不是文本值。检查$_FILES['image']['error'] == 4
是否工作得更好。
更新2
将文件中的if(isset($_FILES['image'])) {
替换为:
if ($_FILES['image']['error'] != 4)}{
如果有给定的文件,这将只运行以下代码,尽管可能仍然存在文件大小等错误。
还有SQL插入:
将原件替换为:
$query_params = array(
':email' => $_POST['email'],
':username' => $_POST['username'],
':name' => $_POST['name'],
':description' => $_POST['description'],
':moderatedcheck' => $moderatedcheck,
':user_id' => $_SESSION['user']['id']
);
$query = " UPDATE users SET email = :email, username = :username, name = :name, description = :description, moderatedcheck = :moderatedcheck";
并添加一个限定符:
if ($_FILES['image']['error'] == 0 ){
$query_params[':image'] = $_FILES['image']['name'];
$query = " UPDATE users SET email = :email, username = :username, name = :name, image = :image, description = :description, moderatedcheck = :moderatedcheck";
}
如果文件i)存在并且ii)没有错误,那么这将仅将文件名数据添加到插入数组。或者,如果确实有错误,则将文件保存到数据库可能毫无意义,但可以用if ($_FILES['image']['error'] != 4 )
替换if语句。
您的Sql可以动态构建,在代码开始时定义一个类似$imageSql = '';
的变量,当$_FILES['image']
被设置时,将覆盖您的$imageSql = ', image = :image';
和$query_params[':image'] = $_FILES['image']['name']
,最后您的查询将如下:$query = " UPDATE users SET email = :email, username = :username, name = :name".$imageSql.", description = :description, moderatedcheck = :moderatedcheck";
我使用这个:
if(is_uploaded_file($_FILES["image"]["tmp_name"])){
//query to update the data with the image
}else{
//query to update the data without the image
}