douluan5738 2009-05-29 20:34
浏览 46
已采纳

保持上传的文件跨多个页面

I have a page that needs to accept .csv files with no predefined layout. That is, the user will choose what kind of data is in each column.

Right now, the upload file form sends me to a .php script which can access the uploaded file. It reads it, and lets the user assign categories to each of the columns. My next step is to copy the data from the .csv file to the database, but I'm not sure how to keep the file from being deleted from the temporary directory, I still need it on the next page where I do the actual copying. Also, I'm not interested in copying over the file to a different location on the server, it seems like a waste, and would be a hassle to do properly while allowing for concurrency.

How do I prevent the file from being deleted, or, alternatively, what's a better solution to this problem?

  • 写回答

2条回答 默认 最新

  • doujiao9574 2009-05-29 20:48
    关注

    Well, I'm not sure this is maintainable without moving the uploaded file.

    Personally, I would implement it thusly:

    On the page that receives the uploaded file:

    //assuming it hasn't already been started...
    session_start();
    
    //generate a unique filepath
    $filepath = PATH_TO_THE_DIR . "/" . time() . 
    "_" . md5_file($_FILES['formelement']['tmp_name']) . ".csv";
    
    //move the uploaded file
    move_uploaded_file($_FILES['formelement']['tmp_name'], $filepath);
    
    //do whatever you need with the file, now at $filepath
    
    //now store the filepath in the session:
    $_SESSION['uploaded_filepath'] = $filepath;
    
    session_write_close();
    

    On the second page, when you want to do more processing:

    //assuming it hasn't already been started...
    session_start();
    
    $filepath = $_SESSION['uploaded_filepath'];
    
    // do whatever you need to with the file
    
    unlink($filepath);
    
    unset($_SESSION['uploaded_filepath']);
    
    session_write_close();
    

    That should be fine - obviously you would need to check permissions on the PATH_TO_THE_DIR constant, which will just be a directory somewhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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