上传多个文件到服务器与PHP不上传到服务器的所有文件 [英] Upload multiple files to server with php not uploading to server all files

查看:169
本文介绍了上传多个文件到服务器与PHP不上传到服务器的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个问题,我得到了一个表格上传文件,并插入到MySQL的MySQL插入部分工作,但他只移动三个文件中的一个到服务器需要一些帮助,这里是我的代码。
上传表单:

 < form enctype =multipart / form-dataaction =add-exposes .phpmethod =POST>公开名称:< input type =textname =name>< br> Expose Beschreibung:< textarea style =width:810px; height:200pxname =expose_descclass =textarea>< / textarea>< br> Expose Kategorie:< select name =expose_catsize =3> 
<?php
mysql_connect(xxx.de.mysql,xxxx,xxx)或die(mysql_error()); mysql_select_db(handwerker_verb)或死(mysql_error());

$ data = mysql_query(SELECT * FROM cats)或die(mysql_error());
while($ info = mysql_fetch_array($ data)){
echo'< option value ='。$ info ['id']。'>'。info ['cat' ]。 '< /选项>';
}?>


<! - < input type =textname =expose_cat> - >< br> Expose Preis:< input type =textname =price> <峰; br> Bild:< input type =filename =photo>< br> < input type =filename =photo1>< br> < input type =filename =photo2>< br> < input type =submitvalue =Add> < /形式>

添加到服务器和mysql:

 <?php $ target =images /; $ target = $ target。 basename($ _FILES ['photo'] ['name']); 
$ target1 =images /; $ target1 = $ target1。 basename($ _FILES ['photo1'] ['name']);
$ target2 =images /; $ target2 = $ target2。 basename($ _FILES ['photo2'] ['name']);

$ name = $ _ POST ['name']; $ expose_desc = $ _ POST [ expose_desc]; $ expose_cat = $ _ POST [ expose_cat]; $ PIC =($ _ FILES [相片] [名称]); $价格= $ _ POST [价格]; $ PIC1 =($ _ FILES [照片1’ ] [名称]); $ PIC2 =($ _ FILES [照片2’ ] [名称]);
$ b $ mysql_connect(xxxx.de.mysql,xx,xxx)或die(mysql_error()); mysql_select_db(handwerker_verb)或死(mysql_error());
$ b mysql_query(INSERT INTO`显示VALUES('','$ name','$ pic','$ expose_desc','$ expose_cat','$ price','$ pic1' ,'$ pic2'));

if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target)){echoThe file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录; }
elseif(move_uploaded_file($ _ FILES ['photo1'] ['tmp_name'],$ target1)){echoThe 2 file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录; }
elseif(move_uploaded_file($ _ FILES ['photo2'] ['tmp_name'],$ target2)){echoThe 3 file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录; }

else {echo抱歉,上传文件时出现问题。 }?>


解决方案

move_uploaded_file在成功时返回true,到达。
试试这样:

  $ error = false; 

if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target))
{
echoThe file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录;
} else $ error = true;

if(move_uploaded_file($ _ FILES ['photo1'] ['tmp_name'],$ target1))
{
echoThe 2 file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录;
} else $ error = true;

if(move_uploaded_file($ _ FILES ['photo2'] ['tmp_name'],$ target2))
{
echoThe 3 file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录;
} else $ error = true;

if($ error)
echo抱歉,上传文件时出现问题。


Hi at all i got a problem i got a form which uploads files and insert into mysql the mysql insert part is working but he only moves one file of the three to the server need please some help here is my code. The Upload Form:

<form enctype="multipart/form-data" action="add-exposes.php" method="POST">  Expose Name: <input type="text" name="name"><br>  Expose Beschreibung: <textarea style="width: 810px; height: 200px" name="expose_desc" class="textarea"></textarea><br>  Expose Kategorie:<select name="expose_cat" size="3"> 
                        <?php 
                    mysql_connect("xxx.de.mysql", "xxxx", "xxx") or die(mysql_error()) ;  mysql_select_db("handwerker_verb") or die(mysql_error()) ;

                    $data = mysql_query("SELECT * FROM cats") or die(mysql_error());
                    while($info = mysql_fetch_array( $data )) {
                    echo '<option value="'.$info['id'].'">'.$info['cat'].'</option>';
                    } ?>


                         </select><!--<input type="text" name="expose_cat">--><br>Expose Preis:<input type="text" name="price"> <br> Bild: <input type="file" name="photo"><br> <input type="file" name="photo1"><br> <input type="file" name="photo2"><br>   <input type="submit" value="Add">  </form>

Add to server and mysql:

<?php   $target = "images/";  $target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/";  $target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/";  $target2 = $target2 . basename( $_FILES['photo2']['name']);

        $name=$_POST['name'];  $expose_desc=$_POST['expose_desc'];  $expose_cat=$_POST['expose_cat'];  $pic=($_FILES['photo']['name']);  $price=$_POST['price']; $pic1=($_FILES['photo1']['name']); $pic2=($_FILES['photo2']['name']); 

        mysql_connect("xxxx.de.mysql", "xx", "xxx") or die(mysql_error()) ;  mysql_select_db("handwerker_verb") or die(mysql_error()) ;

          mysql_query("INSERT INTO `exposes` VALUES ('', '$name', '$pic', '$expose_desc', '$expose_cat', '$price', '$pic1', '$pic2' )") ; 

          if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  {   echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  } 
          elseif(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))  {   echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  } 
          elseif(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))  {   echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  }

           else {    echo "Sorry, there was a problem uploading your file.";  }  ?>                         

解决方案

move_uploaded_file returns true on success, the remaining elseif are never reached. Try something like this:

$error = false;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  
{   
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true;

if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))  
{   
echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true; 

if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))  
{   
echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true;

if($error)
   echo "Sorry, there was a problem uploading your file.";

这篇关于上传多个文件到服务器与PHP不上传到服务器的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆