[英]download files once uploaded via script
我正在創建一個簡單的上載腳本,但是一旦上載了文件,如果使用鏈接到文件,則會出現服務器禁止錯誤。 我已將權限設置為750(已檢查並且權限正確),所以不明白為什么會這樣。
任何幫助都會很棒,以下是我的上傳腳本:
if($_POST["upload"]){
//gets current year for path
$year = date('Y');
//path to directory
$path = $_SERVER["DOCUMENT_ROOT"] . '/uploads/' . $year . '/' . strtolower(str_replace(' ','',$_POST["username"])) . '/' . $_POST["month"];
//path to file
$target_path = $path . '/' . basename($_FILES['uploadedfile']['name']);
// $_FILES is the array auto filled when you upload a file and submit a form.
$file_name = $_FILES['uploadedfile']['name']; // file name
$file_tmp = $_FILES['uploadedfile']['tmp_name']; // actual location
$file_size = $_FILES['uploadedfile']['size']; // file size
$file_type = $_FILES['uploadedfile']['type']; // mime type of file sent by browser. PHP doesn't check it.
$file_error = $_FILES['uploadedfile']['error']; // any error!. get from here
if($file_error == UPLOAD_ERR_NO_FILE){
print "<div class='error'>Please select a file first</div>";
} elseif ($file_error == UPLOAD_ERR_INI_SIZE) {
print "<div class='error'>The file is too large</div>";
} elseif($file_error == UPLOAD_ERR_PARTIAL){
print "<div class='error'>An error occured whilst trying to receive the file, please try again.</div>";
} elseif( !($file_type=="application/pdf")) {
print "<div class='error'>Your File Type is: <b>". $file_type."</b> the file type must be <b>PDF</b></div>";
} elseif($file_error == 0){
if(!is_dir($path)){
mkdir($path, 0750, true);
}
move_uploaded_file($file_tmp, $target_path);
chmod($target_path, 0750);
print "<div class='success'>The file " . "<span class='filename'>" . basename($file_name) . "</span>" . " has been uploaded to <b>" . $_POST["username"] . "'s</b> folder</div>";
}
}
Apache可以訪問這些文件嗎? 嘗試在chmod更改中添加一條弦線。
原來,文件許可權750或777不允許您下載文件,但文件許可權是444。 不太清楚為什么這是誠實的,但它現在正在起作用。
感謝您的幫助。
直到下一次!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.