我已經有一個代碼 - 請在下面檢查。這段代碼工作正常,但我只是意識到它只會將文件添加到上傳文件夾中,並且不會向數據庫添加任何內容。有人能幫我填補空白嗎?我想使用一個數組並儘可能簡單。使用數組將多個圖像名稱插入數據庫
請向下滾動到執行代碼並將圖像放入文件夾的註釋。
uploader.php
<?php
if (isset($_POST['submit'])) {
$j = 0; // Variable for indexing uploaded image.
$target_path = "uploads/test/"; // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png");
// Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i]));
// Explode file name from dot(.)
$file_extension = end($ext);
// Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];
// Set the target path with a new name of image.
$j = $j + 1;
// Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 100000)
// Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
// If file moved to uploads folder.
?>
<div id="noerror">Image <?php echo $j;?>-->Image Uploaded!</div>
<?php
// File was moved, so execute code here
// In this code I would like to add file name to a DB
} else { // If File Was Not Moved.
?>
<div id="error">Image <?php echo $j;?>--> <b>Please Try Again!</b></div>
<?php
}
} else { // If File Size And File Type Was Incorrect.
?>
<div id="error">Image <?php echo $j;?>--> <b>Check file Size or Type</b></div>
<?php
}
}
}
?>
您的查詢行似乎不工作。我用myPHP測試過它,它給我語法錯誤。 – CharlotteOswald 2015-04-05 10:05:49
你能解釋我的錯誤嗎?你添加了數據庫連接文件嗎? – 2015-04-05 11:53:00