doulu3399 2016-06-24 21:54
浏览 38

PHP上传多个文件并将其路径保存到DB中的单个行中

i been looking around and didnt found a solution that fits my needs. so lets cut to the chase.

SCENARIO: User will fill some text forms, the input of those forms will be stored into a database table (the text input are working and stores into database just fine), in the same form the user will upload 7 files and the paths of the files will be stored in the same table in diferent columns say image1 image2 image3 etc, but in the same row, so when the id of that row is needed the file paths will be retrieved also but im unable to build the query to insert the paths into database, ive managed store only one file and store its path into the database but im struggling with the multiple files, i just dont know how to check if the for loop has finished and how to build the query to insert all upladed file's paths into a single row in their respective columns.

Server is running PHP and postgreSQL

thanks in advance and hope you can help me!

  <?php
  include '../include/dbase.php';
  $result = '';
  if (isset($_FILES['upload'])) {
   $nombre = $_FILES['upload']['name'];
   $ntemporal = $_FILES['upload']['tmp_name'];
   $tipo = $_FILES['upload']['type'];
   $tamaño = $_FILES['upload']['size'];
   $error = $_FILES['upload']['error'];
   $file_name_all = '';
    for($i=0; $i<count($_FILES['upload']['name']); $i++)
     {
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
           if ($tmpFilePath != "")
             {
               $path = "../imagenes/propiedades/";
               $name = $_FILES['upload']['name'][$i];
               $size = $_FILES['upload']['size'][$i];

               list($txt, $ext) = explode(".", $name);
               $file= time().substr(str_replace(" ", "_", $txt), 0);
               $info = pathinfo($file);
               $filename = $file.".".$ext;
               if(move_uploaded_file($_FILES['upload'['tmp_name'][$i],   $path.$filename))
               {
                  //my guess is an if check here to see if for 
         loop has finished all the uploads, and if returns true a try and 
      catch with the sql query to upload  only the path of each file the 
       row containing all other user input text.
               }
         }
           }
           }
          ?>
  • 写回答

2条回答 默认 最新

  • drddx3115 2016-06-24 22:25
    关注

    It's better to save the file in a directory like files/$id_user/.. and the names in the database.

    You can make a table for files; table files with id_user + file_name for example.

    If you want all in a same table, you can put all the file's names separated with comma or ; and after select them and explode for have them in array..

    In your code, it's better to use ' than ", because php will try to find variables inside the " " .. and lost time for nothing...

    $filename = $file.'.'.$ext; $filename = $file.".".$ext;;
    

    If you write:

    $test = 'hello';
    echo 'i tell you $test'; => i tell you $test
    echo "i tell you $test"; => i tell you hello
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测