无限图像上传到服务器&名称,类别等&多个图像路径(1行)到db

时间:2013-11-02 18:21:47

标签: php mysql image file-upload

我需要一个目录脚本。我到处都看,但我似乎找不到合适的东西。我需要将多个(无限制)图像上传到服务器,同时将产品名称,描述等和多行图像路径保存在一行中。

如果可能,我希望使用此功能无限制地上传图片 - >

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

图片上传

  • 文件最大尺寸:2048kb
  • 文件类型:jpg / png / gif
  • 上传文件夹:[category] ​​/ image [random_number] .jpg

数据库

产品

  • ID
  • 产品
  • 描述
  • 类别
  • 材料
  • 颜色
  • 尺寸
  • 股票
  • ws_price
  • rt_price
  • multiple_image_path

分类

  • CAT_ID
  • PARENT_ID
  • 类别
  • 描述

文件

  • add_product.php
  • view_product.php
  • add_category.php
  • db_connect.php
  • upload.php的
  • 的.sql

如果您知道已经为此或类似设置的任何脚本,并且只需要稍微调整一下,那么如果您可以向我发送链接会很棒。 :)

之前我已经整理了一个文件上传脚本,但它非常草率,我不得不分开上传文件[]。这是很久以前的旧版本:

    <?php
    $con = mysql_connect("localhost","","");
    if (!$con) { die('Could not connect: ' . mysql_error());  }
    mysql_select_db("test", $con);

    $random_digit = rand(0000,9999);
    $target = 'images/';

    $px = array();
    echo "<ul>";
    for($i = 0; $i < 10; $i ++ )
    {
       $px[$i] = "";
       $pic[$i+1] = ($_FILES["image" . ($i + 1)]["name"]);

       if (empty($pic[$i + 1]))
       {
          echo "<li>IMG $i + 1 EMPTY </li>";
       }
       else
       {
          $px[$i] = $random_digit . $pic[$i + 1];
          $px[$i] = preg_replace('/\s+/','_', $px[$i]);
          $px[$i] = str_replace('&', 'and', $px[$i]);
          $u = $target . $px[$i];
          $result = move_uploaded_file($_FILES['image' . ($i + 1)]['tmp_name'], $u);
          echo "<li>IMG " . ($i + 1) . " Uploaded</li>";
       }
    }

    echo "</ul>";

     $itemnumber=mysql_real_escape_string($_POST['itemnumber']); 
     $product=mysql_real_escape_string($_POST['product']); 
     $category=mysql_real_escape_string($_POST['category']); 
     $color=mysql_real_escape_string($_POST['color']); 
     $style=mysql_real_escape_string($_POST['style']); 
     $material=mysql_real_escape_string($_POST['material']); 
     $sizes=mysql_real_escape_string($_POST['sizes']); 
     $stocks=mysql_real_escape_string($_POST['stocks']); 
     $orders=mysql_real_escape_string($_POST['orders']); 
     $wsprice=mysql_real_escape_string($_POST['wsprice']); 
     $rtprice=mysql_real_escape_string($_POST['rtprice']); 
     $profit=mysql_real_escape_string($_POST['profit']); 
     $itemcode=mysql_real_escape_string($_POST['itemcode']); 
     $suppliers=mysql_real_escape_string($_POST['suppliers']); 
     $dc=mysql_real_escape_string($_POST['dc']); 

    $sql = "INSERT INTO `items` (itemnumber, product, category, color, style, material, sizes, stocks, orders, wsprice, rtprice, profit, itemcode, suppliers, dc, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10) VALUES ('$itemnumber', '$product', '$category', '$color', '$style', '$material', '$sizes', '$stocks', '$orders', '$wsprice', '$rtprice', '$profit', '$itemcode', '$suppliers', '$dc', '$px[0]', '$px[1]', '$px[2]', '$px[3]', '$px[4]', '$px[5]', '$px[6]', '$px[7]', '$px[8]', '$px[9]')"; 

    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "<ul>";
        echo "<li>Item #: " . $itemnumber . "</li>";
        echo "<li>Product: " . $product . "</li>";
        echo "<li>Category: " . $category . "</li>";
        echo "<li>Style: " . $style . "</li>";
        echo "<li>Material: " . $material . "</li>";
        echo "<li>Sizes: " . $sizes . "</li>";
        echo "<li>Stocks: " . $stocks . "</li>";
        echo "<li>Orders: " . $orders . "</li>";
        echo "<li>WS Price: " . $wsprice . "</li>";
        echo "<li>RT Price: " . $rtprice . "</li>";
        echo "<li>Profit: " . $profit . "</li>";
        echo "<li>Item Code: " . $itemcode . "</li>";
        echo "<li>Suppliers: " . $suppliers . "</li>";
        echo "<li>DC: " . $dc . "</li>";
    echo "</ul>";

    mysql_close($con);
    ?>

报名表

    <form enctype="multipart/form-data" action="add.php" method="POST"> 
    <input type="hidden" name="id" value="" /> 

    <table border="0" cellpadding="3">
    <th colspan="3">Shop Item Entry</th>
    <tr><td>Item # <input type="text" name="itemnumber" size="27" value="01-00000">                  </td>
    <td>Product <input type="text" name="product" size="100"></td>
    </tr>
    <tr>
    <td>Category <?php
    $con = mysql_connect("localhost","","");
    if (!$con) { die('Could not connect: ' . mysql_error());  }
    mysql_select_db("test", $con);
    function finddepth($byid) {
    $re_query=mysql_query("SELECT * FROM categories where parent_id='$byid'");
    $num_rows = mysql_num_rows($re_query); //echo " Has children:".$num_rows."<br>";
    if(mysql_num_rows($re_query) > '0') {
while($recursive_rw=mysql_fetch_array($re_query)){
    $p=$recursive_rw['category_id'];
    $catnm=$recursive_rw['title'];
    $parent=$recursive_rw['parent_id'];
    $pre="- - ";
if($parent=="0") {
    ?>
<option><?php echo $catnm; ?></option>
    <?php
        } else {
    echo '<option value="'.$catnm.'">'.$pre.$catnm.'</option>' ;
        }
    finddepth($p);
        }//end while
    }//end if
    }//end of function
    ?><select name="category">
    <option values=" ">    </option>
    <?php finddepth(0); ?>
    </select>  
</td>
<td>Color <input type="text" name = "color"size="45">&nbsp;&nbsp;&nbsp;Style <input type="text" name = "style" size="45"></td>
    </tr>
    <tr>
<td> Material <input type="text" name = "material" size="25"></td>
<td>
    Sizes <input type="text" name = "sizes" size="46">&nbsp;&nbsp;&nbsp;Stocks                 <input type="text" name = "stocks" size="15">&nbsp;&nbsp;&nbsp;Orders <input type="text" name = "orders" size="14">
</td>
    </tr>
    <tr>
<td  colspan="3">
    WS Price <input type="text" name = "wsprice" size="24">&nbsp;RT Price         <input type="text" name = "rtprice">&nbsp;&nbsp;&nbsp;Profit <input type="text" name = "profit">&nbsp;&nbsp;&nbsp;DC: <input type="radio" name="dc" value="Yes"> Yes <input type="radio" name="dc" value="No" checked> No 
</td>
    </tr>
    <tr>
<td> Item Code: <input type="text" name = "itemcode" size="22"></td>
<td>Suppliers: <input type="text" name = "suppliers" size="100"></td>
    </tr>
    <tr>
<td colspan="3">
    <input size="25" name="image1" type="file" />
    <input size="25" name="image2" type="file" />
    <input size="25" name="image3" type="file" />
    <input size="25" name="image4" type="file" />
    <input size="25" name="image5" type="file" />
    <input size="25" name="image6" type="file" />
    <input size="25" name="image7" type="file" />
    <input size="25" name="image8" type="file" />
    <input size="25" name="image9" type="file" />
    <input size="25" name="image10" type="file" />
    </td>
    </tr>

    </table>
     <input type="submit" name="submit" value="Add"> 
     </form>

这个工作正常,但是所有“上传文件”的东西都很混乱,如果可能的话,还可以添加无限图片上传的功能,而不只是10,因为有些产品还有更多功能超过10张图片需要。任何事情都可以提供帮助,所以如果你能帮我解决这两个文件,我将不胜感激。谢谢!

*任何人都可以告诉我如何在一行中保存多个数据(图像路径)。

例如:img_path:“img1.jpg,img2.jpg,img3.jpg”

   $img_path = implode(',', $_POST['img[]']); 

   INSERT INTO products (id, product, description, img_path)
   VALUES ('$id',$product','$description','$img_path')

这会有用吗?我还没有去测试它。

1 个答案:

答案 0 :(得分:4)

您可以查看jQuery文件上传:http://blueimp.github.io/jQuery-File-Upload/ 对于'基本'文件上传,你几乎只需要添加js-libs和一些回调。非常简单,请查看示例以获取详细信息。此外,如果需要,您可以轻松地随后采用随附的酷炫进度条等功能。

对于服务器端代码,这里有一个非常干净的PHP实现:https://github.com/Gargron/fileupload