2012-03-22 175 views
1

我在這裏找過類似的問題,但找不到解決我的問題的任何問題。 我想將文件上傳到Web根目錄(www文件夾)上方的文件夾。我在窗戶上跑步。請問我該如何做到這一點?謝謝。如何將文件上傳到web根目錄之外的文件夾

+1

像其他任何上傳...然後只是將文件移動到所需的位置。 [上傳POST的PHP手冊](http://www.php.net/manual/en/features.file-upload.post-method.php) – 2012-03-22 17:40:17

+0

引用此問題:http://stackoverflow.com/questions/223800/how-can-i-relax-phps-open-basedir-restriction – gorelative 2012-03-22 17:40:33

回答

4

默認情況下,文件將被上傳到Web根目錄以上,以確保安全,您必須將它們移動到任何您想要的位置。看看move_uploaded_file()

看看print_r($_FILES),它會顯示你上傳的每個文件的位置。

+0

我不想重新配置我的apache服務器,因爲我沒有託管服務器的特權,我用move_uploaded_file()將文件移入我的網站根目錄,但現在我希望它們移動到Web根目錄之上的名稱目錄。 – Chibuzo 2012-03-22 17:57:36

+0

您可以將其移動到您需要的任何文件夾,只需更改move_uploaded_file()中的第二個參數 – 472084 2012-03-22 18:02:20

+0

是的我知道我可以,我試過這個$ upload_folder =「../../matrials/」;因爲上載腳本位於文檔根目錄(mywebsitefolder/user/the-upload-script.php)內的文件夾內。 「材料」文件夾就在我的網站根目錄之上,即materials/www /我不知道還有什麼要嘗試的。謝謝你的時間 – Chibuzo 2012-03-22 18:22:41

2

這與我用於通過表單上傳的圖像類似。這假定輸入字段的名稱爲'圖像'。

function getExtension($str) { 
    $i = strrpos($str,"."); 
    if (!$i) { return ""; } 
    $l = strlen($str) - $i; 
    $ext = substr($str,$i+1,$l); 
    return strtolower($ext); 
}  

// Get the extension of the uploaded file 
$ext = getExtension($_FILES['image']['name']); 

// Give the file a new name (if you need) and append the extension. 
$img_name = time().$ext; 

// Set destination for upload 
$new_image = "./images/uploaded/" . $img_name; 

// Copy the file to the new location 
$copied = copy($_FILES['image']['tmp_name'], $new_image); 

您可以使用此爲上傳過任何文件,因爲以前的答案說,做一個var_dump($_FILES)會告訴你,你需要了解你上傳的文件,你用它做任何事情之前一切。

相關問題