php PHP无法上传文件到服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9550972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:06:22  来源:igfitidea点击:

PHP can't upload files to server?

phpfile-upload

提问by J_rad

I have a php file that uploads images like jpegs and png onto a folder called uploads that is stored on the apache server and in the same location as the php file.

我有一个 php 文件,可以将 jpegs 和 png 等图像上传到一个名为 uploads 的文件夹中,该文件夹存储在 apache 服务器上,并且与 php 文件位于同一位置。

I have checked the code of both the HTML and the PHP and both seem to be perfectly fine, however whenever I try to upload a file I always get an error message and the file doesn't get uploaded.

我已经检查了 HTML 和 PHP 的代码,两者似乎都很好,但是每当我尝试上传文件时,我总是收到一条错误消息并且文件没有上传。

It would be much appreciated if someone with more experience than me can look at my code and tell me why it is behaving in this manner.

如果有比我更有经验的人可以查看我的代码并告诉我为什么它会以这种方式运行,我将不胜感激。

Here is the HTML form:

这是 HTML 表单:

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Upload Your File</title>
</head>
<body>
    <?php
    // put your code here
    ?>

    <form enctype="multipart/form-data" method="post" action="fileHandler.php">
        Select File:
        <input name="uploaded_file" type="file"/><br/>
        <input type="submit" value="Upload"/>
    </form>
</body>
</html>

and here is the PHP file that is executed when the form is submitted:

这是提交表单时执行的 PHP 文件:

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * PHP file that uploads files and handles any errors that may occur
 * when the file is being uploaded. Then places that file into the 
 * "uploads" directory. File cannot work is no "uploads" directory is created in the
 * same directory as the function. 
 */

$fileName = $_FILES["uploaded_file"]["name"];//the files name takes from the HTML form
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];//file in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"];//the type of file 
$fileSize = $_FILES["uploaded_file"]["size"];//file size in bytes
$fileErrorMsg = $FILES["uploaded_file"]["error"];//0 for false and 1 for true
$target_path = "uploads/" . basename( $_FILES["uploaded_file"]["name"]); 

echo "file name: $fileName </br> temp file location: $fileTmpLoc<br/> file type: $fileType<br/> file size: $fileSize<br/> file upload target: $target_path<br/> file error msg: $fileErrorMsg<br/>";

//START PHP Image Upload Error Handling---------------------------------------------------------------------------------------------------

    if(!$fileTmpLoc)//no file was chosen ie file = null
    {
        echo "ERROR: Please select a file before clicking submit button.";
        exit();
    }
    else
        if(!$fileSize > 16777215)//if file is > 16MB (Max size of MEDIUMBLOB)
        {
            echo "ERROR: Your file was larger than 16 Megabytes";

            unlink($fileTmpLoc);//remove the uploaded file from the PHP folder
            exit();
        }
        else
            if(!preg_match("/\.(gif|jpg|jpeg|png)$/i", $fileName))//this codition allows only the type of files listed to be uploaded
            {
                echo "ERROR: Your image was not .gif, .jpg, .jpeg or .png";
                unlink($fileTmpLoc);//remove the uploaded file from the PHP temp folder
                exit();
            }
            else
                if($fileErrorMsg == 1)//if file uploaded error key = 1 ie is true
                {
                    echo "ERROR: An error occured while processing the file. Please try again.";
                    exit();
                }


    //END PHP Image Upload Error Handling---------------------------------------------------------------------------------------------------------------------


    //Place it into your "uploads" folder using the move_uploaded_file() function
    $moveResult = move_uploaded_file($fileTmpLoc, $target_path);

    //Check to make sure the result is true before continuing
    if($moveResult != true)
    {
        echo "ERROR: File not uploaded. Please Try again.";
        unlink($fileTmpLoc);//remove the uploaded file from the PHP temp folder

    }
    else
    {
        //Display to the page so you see what is happening 
        echo "The file named <strong>$fileName</strong> uploaded successfully.<br/><br/>";
        echo "It is <strong>$fileSize</strong> bytes.<br/><br/>";
        echo "It is a <strong>$fileType</strong> type of file.<br/><br/>";
        echo "The Error Message output for this upload is: $fileErrorMsg";
    }
?>

采纳答案by Brombomb

make sure that the directory structure has write permissions. You can check within php by using is_writeable. By checking from within PHP you will also be making sure that the PHP user has write access.

确保目录结构具有写权限。您可以使用is_writeable在 php 中进行检查。通过在 PHP 内部进行检查,您还将确保 PHP 用户具有写入权限。

回答by adamdehaven

Check the folder permissions on the server. If incorrect, you can modify your php.ini file.

检查服务器上的文件夹权限。如果不正确,您可以修改您的 php.ini 文件。