我已经有了代码 - 请查看下面的内容。这段代码工作正常但我只是意识到它只将文件添加到uploads文件夹中,并且不会向数据库添加任何内容。有人可以帮我填空吗?我想使用一个数组,并尽可能简单。
请向下滚动到执行代码的注释,图像会进入文件夹。
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
}
}
}
?>
答案 0 :(得分:0)
你在寻找这样的东西:
请在以下查询中添加database field names
和database table name
:
mysqli_query("INSERT INTO `images` SET `image_name` = '".$_FILES['file']['name'][$i]."'") or die (mysqli_error());
您的代码现在应该是:
<?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
mysqli_query("INSERT INTO `images` SET `image_name` = '".$_FILES['file']['name'][$i]."'") or die (mysqli_error()); // 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
}
}
}
?>
答案 1 :(得分:0)
由于以下原因,您的代码对我来说非常混乱:
以下是我要做的事情:
<?php
if (isset($_POST['submit'])) {
// Variable for indexing uploaded image.
$j = 0;
// Declaring Path for uploaded images.
$target_path = "uploads/test/";
// Loop to get individual element from the array
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
// Extensions which are allowed.
$validextensions = array("jpeg", "jpg", "png");
// Explode file name from dot(.)
$ext = explode('.', basename($_FILES['file']['name'][$i]));
// Store extensions in the variable.
$file_extension = end($ext);
// Set the target path with a new name of image.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];
// Increment the number of uploaded images according to the files in array.
$j = $j + 1;
if (($_FILES["file"]["size"][$i] < 100000) && in_array($file_extension, $validextensions)) {
// If file moved to uploads folder.
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
echo '<div id="noerror">Image '.$j.'-->Image Uploaded!</div>';
// File was moved, so execute code here
//Connection to DB
$mysqli = new mysqli("localhost", "my_user", "my_password", "table_name");
// check connection
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$mysqli->query("INSERT INTO images_table (img_column_1) VALUES ('".$_FILES['file']['tmp_name'][$i]."')");
//Close DB connection
mysqli_close($con);
// If File Was Not Moved.
} else {
echo '<div id="error">Image '.$j.'--> <b>Please Try Again!</b></div>';
}
//If File Size And File Type Was Incorrect.
} else {
echo '<div id="error">Image '.$j.'--> <b>Check file Size or Type</b></div>';
}
}
}
?>
我通常不建议使用:
echo '<div>'.$var.'</div>'
但是你的代码非常混乱,我认为这是一个更好的解决方案。