doujunchi1238 2016-03-17 14:31
浏览 48

在整个页面中获取并使用用户以前的表单数据变量

The purpose is to allow user uploading files (usual uploading process) and confirm uploading by pressing on the confirmation button. Programming side: 2 folders - 1 for unconfirmed files where files get deleted periodically and 2 - confirmed folder - where files are copied from unconfirmed folder if a user presses the confirmation button.

Basically, I have a form, where a user uploads files that are stored in the folder and the path to it - in a database. And on the same page I have another button, so when the user presses it I would like to move his/her uploaded files from one directory to another (not re-uploading - coping on server).The issue is taht I should know WHICH files to deal with!

My problem is: everything seems ok, but I can't get the variable $name - that tells me the name of the file, user has uploaded. Because When I try to use it to proceed second submit button - it says variable is undefined. I need to get the variable that tells me the name of the file user has submitted so I can copy that file to another directory, but it can only be assigned (and unfornunatelly used) when procesing the form submittinf and enabled to use only inside of the processing, while I want to use it outside.

I have tried to use sessions - but this code doesnt work, same problem - it says that - 'undefined index - regex', same with COOKIES.

All the html:

<form action="videator.php"method="post" enctype="multipart/form-data">
       <input class="form" type="file" name="fileToUpload" id="fileToUpload"    accept="video/*" >
       <input class="form" type="submit"  value="Upload Image" name="submit">
</form>
<form action="love.php" method="post">
<input class="post" type="submit" value="POST" name="post" id="post"/>
</form>

videator.php:

if(isset($_POST["submit"])) {
$file = $_FILES['fileToUpload'];
$name = $file['name'];
$_COOKIE['name'] = $name;
$_SESSION['regex'] = $name;
...
}

love.php

if(isset($_POST["post"])) {
session_start();
$name = $_GET['regex'];
$from = "temp_videos/".$name;
$to = "videos/";

if (!copy($from, $to)) {
echo("<script>alert('fail')</script>");
}else {
echo("<script>alert('Success!')</script>");
}

It says also that 'The first argument to copy() function can not be a directory. I guess, that's due to the fact that my variable $name is undefined, therefore only the folder is here as the path.

Please, help. I've spent all my day on that.

(If I just use copy() function with indicating actual path and not a variable of it - everything works) Thanx in advance.

  • 写回答

1条回答 默认 最新

  • donglanfu5831 2016-03-17 15:27
    关注

    I have a few tips that may help.

    (1) At the top of each PHP file, as the first instruction, put:

    <?php
        if (!session_id()) session_start();
        //any other PHP instructions follow.
    ?>
    

    (2) Try using the excellent jQuery File Upload plugin by Ravi Kusuma. Although I generally try to code everything myself and not use plugins, this one is so well done that it's my primary goto for file uploads.

    (3) Until you get this working, there is no need to put the session_start() inside an if statement. The session_start() must be the first instruction at top of each PHP page that references the $_SESSION variable.

    (4) Look into AJAX instead of using <form> (for managing the file upload process). Kusuma's plugin also works great with AJAX. If you haven't used AJAX before, it may sound intimidating - but it's super simple. Copy the examples at the bottom of this post and make them work on your system. It may be the most important 15 mins you've spent in 2016. Note that the point of the linked question is that there must be a separate PHP file that receives the AJAX post.

    (5) Don't forget that AJAX is a two-step process: (a) the javascript on the current page communicates with the specified PHP page; (b) the PHP page receives the data (or file upload data) via POST varibles. Therefore, be sure to check out the "SERVER SIDE" tab at top of Kusuma's Hayageek page. At the top of the SERVER SIDE page are four links to sample PHP files. Study the upload.php example.

    Happy coding.

    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析