用php和sql上传多个图像 [英] Upload multiple images with php and sql
问题描述
我是新来的PHP,我正在尝试制作一个多图像上传。我的表单看起来像这样:
< form action =method =postenctype =multipart / form-data >
< p class =style2> Izberi datoteko:< / p>< input type =filename =imagemultiple =multiple/>< / p>
< p class =style2>
Izberi专辑:< br />
< select name =album_id>
<?php
foreach($ albums as $ album){
echo'< option value =',$ album ['id'],'>',$相册['name'],'< / option>';
}
?>
< / select>
< / p>
< p class =style2>< input type =submitvalue =NaložiSlike/>< / p>
< / form>
而在PHP方面,我做了类似的事情:
if(isset($ _ FILES ['image'],$ _POST ['album_id'])){
$ image_name = $ _FILES ['image'] [ '名称'];
$ image_size = $ _FILES ['image'] ['size'];
$ image_temp = $ _FILES ['image'] ['tmp_name'];
$ allowed_ext = array('jpg','jpeg','png','gif');
$ a = explode('。',$ image_name);
$ image_ext = strtolower(end($ a));
$ album_id = $ _POST ['album_id'];
$ errors = array();
if(empty($ image_name)|| empty($ album_id)){
$ errors [] ='< p class =style2> Nekaj manjka! p为H.';
} else {
if(in_array($ image_ext,$ allowed_ext)=== false){
$ errors [] ='< p class =style2> ; Izbrani tip datoteke ni dovoljen!< / p>';
}
if($ image_size> 2097152){
$ errors [] ='< p class =style2> Izbrana datoteka je prevelika! Maksimalna velikost datoteke mora biti 2mb!< / p>';
$ b $ if(!empty($ errors)){
foreach($ errors as $ error){
echo $ error;
}
} else {
upload_image($ image_temp,$ image_ext,$ album_id);
header('Location:view_album.php?album_id ='。$ album_id);
exit();
我创建了一个函数,图像到数据库中。这个函数看起来像这样:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' (INT)$ album_id;
$ b mysql_query(INSERT INTO`images` VALUES('','。$ _SESSION ['user_id']。','$ album_id',UNIX_TIMESTAMP(),'$ image_ext') );
$ image_id = mysql_insert_id();
$ image_file = $ image_id。'。'。$ image_ext;
move_uploaded_file($ image_temp,'galerija /'.$ album_id。'/'。$ image_file);
create_thumb('galerija /'.$ album_id。'/',$ image_file,'galerija / thumbs /'.$ album_id。'/');
这只适用于上传一张图片。我试图创建一个窗体多个选择文件,我可以选择更多的图像,但在数据库中只获得第一个图像。我试图在我的脚本中放入 foreach()
,但是我不知道在哪里以及如何放置它。
所以我怎样才能改变脚本可以上传更多的图片到我的数据库?我只是无法弄清楚。任何帮助将是伟大的!
PHP将不会看到你的多个文件像这样
替换
< input type =filename =imagemultiple =multiple/>
< input type =filename =image []multiple =multiple/>
要循环这些文件,您需要像
foreach($ _FILES ['image'] ['tmp_name'] as $ key => $ val){
$ fileName = $ _FILES [ '图像'] [ '名'] [$关键]
$ fileSize = $ _FILES ['image'] ['size'] [$ key];
$ fileTemp = $ _FILES ['image'] ['tmp_name'] [$ key];
$ fileExt = pathinfo($ fileName,PATHINFO_EXTENSION);
$ fileExt = strtolower($ fileExt);
//继续
}
I am new in php and I am trying to make a multiple image uploader. My form looks like this:
<form action="" method="post" enctype="multipart/form-data">
<p class="style2">Izberi datoteko: <br /><input type="file" name="image" multiple="multiple" /></p>
<p class="style2">
Izberi Album: <br />
<select name="album_id">
<?php
foreach ($albums as $album) {
echo '<option value="', $album['id'] ,'">', $album['name'] ,'</option>';
}
?>
</select>
</p>
<p class="style2"><input type="submit" value="Naloži Slike" /></p>
</form>
And on the php side I did something like:
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$a = explode('.', $image_name);
$image_ext = strtolower(end($a));
$album_id = $_POST['album_id'];
$errors = array();
if (empty($image_name) || empty($album_id)) {
$errors[] = '<p class="style2">Nekaj manjka!</p>';
} else {
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = '<p class="style2">Izbrani tip datoteke ni dovoljen!</p>';
}
if ($image_size > 2097152) {
$errors[] = '<p class="style2">Izbrana datoteka je prevelika! Maksimalna velikost datoteke mora biti 2mb!</p>';
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error;
}
} else {
upload_image($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit();
}
}
I created a function that puts the image into database. This function looks like this:
function upload_image($image_temp, $image_ext, $album_id) {
$album_id = (int)$album_id;
mysql_query("INSERT INTO `images` VALUES ('', '". $_SESSION['user_id'] ."', '$album_id', UNIX_TIMESTAMP(), '$image_ext')");
$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'galerija/'.$album_id.'/'.$image_file);
create_thumb('galerija/'.$album_id.'/', $image_file, 'galerija/thumbs/'.$album_id.'/');
}
This works fine for uploading only one image. I tried to create a form for multiple select files and i can select more images but in database only gets first image. I tried to put foreach()
in my script but I don t know where and how I must put it in.
So how can i change my script so I can upload more images into my database? I just can't figure it out. Any help would be great!
A. PHP would not be albe to see your multiple files like that
Replace
<input type="file" name="image" multiple="multiple" />
With
<input type="file" name="image[]" multiple="multiple" />
To loop over this files you need something like
foreach ( $_FILES['image']['tmp_name'] as $key => $val ) {
$fileName = $_FILES['image']['name'][$key];
$fileSize = $_FILES['image']['size'][$key];
$fileTemp = $_FILES['image']['tmp_name'][$key];
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
$fileExt = strtolower($fileExt);
// Continue
}
这篇关于用php和sql上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!