你有相同的标题的原因是在它自己的循环不排列变量,而是覆盖自己。使其成为一个数组,然后写入相应的键:
$taitel[$i] = $_POST['photo_title'][$i];
你有这么多回路回事,很难建议把它放在你的脚本的最佳地点。
编辑:既然你有问题,设置此功能,我将给出一个演示。我可能会尝试一些东西。一,我会创建一个文件观察员和两个,做一个扩展类的上传类。这一次似乎有点尴尬:
UploadObserver.php
/*
** @description This will take over for a couple of pieces of your script
*/
class UploadObserver
{
private $Upload;
/*
** @description Pass your database. I like PDO and know it better so I am using that connection
** You would have to change this to accept your MySQLi connection
*/
public function __construct(\PDO $con)
{
$this->con = $con;
}
/*
** @description Create a new instance of a version of the Upload class
*/
public function upload($file)
{
# Create a new instance of our extended class
$this->Upload = new \PowerUpload($file);
# Return it for use
return $this->Upload;
}
/*
** @description This is the class that will process the files array
*/
public function getFiles($key = 'image_field')
{
$files = array();
foreach ($_FILES[$key] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
# Send back formatted array
return $files;
}
/*
** @description Checks to see if the files array has files
*/
public function filesSet($key = 'image_field')
{
return (!empty($_FILES[$key]));
}
/*
** @description This will write the row to your database
*/
public function saveToDb($settings)
{
# Make sure to prepare and bind your values just in case
$sql ="INSERT INTO tbl_report_photos SET
photo_title = ?,
photo_link = ?,
photo_unique = ?";
$query = $this->con->prepare($sql);
$query->execute($settings);
}
/*
** @description You could store this in a View class, but for sake of ease I included it here
*/
public function getSuccess()
{
ob_start();
?>
<script>
alert('Report Updated');
window.location.href = 'index.php';
</script>
<?php
$data = ob_get_contents();
ob_end_clean();
return $data;
}
}
upload.php的
这仅仅是一个什么这个类会是什么样基于你已经证明了什么的猜想在你的例子中。
class Upload
{
public $uploaded,
$file_new_name_body,
$processed,
$error;
protected $filename,
$fileArray;
public function __construct($fileArray)
{
$this->fileArray = $fileArray;
}
public function process($path)
{
if(!is_dir($path))
mkdir($path,0755,true);
if(empty($this->file_new_name_body))
$this->file_new_name_body = $this->fileArray['name'];
$this->error = $this->fileArray['error'];
$this->processed = move_uploaded_file($this->fileArray['tmp_name'],str_replace('//','/',$path.'/'.$this->file_new_name_body));
if(!$this->processed)
throw new \Exception("File was unable to upload.");
}
}
PowerUpload.php
/*
** @description If you can, I would extend the Upload class and save some more readable methods to it
*/
class PowerUpload extends Upload
{
public function setNewFile($fileArray)
{
$this->fileArray = $fileArray;
return $this;
}
public function setFileName($name)
{
$this->file_new_name_body = $name;
return $this;
}
public function checkHasUploaded()
{
$files = (!empty($this->fileArray));
if(!$files)
throw new \Exception("File array can not be empty.");
return $files;
}
public function hasProcessed()
{
return $this->processed;
}
public function processUpload($path)
{
$this->process($path);
}
public function getError()
{
return $this->error;
}
}
根据变化修改后的脚本:注意,表单输入名称具有实际键值,所以你可以用更简单的名称中的文件相匹配。
# Create the upload observer, $con is your database connection
$Uploader = new UploadObserver($con);
# If there are files uploading
if($Uploader->filesSet()) {
# Create a loop from the new array
# Keep the $key value to match it up with the files value
foreach($Uploader->getFiles() as $key => $file) {
# Use a try because the uploader submits exceptions on failures
try {
# Set the name
$newname = $t_timestamp."_".$key;
# Tag on the extension
$filename = $newname.".jpg";
# Create the upload instance
$handle = $Uploader->upload($file);
# This will throw exception if failed
$handle->checkHasUploaded();
# Assign the file name and upload the file to folder
$handle->setFileName($newname)->processUpload(realpath(__DIR__.'/..').'/uploaded/');
# Save the file info to your database using the connection passed originally
# You should throw exception here on database fail, PDO does it automatically so it's caught
$Uploader->saveToDb(array($_POST['photo_title'][$key],$filename,$aid));
# Create javascript view
echo $Uploader->getSuccess();
}
# Catch error(s) from the non-db classes
catch(\Exception $e) {
echo 'Error: '.$e->getMessage();
}
# Catch error(s) from the db classes
catch(\PDOException $e) {
echo 'Error: '.$e->getMessage();
}
}
}
?>
<form method="post" enctype="multipart/form-data" action="<?php echo $this->getDataNode('_SERVER')->REQUEST_URI ?>">
<table>
<tr>
<td><input class="form-control" id="customFieldValue2" name="photo_title[1]" type="text"></td>
<td><input class="form-control" id="customFieldName2" name="image_field[1]" type="file"></td>
</tr>
<tr>
<td><input class="form-control" id="customFieldValue2" name="photo_title[2]" type="text"></td>
<td><input class="form-control" id="customFieldName2" name="image_field[2]" type="file"></td>
</tr>
</table>
<input type="submit" value="SAVE" />
</form>
感谢您的指出,我已编辑的代码。现在它有一个较少的循环。但是我仍然无能为$ _POST ['photo_title']创建数组。 – musaspot