我想用PHP上傳文件。上傳PHP中的文件
我的文件夾結構如下:
我有一個在我的根htdocs文件夾名爲schemas
目錄。在這個文件夾中,我想爲每個用戶創建一個新文件夾;在這個文件夾中,我將存儲用戶特定的文件。
現在,我的代碼是不完整的(不檢查文件夾/文件是否已經存在,可能會出錯),但我只是想讓基礎知識工作。
<?php
if ($_FILES ["bestand"] ["error"] > 0) {
//for error messages: see http://php.net/manual/en/features.fileupload.errors.php
switch ($_FILES ["bestand"] ["error"]) {
case 1 :
$msg = "U mag maximaal 2MB opladen.";
break;
default :
$msg = "Sorry, uw upload kon niet worden verwerkt.";
}
} else {
//check MIME TYPE - http://php.net/manual/en/function.finfo-open.php
$allowedtypes = array ("application/pdf");
$filename = $_FILES ["bestand"] ["tmp_name"];
$finfo = new finfo (FILEINFO_MIME_TYPE);
$fileinfo = $finfo->file ($filename);
if (in_array ($fileinfo, $allowedtypes)) {
//move uploaded file
$dir = "schemas";
chdir("schemas");
$user_folder = str_replace(" ", "", $_POST['schema']);
mkdir($user_folder, 0777);
// echo getcwd();
// closedir($open);
$folder = "/schemas/" . $user_folder . "/" . $_FILES ["bestand"] ["name"];
echo $folder;
echo $user_folder;
if (move_uploaded_file ($_FILES ["bestand"] ["tmp_name"], $folder)) {
$msg = "Uw schema is succesvol geupload!";
} else {
$msg = "Upload mislukt.";
}
} else {
$msg = "U kan enkel een pdf uploaden.";
}
}
echo $msg . "<br />";
?>
當我嘗試,我得到以下警告:
Warning: move_uploaded_file(/schemas/loesp/loopschema-0-tot-5-kilometer.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Sportjefit2/uploadenfile.php on line 30
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/MAMP/tmp/php/phpMsxxsu' to '/schemas/loesp/loopschema-0-tot-5-kilometer.pdf' in /Applications/MAMP/htdocs/Sportjefit2/uploadenfile.php on line 30
腳本不創建的文件夾(在這種情況下loesp
),但似乎並沒有上傳或移動實際的文件。我很難過。
*(參考)* http://de2.php.net/manual /en/features.file-upload.php – Gordon 2011-05-11 08:23:14
嘗試使用完整路徑而不是相對路徑,看看是否有幫助。另外,從閱讀手冊看來,destinatin應該包含目標文件名稱,而不僅僅是文件夾名稱。 – Grim 2011-05-11 08:26:46