PHP kullanarak birden fazla dosya yükleme işlemek nasıl

3 Cevap php

Ben PHP kullanarak dosya yüklemek istiyorum ama sorun ben upload edecek kaç dosya bilmiyorum olmasıdır.

Benim soru file[] kullanırsanız nasıl dosya yükleyebilir mi?

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

Ben sadece dosya kutusunu katacak ve PHP bunları işlemek için nasıl yüklemek için daha fazla dosya girişi oluşturmak için JavaScript kullanabilirsiniz ama olacak?

3 Cevap

Bakınız: $_FILES, Handling file uploads

<?php
    if(isset($_FILES['file']['tmp_name']))
    {
        // Number of uploaded files
        $num_files = count($_FILES['file']['tmp_name']);

        /** loop through the array of files ***/
        for($i=0; $i < $num_files;$i++)
        {
            // check if there is a file in the array
            if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
            {
                $messages[] = 'No file uploaded';
            }
            else
            {
                // copy the file to the specified dir 
                if(@copy($_FILES['file']['tmp_name'][$i],$upload_dir.'/'.$_FILES['file']['name'][$i]))
                {
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['file']['name'][$i].' uploaded';
                }
                else
                {
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
                }
            }
        }
    }
?>

Bu benim tercih edilen bir yöntemdir. Bu tarih görüntülerde bir tablo tutmak için bir mysql ek içerir. Ayrıca kullanıcı siteler resim klasörüne yönetici görüntü klasörü ve kopya görüntü için görüntü taşır.

<?php
if(isset($_FILES['image']['tmp_name']))
{
    $num_files = count($_FILES['image']['tmp_name']);
    for($x =0; $x< $num_files;$x++){
        $image = $_FILES['image']['name'][$x];
        if(!is_uploaded_file($_FILES['image']['tmp_name'][$x]))
        {
            $messages[] = $image.' No file uploaded."<br>"';
        }
        if (move_uploaded_file($_FILES["image"]["tmp_name"][$x],"images/". $image)){
            $messages[] = $image .' uploaded';
        }
        copy("images/".$image, '../images/'.$image);
        mysql_query("INSERT INTO $table_name VALUES ('NULL','$id','images/$image')");
    }
}
?>
<?php /*insert this into the form*/ 
$count= count($messages); for ($i =0; $i < $count; $i++){echo $messages[$i]."<br>";}
  ?>

Bu deneyin:

if(isset($_FILES['image_file'])) {
    $file = $_FILES['image_file'];
    for($i = 0; $i < count($file['name']); $i++){
        $image = array(
            'name' => $file['name'][$i],
            'type' => $file['type'][$i],
            'size' => $file['size'][$i],
            'tmp_name' => $file['tmp_name'][$i],
            'error' => $file['error'][$i]
        );
// Here is your code to handle one file
}

Kodunuzda, yerine sadece '$ HTTP_POST_FILES' ve '$ resim' kullanın ...