处理表单上传中的tmp文件,并在上传之前将文件传递给其他表单

时间:2013-12-23 12:48:35

标签: php

我需要帮助很严重,无法将其修复为我想要的工作..!

我有两个表单,一个是联系表单,另一个是联系表单中的图片上传,表单使用AJAX进行处理。

my form example

想要move_upload_file,除非提交所有表单(联系人)。到目前为止,当我尝试通过联系表单传递文件并使用@move_upload_file时,它的所有工作, exept 它不会将文件移动到服务器。

我使用这种方式传输文件,当我在做var_dump时,它的工作完美:

 if ($proc == "question")
{
    // Handle some normal form 'contact us'.

    // ... Some proccses code when submited via ajax ...

    $fileElementName = 'ImageBrowse';

 //This session coming from other proccses form down if isset.
  if(isset($_SESSION['imgfilename']) && !empty($_SESSION['imgfilename'])){
     //Then i handle it with list to make the rest of the proccses.
     list($uploadFilename,$_FILES['ImageBrowse']) = $_SESSION['imgfilename'];
     //unset session for security.
     unset($_SESSION['imgfilename']);
    }
   var_dump($_FILES['ImageBrowse']);
   // will print the file array perfect.

   //Trying now to move_uploaded_File 
if(@move_uploaded_file($_FILES['ImageBrowse']['tmp_name'],$uploadsDirectory.$uploadFilename)){
         echo 'Uploaded!';
 }else{
        echo 'Not Uploaded';
 }
 // and here is the problem , it wont move uploaded file , even if there is array of the file..

}
else if ($proc == "sendQuestionUploadImg")
{
    // Handle img upload via ajax.

    $fileElementName = 'ImageBrowse';

    $uploadsDirectory = '../uploads/'; 

    $allowedTypes = array
        (
            'image/jpeg',
            'image/jpg',
            'image/pjpeg',
            'image/pjpeg',
            'image/png',
            'application/x-rar-compressed'
        );

    // ... Some more proccses code when submited via ajax ...

    // In the end if goes well...
    // I want to pass the file array to the other if above.

    if (isset($msg) && !empty($msg))
    {
       // I insert the file array into session for take the rest of the proccses in above form if.
     $_SESSION['imgfilename'] = array($uploadFilename,$_FILES['ImageBrowse']); 

       //If i use here , it will work.
       if(@move_uploaded_file($_FILES['ImageBrowse']['tmp_name'],$uploadsDirectory.$uploadFilename)){
             echo 'Uploaded!';
            }else{
            echo 'Not Uploaded';
            }
       //
    }
    else
    {
        @unlink($_FILES[$fileElementName]); 
    }

如你所见,我将整个文件数组传递给会话,并在另一个上面调用它,如果获得更多的proccses,当我在@move_upload_file中使用$proc == "sendQuestionUploadImg"时,它将完美并将上传该文件,但如果我尝试在另一个内部使用@move_upload_file,如果没有人知道为什么?

1 个答案:

答案 0 :(得分:0)

要清除一件事:如果您的$_FILES数组没有错误,则文件已上传并存储在服务器ini_get('upload_tmp_dir');上的临时文件夹中

函数move_uploaded_file()只是将临时文件安全地移动到您选择的位置。

http://php.net/manual/en/function.move-uploaded-file.php

如果失败,您的php进程很可能无法写入目标文件夹。

调试此

  • 启用错误报告:

    ini_set( 'error_reporting', E_ALL );

  • 删除@功能

  • 前面的错误抑制move_uploaded_file
  • 使用Firebug之类的工具查看Ajax响应

http://ajaxian.com/archives/ajax-debugging-with-firebug