2013-10-06 20 views
1

我有一個表單,我允許用戶上傳文件。由於用戶輸入其他信息,我只是將後處理更改爲重定向後獲取。我注意到全局$ _FILE對redirect.php可見,但在重定向回輸入表單後丟失。我試圖保存$ _FILE數組,但看起來臨時文件是通過post-redirect-get刪除的。有什麼辦法可以告訴服務器在離開redirect.php時保留臨時文件,以便在我看到合適的時候處理它們?提前致謝。輸入類型=「文件」 - 臨時文件刪除後重定向 - 得到

用戶表單:

<input type="file" name="file[]" id="userfiles" size='1px' multiple onChange="makeFileList();" /> 

重定向文件:

if (isset($_FILES)){ 
    $_SESSION['post-files'] = $_FILES; 
} 
header("Location: /back/to/input/form.php"); 
+0

你''session_start()'在這兩個頁面的頂部? –

+0

是的,它總是我做的第一件事。這些文件在重定向文件中可見,但在返回到輸入表單時不可見。 – mseifert

+0

這就是爲什麼他們被稱爲*臨時*文件。 – mario

回答

0

在年底,simplist的解決辦法是處理在redirect.php臨時文件和文件存儲在自己的臨時位置。然後我可以在回到我的處理形式中處理它們。對於任何跟隨,這是我做的...

if (isset($_FILES)){ 
    $_SESSION['post-files'] = $_FILES; 
    $i=0; 
    foreach ($_SESSION['post-files']['file']['name'] as $filename){ 
    // get the file to upload 
    $fromfile=$_SESSION['post-files']['file']['tmp_name'][$i]; 

    // get just the filename 
    $filename = pathinfo($fromfile, PATHINFO_FILENAME) . '.' . pathinfo ($fromfile, PATHINFO_EXTENSION); 

    // give it a new path 
    $tofile = "/some/temp/path/". $filename; 

    // store the new temp location 
    $_SESSION['post-files']['file']['tmp_name'][$i] = $tofile; 

    // move the files to a temp location 
    if (!is_dir(pathinfo($tofile,PATHINFO_DIRNAME))) { 
     mkdir(pathinfo($tofile,PATHINFO_DIRNAME), 0777, true); 
    } 
    move_uploaded_file($fromfile,$tofile); 
    } 
} 
0

您可能能夠將文件(S)的編碼副本傳遞到會話。

喜歡的東西...

$tempImages = array(); 

foreach($_FILES as $file) 
{ 
    $tempImages[] = base64_encode(file_get_contents($file['tmp_name'])); 
} 

$_SESSION['post-files'] = serialize($tempImages); 
+0

這對於單個文件來說是一個很好的解決方案。我可能有很多兆字節的文件上傳並將它們存儲在$ _SESSION中並不是一個好主意。但這很容易,可能會幫助某個人。查看我的解決方案,瞭解最終結果。 – mseifert