简体   繁体   English

Move_uploaded_file()权限

[英]Move_uploaded_file() permission

I'm working on a simple php script that allow me to upload images. 我正在研究一个简单的PHP脚本,该脚本允许我上传图像。 File uploads correctly, but when I go to open the image uploaded in the directory there's an errore "you're not authorized to see this file, check the authorization and retry" 文件正确上传,但是当我打开打开目录中上传的图像时,出现错误“您无权查看此文件,请检查授权并重试”

    <!DOCTYPE html>
<html>
    <head>
        <title> File upload </title>
        <meta charset = "UTF-8" />
    </head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Seleziona il file:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
</body>
</html>

    <?php
    $target_dir = "C:\Users\test\Desktop\upload_succeeded\\";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $fileType = pathinfo($target_file,PATHINFO_EXTENSION);

    // Controllo se il file esiste gia
    if (file_exists($target_file)) 
    {
        echo "Spiacenti, il file esiste gia'.";
        $uploadOk = 0;
    }

    // Abilitare solo alcuni formati di file
    if($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif" ) 
    {
        echo "Spiacenti, solo file JPG, JPEG, PNG & GIF sono abilitati.";
        $uploadOk = 0;
    }

    // Controllo se $uploadOk e' settato a 0 da un errore
    if ($uploadOk == 0) 
    {
        echo "Spiacenti, il tuo file non e' stato caricato.";
    // Se tutto e' ok prova a caricare il file
    } 
    else 
    {
        if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
        {
            echo "Il file ". basename( $_FILES["fileToUpload"]["name"]). " e' stato correttamente caricato.";
        } 
        else 
        {
            echo "Spiacenti, c'e' stato un errore nel caricamento del file.";
        }
    }
?>

You're running windows. 您正在运行Windows。 Download any FTP client and connect to your server than change permission to files to 777(rwe). 下载任何FTP客户端并连接到服务器,然后将文件权限更改为777(rwe)。

I think I know the answer, but let me first explain what exactly happens here. 我想我知道答案,但是让我先解释一下这里到底发生了什么。

The browser sends a multipart/form-data through post that contains the file. 浏览器通过包含文件的文章发送多部分/表单数据。 Apache receives said file and PHP then places it in a temp directory. Apache收到所述文件,然后PHP将其放在临时目录中。 Where it is created as a new file on the file system. 在文件系统上将其创建为新文件的位置。 What you get through PHP is; 通过PHP获得的是: the normalised checked file inside the $_FILES variable which goes through some further testing before it gets copied by the move_uploaded_file function. $ _FILES变量中的规范化检查文件,在通过move_uploaded_file函数复制之前,它经过了一些进一步的测试。

What most likely is your problem is: that the user used by PHP/Apache and the permissions set while writing the file, does not allow the user you are current logged in with to open it. 您最有可能遇到的问题是:PHP / Apache使用的用户以及在编写文件时设置的权限不允许您当前登录的用户打开它。

2 Possible solutions: 2可能的解决方案:

Change the Apache / PHP user to write the file with permissions so you have rights to open it. 更改Apache / PHP用户以写入具有权限的文件,以便您有权打开它。

Or change permissions on the file afterwards, assuming the user used to created the file has enough permissions to do so: 或在以后更改文件的权限,假设用于创建文件的用户具有足够的权限:

Change permissions of file uploaded by PHP 更改PHP上传的文件的权限

无需手动授予权限,您可以在上传文件后立即使用chmod()函数在php中授予权限,您可以在上传文件后立即使用file_path/file_name调用此函数,然后可以授予权限,将0777用于所有读取/正确权限

chmod(IMAGES_DIR.'/'.'image_name.'.$image_file_type, 0777);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM