使用 PHP 上传文件 [英] Upload a file using PHP

查看:23
本文介绍了使用 PHP 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件上传到指定文件夹.

I want to upload a file to a given folder.

<?php
$folder = "upload/";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name']))  {   
    if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
         echo "File uploaded";
    } else {
         echo "File not moved to destination folder. Check permissions";
    };
} else {s
     echo "File is not uploaded";
}; 
?>

错误是:

注意:未定义变量:HTTP_POST_FILES in C:wampwwwsdgimportips.php 第 3 行

Notice: Undefined variable: HTTP_POST_FILES in C:wampwwwsdgimportips.php on line 3

推荐答案

下面是上传文件的一种方式,还有很多其他的方式.

Below is one way to upload files, there are many other ways.

正如@nordenheim 所说,$HTTP_POST_FILES 自 PHP 4.1.0 起已被弃用,因此不建议使用.

As @nordenheim said, $HTTP_POST_FILES has been deprecated since PHP 4.1.0, thus not advisable to use so.

PHP 代码 (upload.php)

PHP Code (upload.php)

<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {

    if ($target_file == "upload/") {
        $msg = "cannot be empty";
        $uploadOk = 0;
    } // Check if file already exists
    else if (file_exists($target_file)) {
        $msg = "Sorry, file already exists.";
        $uploadOk = 0;
    } // Check file size
    else if ($_FILES["fileToUpload"]["size"] > 5000000) {
        $msg = "Sorry, your file is too large.";
        $uploadOk = 0;
    } // Check if $uploadOk is set to 0 by an error
    else if ($uploadOk == 0) {
        $msg = "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)) {
            $msg = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
        }
    }
}

?>

启动函数的HTML代码

HTML Code to start function

<form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
 </form>

希望这会有所帮助.

这篇关于使用 PHP 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆