【问题标题】:html php file upload sorting / orderhtml php文件上传排序/排序
【发布时间】:2014-01-03 10:50:26
【问题描述】:

我有以下html代码最多可以上传8张图片:

<div class="form-group">
<label class="col-sm-3 control-label">img 1</label>
    <div class="col-sm-6">
        <input type="file" name="img1" />
    </div>
 </div>
 <div class="form-group">
 <label class="col-sm-3 control-label">img 2</label>
     <div class="col-sm-6">
         <input type="file" name="img2" />
     </div>
 </div>
 ...... same for img3,4,5,6,8      

php代码

if (array_key_exists('img1', $_FILES)) {
   if (in_array('image/jpg', $_FILES['img1']) && $_FILES['img1']['error'] == '0') {
      $img1 = "http://www.*.com/tmp/".$findid."_1.jpg";
      $name1 = $findid."_1.jpg";             
      move_uploaded_file($_FILES['img1']['tmp_name'], 'tmp/'.$name1);
   }
} 
...... same for img2,3,4,5,6,8   

<?php 
$images = explode(';', $ad['images']);
if ($images != NULL) {
   foreach ($images as $i => $filename) {
   echo '<a href="'.$filename.'" target="_blank" ><img src="'.$filename.'" width="60px" height="55px" style="float:left; border:1px solid #B1C4DF;" /></a>';
   }
}
?>

$images = implode(";",array_filter(array($img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8)));
  if (isset($_GET['type']) && $_GET['update'] == 'true') {
      if($images != null){
        mysql_query("UPDATE ads SET title ='$title', images ='$images', description ='$description', priceValue ='$priceValue' WHERE id = '" . $_GET['id'] . "'") OR die(mysql_error());
        updateImages($un,$pw,$_GET['id'],$images);
      }else{
        mysql_query("UPDATE ads SET title ='$title', description ='$description', priceValue ='$priceValue' WHERE id = '" . $_GET['id'] . "'") OR die(mysql_error()); 
      }

      header("Location: edit.php?type=edit&id=" . $_GET['id'] . "");
  }

问题是当我只上传 img2 并保存表单时,其他现有图像已从数据库中删除。当我上传 img1 和 img2 时,一切都很好。如何编辑 php 代码,以便仅上传 img3 并保留 img 1 和 img2 ?

【问题讨论】:

  • 您尝试了什么,什么没有奏效?您甚至了解您发布的代码吗?
  • etc for 2,3,4....放下那把霰弹枪!

标签: php html image upload jquery-ui-sortable


【解决方案1】:

首先获取图像数据,然后按如下方式更新详细信息

    $sql = "Select images from ads where id = '" . $_GET['id'] . "'";
    $q   = mysql_query($sql);
    if(mysql_num_rows($q) > 0){
        $res     = mysql_fetch_array($q);
        $imagesd = $res['images'];
        if(!empty($imagesd))
            $images  = $imagesd.';'.$images;
    }

    mysql_query("UPDATE ads SET title ='$title', images ='$images', description ='$description', priceValue ='$priceValue' WHERE id = '" . $_GET['id'] . "'") OR die(mysql_error());

【讨论】:

  • $info = mysql_query("SELECT * FROM info WHERE id = '" . $findid . "'"); $info_details = mysql_fetch_array($ad); $images_ex = explode(';', $info_details['images']); if (empty($img1)) { $img1 = $images_ex[0]; } if (empty($img2)) { $img2 = $images_ex[1]; } 这成功了 :)
【解决方案2】:

可以尝试换行

if (array_key_exists('img1', $_FILES)) {

if (array_key_exists('img1', $_POST)) {

并为您拥有 img2、img3... 等的所有输入执行此操作。

【讨论】:

    【解决方案3】:

    在上传图片之前,您需要验证并检查您上传的是哪张图片:

    试试下面的例子。而不是array_key_exists('img1', $_FILES)

    例如:

    if($_FILES["img1"]["tmp_name"]!='')
    {
    //do uploading //insert,update query
    } 
    
    else if($_FILES["img2"]["tmp_name"]!='')
    {
    //do uploading //insert,update query
    } 
    

    【讨论】:

      【解决方案4】:

      您有 8 个用于上传图片的输入,因此请将其设置为数组。这对您来说更容易和有帮助。当您上传图片时,您需要编写 8 次代码来上传图片,这并不好,所以如果您愿意,可以按照以下方式更改它.

      首先为所有输入创建数组

      <input type="file" name="img[]" />
      

      然后在php中

      foreach($_FILES[ 'img' ][ 'name' ] as $key=>$img)
      {
          if($img!="")
          {
               move_uploaded_file($_FILES['img']['tmp_name'][$key], 'tmp/'.$img);
      
               //and here update code
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-07
        • 2013-02-10
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多