dongtan9066 2015-01-06 20:45
浏览 193

由move_uploaded_file()引起的权限问题的解决方法

I'm currently uploading images to my webserver using below PHP script. However, this causes permission troubles which I can not seem to change due to restrictions of webserver access. What's the best way to work around these permission warnings?

<?php
function upload_image()
{
    $target_dir = "../images/uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = true;
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
    if (isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if ($check !== false) {
            $uploadOk = true;
        } else {
            $uploadOk = false;
            return "Sorry, file is not an image.";
        }
    }
// Check if file already exists
    if (file_exists($target_file)) {
        $uploadOk = false;
        return "Sorry, file already exists.";
    }
// Check file size
    if ($_FILES["fileToUpload"]["size"] > 200000000) {
        $uploadOk = false;
        return "Sorry, your file is too large.";
    }
// Allow certain file formats
    if ($imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG"
        && $imageFileType != "GIF"
    ) {
        $uploadOk = false;
        return "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    }
// Check if $uploadOk is set to 0 by an error
    if ($uploadOk == false) {
        return "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            return basename($_FILES["fileToUpload"]["name"]);
        } else {
            return "Sorry, there was an error uploading your file.";
        }
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab有关常微分方程的问题求解决
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable