我正在设计一个带有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";
答案 0 :(得分:1)
执行此操作的最佳和正确方法是通过判断图像错误值,错误值4表示没有上载文件。对文件名称或其他属性的其他测试有些不精确。
$imageFile = $_FILES['image'];
if ($imageFile['error'] != 4) {
///this code only runs if a file has been given.
}
我一直在看if($ _FILES ['image'] ['error'] === UPLOAD_ERR_NO_FILE)但不是运气,但我认为我在正确的区域。
要检查$_FILES['image']['error']
是正确的,但此值是数值而不是文本值。查看是否检查$_FILES['image']['error'] == 4
是否更有效。
将文件中的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 if ($_FILES['image']['error'] != 4 )
替换if语句。
答案 1 :(得分:0)
您的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";
查询将如下:
{{1}}
答案 2 :(得分:0)
我用这个:
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
}