簡體   English   中英

多張照片上傳php mysql

[英]multiple photos upload php mysql

這是上傳帶有說明的文件的代碼。 文件傳輸沒有問題。 我不知道如何使用它上傳多個文件。 我想發送2個文件。 我嘗試了很多方法來解決自己的問題,但我全力以赴:)預先感謝您的幫助。 在此處輸入圖片說明

形成

    <form enctype="multipart/form-data"
action="upload_image.php" method="post">
 <input type="hidden"
name="MAX_FILE_SIZE" value="524288">

 <fieldset><legend>Select a JPEG or PNG
image of 512KB or smaller to be
uploaded:</legend>

 <p><b>File:</b> <input type="file"
name="filename" /></p>
 <p>
   <label for="caption">Caption:</label>
   <input type="text" name="caption" id="caption">
 </p>

 <p><b>File2:</b> <input type="file"
name="filename2" /></p>
 <p>
   <label for="caption2">Caption2:</label>
   <input type="text" name="caption" id="caption">
 </p>

 </fieldset>
 <div align="center"><input type="submit"
name="submit" value="Submit" /></div>
 <input type="hidden" name="submitted"
value="TRUE" />
 </form>

的PHP

 <?php # Upload image

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

// Check for an uploaded file:
    if (isset($_FILES['filename'])) {

// Validate the type. Should be JPEG or PNG.
        $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPEG', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
        if (in_array($_FILES['filename']['type'], $allowed)) {

// Move the file over.
            if (move_uploaded_file ($_FILES['filename']['tmp_name'], "../images/{$_FILES['filename']['name']}")) {
                echo '<p><em>The file has been uploaded!</em></p>';
            } // End of move... IF.

        } else { // Invalid type.
            echo '<p class="error">Please upload a JPEG or PNG image.</p>';
        }
    } // End of isset($_FILES['upload']) IF.


    @$path = $_FILES["filename"]["name"];
    @$path = mysql_real_escape_string($path);
    @$type = $_FILES["filename"]["type"];
    @$size = $_FILES["filename"]["size"];
    @$nazwa = $_POST["caption"];

    $query  = "INSERT INTO photographs (";
    $query .= "  filename, type, size, caption,";
    $query .= ") VALUES (";
    $query .= "  '{$path}', '{$type}', '{$size}', '{$caption}'";
    $query .= ")";
    echo $query;

    $result = mysqli_query($connection, $query);
    if ($result) {
        // Success
        echo " Success. ";
    } else {
        // Failure
        die(" Failure. " . mysqli_error($connection));


 // Check for an error:


    if ($_FILES['filename']['error'] > 0) {
        echo '<p class="error">The file could not be uploaded because: <strong>';

// Print a message based upon the error.
        switch ($_FILES['filename']['error']) {
            case 1:
                print 'The file exceeds the upload_max_filesize setting in php.ini.';
                break;
            case 2:
                print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
                break;
            case 3:
                print 'The file was only partially uploaded.';
                break;
            case 4:
                print 'No file was uploaded.';
                break;
            case 6:
                print 'No temporary folder was available.';
                break;
            case 7:
                print 'Unable to write to the disk.';
                break;
            case 8:
                print 'File upload stopped.';
                break;
            default:
                print 'A system error occurred.';
                break;
        } // End of switch.

        print '</strong></p>';

    } // End of error IF.

    // Delete the file if it still exists:

    if (file_exists ($_FILES['filename']['tmp_name']) && is_file($_FILES['filename']['tmp_name']) ) {
        unlink ($_FILES['filename']['tmp_name']);
    }   // End of the submitted conditional.        
} 
}

?>

您只用$filename做事,但也有$filename2檢查$ filename2是否也存在並為其執行代碼。

更好的方法是這樣做:

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

每個字段名稱將是$ _FILES數組中的鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM