选择我要上传的文件后,如何添加文本框等?

时间:2016-06-22 17:04:08

标签: php dynamic pdo image-uploading

所以我有这个多文件上传器,我想知道我是否可以做一些事情,比如添加一些文本框,在一些缩略图中显示图像,甚至在上传数据之前检查尺寸/扩展名。

对于每个选定的文件,我必须向数据库添加一些数据,我不知道如何使每个文本框动态显示并将数据添加到数据库

这是php代码:

//cod pentru upload
if(!empty($_FILES['files']['name'][0])) {

$files = $_FILES['files'];

$uploaded = array();
$failed = array();

$allowed = array('jpg', 'jpeg', 'png', 'gif');


$i = 1;
foreach($files['name'] as $position => $fileName) {

    $fileTmpLoc = $files['tmp_name'][$position];
    $fileSize = $files['size'][$position];
    $fileErrorMsg = $files['error'][$position];
    $fileType = $files['type'][$position];

    $file_ext = explode('.', $fileName);
    $file_ext = strtolower(end($file_ext));

    if(in_array($file_ext, $allowed)) {

        if($fileErrorMsg === 0) {

            if($fileSize <= 4000000) {

                //fac query care sa mi salveze in variabila auto_incrementu

                $stmt = $conn->prepare("SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'history' AND TABLE_NAME = 'stories'");
                $stmt->execute();
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
                $auto_inc = $row['AUTO_INCREMENT'];
                $auto_inc--;

                 // end fac query care sa mi salveze in variabila auto_incrementul

                $fileName_new = $auto_inc . "_" . $i . "." . $file_ext;
                $file_destination = '../licenta/app/uploads/media/' . $fileName_new;

                if(move_uploaded_file($fileTmpLoc, $file_destination)) {
                    $uploaded[$position] = $file_destination;
                    //cod resize
                    include_once("ak_php_img_lib_1.0.php");
                    $target_file ='../licenta/app/uploads/media/' . $fileName_new;
                    $resized_file = '../licenta/app/uploads/media/thumbnails/' . $fileName_new;
                    $wmax = 200;
                    $hmax = 300;
                    //test
                     $white=imagecolorallocate($fileName_new, 255, 255, 255); 
                    imagefill($fileName_new,0,0,$white);
                    //end test
                    ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
                    //end cod resize
                    //cod pic data to database
                    $sql = "INSERT INTO photos (file, notes, story_id) VALUES (:files, :notes, :story_id)";
                    $stmt = $conn->prepare($sql);

                    $stmt->bindParam(':file', $fileName_new);
                    $stmt->bindParam(':notes', $_POST['descriere_poza']);
                    $stmt->bindParam(':story_id', $auto_inc);


                    if($stmt->execute()){
                        $message = "POVESTEA A FOST ADAUGATA";
                    } else {
                        $message = "EROARE";
                    }
                    //end cod pic data to database
                } else {
                    $failed[$position] = "[{$fileName}] failed to upload";
                }


            } else {
                $failed[$position] = "[{$fileName}] is too large";
            }

        } else {
             $failed[$position] = "[{$fileName}] failed to upload {$fileErrorMsg}";
        }

    $i++;   
    } else {
        $failed[$position] = "[{$fileName}] file extension '{$file_ext} not allowed.";
    }
}

if(!empty($uploaded)) {
    //cod in care ii zic ce sa faca cu ce s a uploadat cu succes
    print_r($uploaded);
}

 if(!empty($failed)) {
    //cod in care ii zic ce sa faca cu ce nu s a uploadat cu succes
     print_r($failed);
}


}

html代码:

<form action="upload_test.php" method="POST" enctype="multipart/form-data">
      <textarea name="descriere_poza" rows="5" cols="40" placeholder="descriere" required></textarea>
    <input type="file" name="files[]" multiple>

    <input type="submit">
</form>

<?php if(!empty($message)): ?>
<p><?= $message ?></p>
<?php endif; ?>

0 个答案:

没有答案