我正在创建一个社交网站。我可以选择上传最多 10 张带文字或不带文字的图片,也可以选择上传不带图片的文字。
所以下面我想看看用户是只上传文本还是带有图像的文本。如果用户只上传了文本,那么我希望将文本插入到它自己的单独表格中。如果用户上传带有图像的文本或只是图像,我也想检查一下。如何检查用户是否上传了图像,如果是,则运行图像脚本,如果没有,则跳出文件?这是我目前所拥有的。
<input name="files[]" id='files' accept="image/png,image/gif,image/jpeg" type="file" multiple="multiple" />
$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 4367463;
if(isset($_POST['submit'])) {
$errors = '';
if ($_POST['title_post'] == '') {
$errors = "Choose a topic.";
} else {
$create_topic = $con->prepare("INSERT INTO subtopic (subtopic, description)
VALUES (?, ?)");
$create_topic->bind_param("ss", $_POST['title_post'], $_POST['description']);
$create_topic->execute();
}
// --------------- CHECK IMAGES ----------****
$file_count = count($_FILES['files']['name']);
if ($file_count > 0) {
if ($file_count > $img_limit) {
$errors = 'Cannot upload more than 10 images';
}
if (!$errors) {
$stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
$stmt->bind_param('s', $file_path);
// Loop through each file
for( $i=0; $i < $file_count; $i++ ) {
$file_name = $_FILES['files']['name'][$i];
$file_size = $_FILES['files']['size'][$i];
$file_tmp = $_FILES['files']['tmp_name'][$i];
$imageFileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
if ($file_size >= $maxsize) {
$errors = "Your file is too large";
} elseif ($imageFileType != "jpeg" && $imageFileType != "jpg" &&
$imageFileType != "png" && $imageFileType != "gif") {
$errors = "File type not allowed.";
}
//Make sure we have a file path
if (!$errors /* && $file_tmp != "" */) {
$picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
$uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);
$file_path = "uploads/" . $picToUpload;
$stmt->execute();
}
}
}} else {
header('Location: index4.php');
exit();
}
// --------------- CHECK IMAGES ----------****
}
答案 0 :(得分:1)
您需要检查当前文件 - 例如:
if(!file_exists($_FILES['files']['tmp_name'][0]) || !is_uploaded_file($_FILES['files']['tmp_name'][0])){
}
其中 0 等于 $i