语言:PHP/MySQL的阵列,上传图片,然后插入到数据库(PHP,MySQL的)
我会在我心里,我真的现在问...我有多文件上传表单:
<input type="file" name="fileupload[]" multiple>
随着一些JavaScript的帮助下,在此输入的每处更改,其追加另一个里面输入文件名列表,+一个格式化字符串(从文件名抓起) ,所以我们有布局如下图所示(假设我们只是增加了一些图像):
几乎类似:http://jsfiddle.net/pxfunc/WWNnV/4/
//一个HTML这种布局的表示是...(假设我们加入3个图像)
<input type="file" name="fileupload[]" multiple>
- 图像名称1.JPG
<input type="text" value="Image Name 1" name="keyword[]">
- justsome_file.png
<input type="text" value="Justsome File" name="keyword[]">
- some_Img-031.gif
<input type="text" value="Some Img 031" name="keyword[]">
<input type="submit" value="Upload">
我这样说是因为除了上传文件,我也想吨o将它们添加到我的数据库中,其默认标题基于其文件名(以及在我上传它时为每个图像设置/更改此标题的选项)。我的表单没有问题。
问题:我的两难困境在于提交表单数据/动作的PHP页面。
我只能设法之一:
- 上传正确的图像,但得到相同的标题为所有
- 插入正确的头衔,但得到相同的图像对所有
这里是我的PHP动作页面:(目前正在上传正确的图片,但是相同标题为)
<?php
// CONNECT TO DATABASE...
// INCLUDE UPLOAD CLASS LIBRARY
include (dirname(__FILE__).'/lib/class.upload.php');
$files = array();
foreach ($_FILES['fileupload'] as $k => $l)
{
foreach ($l as $i => $v)
{
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
$imagename = $_POST['keyword'][$i];
}
}
// create an array here to hold file names
$uploaded = array();
foreach ($files as $file)
{
$generate_name = rand(100,99999);
$generate_name_extra = rand(200,9999);
$filenamex = "COVER_PHOTO_".$generate_name.$generate_name_extra."_".time();
$filenamex_thumb = $filenamex."_thumb";
$handle = new upload($file);
if ($handle->uploaded) {
$this_upload = array();
///// 1 ////////////////////////////////////////////////////////////////////
$handle->file_new_name_body = $filenamex_thumb;
$handle->file_force_extension = true;
$handle->image_resize = true;
$handle->image_x = '300';
$handle->image_ratio_y = true;
$handle->jpeg_quality = '100';
// ABSOLUTE PATH BELOW
$handle->process($absoRoot.'covers/thumbs/');
////////////////////////////////////////////////////////////////////////////
if ($handle->processed) {
// store the image filename
$this_upload['image'] = $handle->file_dst_name; // Destination file name
$this_upload['body'] = $handle->file_dst_name_body; // Destination file name body
$this_upload['extension'] = $handle->file_dst_name_ext; // Destination file extension
$category_id = $_POST['cat'];
$hiddenvalues = explode ("|",$_POST["cat"]);
$category = $hiddenvalues[0];
$category_name = $hiddenvalues[1];
$sql = 'INSERT INTO cover (id, img, keyword, category_name, cat_id) VALUES ("", "'.$this_upload['image'].'", "'.$imagename.'", "'.$category_name.'", "'.$category.'")';
mysql_query($sql);
}
$handle->clean();
header("Location: ./upload.php");
$message = "";
} else {
echo ' file not uploaded to the wanted location';
echo ' Error: ' . $handle->error . '';
}
} ?>
(我用的是Upload Class by Colin Verot来处理图像上传,和他们的FAQ教程上this page, under: What about multiple uploads?处理多个图片上传)
这将工作完美的,如果我只是在上传图片,但我加添加的功能每个图像数据到我的数据库。 &这是令人困惑的地方。
我确定关键是将SQL查询放在正确的foreach中,或者可能创建另一个SQL查询,但我试过&它只给我1个好的结果,无论是图像上传还是标题,从来没有为两个。
我需要将图像上传到网站,然后将其数据(包括图像路径)存储到我的数据库。
请查看我的代码并启发我如何解决此问题?一段片段的线索现在真的很棒,因为在尝试了所有我能想到的之后,我已经非常困惑。非常感谢!
清理块的格式并更新你的文章:'if(!array_key_exists($ i,$ files))' –
我很不确定该怎么做,这段代码来自Upload Class FAQ教程在[这个页面上,在:**多重上传怎么办?**](http://www.verot.net/php_class_upload_faq.htm?PHPSESSID=accbee5eb0a694348a445a61a4e40ba1)我真的不知道哪一个是来自该代码块,但我曾尝试更改它,上传停止工作,所以我只是把它留在那里。然而,我加入了这个部分:'$ imagename = $ _POST ['keyword'] [$ i];' – Mafia
**更新:**我从[**用户:138383 ** ,** Aaron W。**](http://stackoverflow.com/users/138383/aaron-w)他的评论是第一个,也是我尝试过的第一个,但我无法标记它因为他删除了它,所以再纠正一下。我已经通过一些小的方式通知他了,如果他想重新提交,我会等待,然后我会将他的回答标记为正确。 [**(他的回答是......)**](http://i4.photobucket.com/albums/y143/cute-spot/Animatiofgdn6.gif)只是为了表扬他。当它工作时我几乎死亡。 :) – Mafia