dongxia2068 2016-10-04 17:13
浏览 35
已采纳

从php脚本存储信息

I m working on an html page that contains a form allowing users to enter their informations and upload files. all informations will be inserted in Mysql database.

in Javascript, im using XMLHttpRequest to send the files to the server and "upload.php" to rename (to avoid dupplicated names) and move them in the upload directory.
For better user experience, this will be done before submitting the whole form.

My question is : How can i store the new filenames (defined in upload.php)to use them in the form submission "submit.php"?
the reason for this is that in "submit.php", i insert first the user informations in "user" table and then select the "user_id" (auto increment) that will be inserted with filenames in the "files" table.

Could php sessions be an approach to do this ? is there another way? Thanks for your help

html:

<form action="submit.php" method="post" id="submitform">
<div>
<--!user info part1-->
</div>
<div id="filesContainer" class="eltContainer">
    <input type="file" id="filesList" multiple>
</div>
<div>
<--!user info part2-->
</div>

javascript:

var fd = new FormData()
var xhr = new XMLHttpRequest()
for (var i=0,nb = fichiers.length; i<nb; i++) {
    var fichier = fichiers[i]
    fd.append(fichier.name,fichier)
}
xhr.open('POST', 'upload.php', true)

upload.php :

<?php 
foreach($_FILES as $file){
    $filename = date('Y') . date('m') . date('d') . date('H') . date('i') . basename($_FILES[$file]['name']);
    move_uploaded_file( $file['tmp_name'],"../upload_dir/" .$filename);  
    }
exit;
  • 写回答

1条回答 默认 最新

  • dongzanghua8422 2016-10-04 17:18
    关注

    I would consider using something like md5 for unique filenames.

    Nevertheless you can push filenames into some array, and than return those filenames, as a result of post request, and put them back into some input field.

    To retrieve the response simply add this lines to your code below open

    xhr.onreadystatechange = function {
    // If the request completed and status is OK
      if (req.readyState == 4 && req.status == 200) {
        // keep in mind that fileNames here are JSON string
        // as you should call json_encode($arrayOfFilenames)
        // in your php script (upload.php)
        var fileNames = xhr.responseText; 
      }
    }
    

    If you'd like consider using a simple library for AJAX requests, like axios. It's promise based HTTP client for the browser, really simple to use and saves you some time and effort cause you don't have to memorize all this stuff you and I have just written.

    This is one approach, but I think you can use $_SESSION as well, and it's perfectly valid. My guess is you don't have logged in user at this point, so my idea is as follows:

    • put filenames into the $_SESSION

    • use db transactions - as @Marc B suggested - to connect files with
      user

    • if there were no errors just remove filenames from $_SESSION, if there was some, just redirect the user back to the form (possibly with some info what went wrong), and this way he doesn't have to reupload files, cause you have filenames still in $_SESSION

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

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算