从多个上传表单阵列,上传图片,然后插入到数据库中(PHP,MySQL的)
语言: PHP / MySQL的
Language: PHP / MySQL
我要在我心里,我真的现在要问...我有一个多文件上传表单:
I am going out of my mind, I really have to ask now... I have a multiple file upload form:
<输入类型=文件名称=文件上传[]多>
随着一些JavaScript的帮助下,到该输入的每次变更,追加另一里面输入文件名列表,+一个格式化字符串(从文件名中抢下),这样的onchange我们有一个布局如图所示以下(假设我们只是增加了一些图像):
With the help of some Javascript, on each change made to this input, it appends a list of filenames, + a formatted string (grabbed from the filename) inside another input, so onchange we have a layout as shown below (assuming that we just added some images):
几乎类似: http://jsfiddle.net/pxfunc/WWNnV/4 /
//一个HTML重新$这种布局的对$ psentation会... (假设我们增加了3张图片)
// An HTML representation of such layout would be... (assuming that we added 3 images)
<输入类型=文件名称=文件上传[]多>
- 图像名称1.JPG 
<输入类型=文本VALUE =图片名称1的名字=关键字[]>
- justsome_file.png 
<输入类型=文本VALUE =Justsome文件NAME =关键字[]>
- some_Img-031.gif 
<输入类型=文本值=一些图031NAME =关键字[]>
- image-name-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[]">
&LT;输入类型=提交值=上传&GT;
的我有这种方式,因为除了上传文件,我也想将它们添加到我的数据库,具有默认标题基于它的文件名(以及设置选项/更改该标题为每个图像我上传)。还有我的表没有问题。的
I have it this way because aside from uploading the files, I would also like to add them to my database, with a default title based on its filename (and the option to set/change this title for each image as I upload it). There is no problem with my form.
问题:我的困境就在于那里的表单数据/动作提交的PHP页面内
PROBLEM: My dilemma lies inside the PHP page where the form data/action is submitted.
我只能设法之一:
- 上传正确的图像,但得到相同的标题为所有
- 插入正确的头衔,但得到相同的图像的所有
这是我的PHP操作页面:(目前上传图像正确,但有相同的标题作为全部)
Here is my PHP action page: (Currently uploading correct images, but having same title for all)
<?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 . '';
}
} ?>
(我用的是 上传类由Colin Verot 来处理图像上传,和他们的FAQ教程来处理的此页,下:?什么多个上传 )
(I use the Upload Class by Colin Verot to handle image uploads, and their FAQ tutorial to handle MULTIPLE image uploads on this page, under: What about multiple uploads?)
这将工作完美的,如果我只是上传图片,但我加了将每个图像数据到我的数据库的功能。 &安培;这是它变得混乱。
我敢肯定,关键是把SQL查询正确的foreach里面,或者使一个又一个,但我已经试过了&放大器;它只是给了我1的好成绩对于任何一个图片上传或标题,永远不会为。
I'm sure the key is placing the SQL query inside the right foreach, or perhaps making another one, but I've tried that & it only gives me 1 good result for either the image upload or the title, never for both.
我需要将其图像数据(包括映像路径)上传到网站,然后将其存储到我的数据库。
I need to upload the image to the site, then store its data (including image path) to my database.
请看看我的code和赐教如何解决这个问题呢?一个片段线索真的,现在的我已经尝试过所有我能想到的后非常困惑是巨大的。太谢谢你了!
Please look into my code and enlighten me how to solve this problem? A snippet clue would really be great for now as I am already very confused after having tried all I could think of. Thank you so much!
当你收集你覆盖文件信息 $ imagename
在每个循环,这样它会被分配给最后一个。尝试将其连接到 $文件
变量(希望这不会乱用上传
类,你正在使用)
When you're gathering the file information you're overwriting $imagename
on every loop so it will be assigned to the last one. Try attaching it to the $files
variable (hopefully this doesn't mess with the upload
class you're using).
foreach ($l as $i => $v)
{
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
$files[$i]['imagename'] = $_POST['keyword'][$i];
}
然后更新您的 $ SQL
字符串引用
$sql = 'INSERT INTO cover (id, img, keyword, category_name, cat_id)
VALUES ("", "'.$this_upload['image'].'", "'.$file['imagename'].'",
"'.$category_name.'", "'.$category.'")';