如果我尝试使用代码保存新图片或将旧图片保留在数据库中,则将无法使用。
我已经重写了几次代码,但无法获取。
这是我的代码。我希望有人能帮助我。
$files = "";
if(isset($_POST['submit']) && !empty($_FILES['files']['tmp_name'])){
$files = $_FILES['files'];
$ergebnis = "SELECT FILE_NAME FROM auktion_images WHERE ANGEBOT_ID = '{$change}'";
$resultat = $pdo->query($ergebnis);
foreach ($resultat as $reihe): ?>
<?php unlink('image_uploads/'.$reihe["FILE_NAME"]);
$statement = $pdo->prepare("delete from auktion_images where ANGEBOT_ID = '{$change}'");
$statement->execute(array());
endforeach;
$query = "INSERT into auktion_images(`ANGEBOT_ID`, `FILE_NAME`, `FILE_SIZE`,`FILE_TYPE`)
VALUES(:ANGEBOT_ID, :FILE_NAME, :FILE_SIZE, :FILE_TYPE)";
$stmt = $pdo->prepare($query);
foreach($_FILES['files']['tmp_name'] as $key => $error ){
if ($error != UPLOAD_ERR_OK) {
$errors[] = $_FILES['files']['name'][$key] . ' wurde nicht hochgeladen.';
continue;
}
$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[] = 'Bilder müssen kleiner als 2 MB sein.';
continue;
}
try{
$stmt->bindParam( ':ANGEBOT_ID', $change, PDO::PARAM_STR );
$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);// Create directory if it does not exist
}
if(is_file($desired_dir.'/'.$file_name)==false){
move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
}else{ //rename the file if another one exist
$new_file=$desired_dir.'/'.$file_name.time();
move_uploaded_file($file_tmp,$new_file) ;
}
}catch(PDOException $e){
$errors[] = $file_name . 'nicht in Datenbank gespeichert.';
echo $e->getMessage();
}
}}
elseif(isset($_POST['submit']) && !isset($files)){
到目前为止,最好的事情是我可以保存新照片。
答案 0 :(得分:0)
if(isset($_POST['submit']) && !empty($_FILES['files']['tmp_name'][0])){ ... }
if(isset($_POST['submit']) && empty($_FILES['files']['tmp_name'][0])){ ...}