2015-02-06 72 views
0

我在一個頁面上工作,該頁面允許用戶一次「上傳」多個文件(它們本地存儲在與其類型相關的文件夾中)。如何設置傳遞給函數的變量的值?

我的問題是,當我試圖通過$upFile1$fileInfo1writeResults()與信息更新$fileInfo1關於$upFile1,回送的結果是空的。

我做了一些研究,這似乎是一個範圍界定問題,但我不確定上個月剛剛開始學習PHP的最佳方法。

任何幫助將不勝感激。

foo.html

<!DOCTYPE HTML> 

<html> 
    <head> 
     <title>File Upload</title> 
    </head> 
    <body> 
     <form method="post" action="foo.php" enctype="multipart/form-data"> 
      <p> 
       <b>File 1:</b><br> 
       <input type="file" name="upFile1"><br/> 
       <br/> 
       <b>File 2:</b><br> 
       <input type="file" name="upFile2"><br/> 
       <br/> 
      </p> 
      <p> 
       <input type="submit" name="submit" value="Upload Files"> 
      </p> 
     </form> 
    </body> 
</html> 

foo.php

<?php 

$upFile1 = $_FILES['upFile1']; 
$upFile2 = $_FILES['upFile2']; 

$fileInfo1 = ""; 
$fileInfo2 = ""; 

// Check if directories exist before uploading files to them 
if (!file_exists('./files/images')) mkdir('./files/images', 0777, true); 
if (!file_exists('./files/text')) mkdir('./files/text', 0777, true); 

// Copies the file from the source input to its corresponding folder 
function copyTo($source) { 
    if (($source['type'] == 'image/jpg') || ($source['type'] == 'image/png')) { 
     @copy($source['tmp_name'], "./files/images/".$source['name']); 
    } 
    if ($source['type'] == 'text/plain') { 
     @copy($source['tmp_name'], "./files/text/".$source['name']); 
    } 
} 

// Outputs file data for input file to destination 
function writeResults($source, $destination) { 
    $destination .= "You sent: "; 
    $destination .= $source['name']; 
    $destination .= ", a "; 
    $destination .= $source['size']; 
    $destination .= "byte file with a mime type of "; 
    $destination .= $source['type']; 
    $destination .= "."; 
    // echoing $destination outputs the correct information, however 
    // $fileInfo1 and $fileInfo2 aren't affected at all. 
} 

// Check if both of the file uploads are not empty 
if ((!empty($upFile1['name'])) || (!empty($upFile2['name']))) { 
    // Check if the first file upload is not empty 
    if (!empty($upFile1['name'])) { 
     copyTo($upFile1); 
     writeResults($upFile1, $fileInfo1); 
    } 
    // Check if the second file upload is not empty 
    if (!empty($upFile2['name'])) { 
     copyTo($upFile2); 
     writeResults($upFile2, $fileInfo2); 
    } 
} else { 
    die("No input files specified."); 
} 

?> 

<!DOCTYPE HTML> 

<html> 
    <head> 
     <title>File Upload</title> 
    </head> 
    <body> 
     <p> 
      <!-- This is empty --> 
      <?php echo "$fileInfo1"; ?> 
     </p> 
     <p> 
      <!-- This is empty --> 
      <?php echo "$fileInfo2"; ?> 
     </p> 
    </body> 
</html> 
+0

那麼,你究竟在說什麼函數,你期望它做什麼,它做了什麼,而你爲什麼期望範圍成爲一個問題? – deceze 2015-02-06 08:21:30

+0

我編輯了我的OP來澄清我的問題。 – 2015-02-06 08:29:02

回答

3

你逝去的的值和$fileInfo2,但它們是空的。之後,$destination值和fileininfo值之間沒有關係。

更改函數以返回$destination值。

更改writeResults命令$fileInfo1 = writeResults($upFile1);

+0

你會推薦通過引用傳遞變量的返回方法嗎? – 2015-02-06 08:32:12

+0

是的,你可以更好地使用返回值 – 2015-02-06 08:33:07

+0

在我看來,它更乾淨。 – 2015-02-06 08:33:09

0
function writeResults($source, &$destination) { 
    $destination .= "You sent: "; 
    $destination .= $source['name']; 
    $destination .= ", a "; 
    $destination .= $source['size']; 
    $destination .= "byte file with a mime type of "; 
    $destination .= $source['type']; 
    $destination .= "."; 
    // echoing $destination outputs the correct information, however 
    // $fileInfo1 and $fileInfo2 aren't affected at all. 
} 

添加&$destination前將通過參考傳遞變量,而不是由值。因此,對函數進行的修改將應用於傳遞的變量,而不是函數內的副本。

相關問題