通过脚本上传后下载文件

时间:2013-12-02 19:18:08

标签: php file-upload

我正在创建一个简单的上传脚本但是一旦我上传了文件如果我链接到该文件,我得到一个服务器Forbidden错误。我已将权限设置为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>";

}
}

2 个答案:

答案 0 :(得分:0)

Apache是​​否可以访问这些文件?尝试使用chmod更改添加chown行。

答案 1 :(得分:0)

原来,文件权限750或777不允许您下载文件,但444会。不太确定为什么这是诚实的,但它现在正在运作。

感谢所有帮助人员。

直到下一次!