我正在创建一个包含最多20个图像文件的库存表单。将图像上传到服务器时,我需要更改名称并将其插入数据库,以便它可以在网站轮播上使用。名称更改以及上传。我似乎遇到的问题是图像在数据库中是相同的名称,但在上载时是唯一的。我可能看起来很明显了。任何帮助 - 任何想法??
这就是我的地方;
<?php
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
try {
if(!empty($_FILES['image']['name'][0])) {
$files = $_FILES['image'];
$uploaded = array();
$failed = array();
$allowed = array('gif', 'png', 'jpg', 'jpeg');
foreach($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
if(in_array($file_ext, $allowed)) {
if($file_error === 0) {
if($file_size <= 2097152) {// 2MB
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'assets/uploads/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
$uploaded[$position] = $file_destination;
} else {
$failed[$position] = "[{$file_name}] failed to upload";
}
} else {
$failed[$position] = "[{$file_name}] is too large";
}
} else {
$failed[$position] = "[{$file_name}] errored with code [{$file_error}]";
}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed";
}
}
}
$insert = DB::getInstance()->insert('stock', array(
'image' => $file_name_new,
'image_1' => $file_name_new,
'image_2' => $file_name_new,
'image_3' => $file_name_new,
'image_4' => $file_name_new,
'image_5' => $file_name_new,
'image_6' => $file_name_new,
'image_7' => $file_name_new,
'image_8' => $file_name_new,
'image_9' => $file_name_new,
'image_10' => $file_name_new,
'image_11' => $file_name_new,
'image_12' => $file_name_new,
'image_13' => $file_name_new,
'image_14' => $file_name_new,
'image_15' => $file_name_new,
'image_16' => $file_name_new,
'image_17' => $file_name_new,
'image_18' => $file_name_new,
'image_19' => $file_name_new
));
} catch(Exception $e) {
die($e->getMessage());
}
}
}
?>
表单模型;
<form action="" method="post" enctype="multipart/form-data">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label class="block"><strong>Upload Vehicle Images</strong><br>(Min = 1)<br>(Max = 20)</label><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="29"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="30"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="31"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="32"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="33"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="34"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="35"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="36"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="37"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="38"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="39"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="40"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="41"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="42"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="43"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="44"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="45"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="46"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="47"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="48"><br>
</div>
</div>
</div>
<br>
<button class="btn btn-primary disabled" type="submit">FORM SUBMIT</button>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
任何人都可以帮助,在这里扼杀我的头脑: - (
答案 0 :(得分:0)
这是因为你将它们插入到foreach
-loop之外的数据库中。当你在循环外执行此操作时,$file_name_new
- 变量值将始终相同。
尝试以下操作:
<?php
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
try {
if(!empty($_FILES['image']['name'][0])) {
$files = $_FILES['image'];
$uploaded = array();
$failed = array();
$allowed = array('gif', 'png', 'jpg', 'jpeg');
foreach($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
if(in_array($file_ext, $allowed)) {
if($file_error === 0) {
if($file_size <= 2097152) {// 2MB
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'assets/uploads/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
$uploaded[$position] = $file_destination;
} else {
$failed[$position] = "[{$file_name}] failed to upload";
}
} else {
$failed[$position] = "[{$file_name}] is too large";
}
} else {
$failed[$position] = "[{$file_name}] errored with code [{$file_error}]";
}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed";
}
$insert = DB::getInstance()->insert('stock', array(
'image' => $file_name_new,
'image_1' => $file_name_new,
'image_2' => $file_name_new,
'image_3' => $file_name_new,
'image_4' => $file_name_new,
'image_5' => $file_name_new,
'image_6' => $file_name_new,
'image_7' => $file_name_new,
'image_8' => $file_name_new,
'image_9' => $file_name_new,
'image_10' => $file_name_new,
'image_11' => $file_name_new,
'image_12' => $file_name_new,
'image_13' => $file_name_new,
'image_14' => $file_name_new,
'image_15' => $file_name_new,
'image_16' => $file_name_new,
'image_17' => $file_name_new,
'image_18' => $file_name_new,
'image_19' => $file_name_new
));
}
}
} catch(Exception $e) {
die($e->getMessage());
}
}
}