douzhicong5965 2013-10-24 18:26
浏览 38
已采纳

在MySQL中简单上传多个图像

I'm trying to do a simple upload in my system, just with the multiple="true" input property. Actually, I have 15 file inputs, and they all rename the file name to: 'id.in.the.bd'_'number'.jpg (Ex.: 65_1.jpg). Like this:

HTML

    // inside a form
    <?php for ($x = 1; $x <= 15; $x++) { ?>
       <input name="foto_<?php echo $x; ?>" type="file" id="foto" multiple="true" />
    <?php } ?>

PHP

if ($_GET['action'] == "insert") {


//sql insert


// do a consult to find the $id
$sql = mysql_query("SELECT * FROM imoveis ORDER BY id DESC LIMIT 1", $link); 

$result = mysql_fetch_array($sql, MYSQL_ASSOC);
$id = $result['id'];

for ($x = 1; $x <= 15; $x++) {
   $file = $_FILES["foto_" . $x];
   $path = '../../images/galeria/' . $id . '_' . $x . '.jpg';
   move_uploaded_file($file["tmp_name"], $path);
}

How can I easily change my code to use the multiple="true"? If I just change the input to multiple="true", he just upload the first image selected.

  • 写回答

1条回答 默认 最新

  • donglu1472 2013-10-24 18:32
    关注

    Change the input tags to

    <input name="foto[]" id="foto_1" type="file" />
    

    In PHP these files are accessible by $_FILES['foto']['name'] array, like this:

    $_FILES['foto']['name'][0] for file 1
    $_FILES['foto']['name'][1] for file 2
    

    etc..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名