使用 php 将文件上传到网站

2024-03-08

我是 php 新手,我正在尝试从这里修改代码:http://www.w3schools.com/php/php_file_upload.asp http://www.w3schools.com/php/php_file_upload.asp文件上传后保留在同一页面上。

页面显示以下错误(文件上传之前但文件上传之后不显示):

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\test-site\index.php on line 12
Sorry, file already exists.

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\test-site\index.php on line 38
Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded.

无论如何,文件上传正常。

这是代码:

<!DOCTYPE html>
<html>
<body>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload" class="upload_file">
    <input type="submit" value="Upload Image" name="submit">
</form>
<?php
$target_dir = "uploads/";
$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"])) 
{
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) 
    {
        echo "File is an image - " . $check["mime"] . ".";
        echo '<a href="'.$target_file.'">Download you file here</a>';
        $uploadOk = 1;
    } 
    else 
    {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) 
{
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) 
{
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&&         $imageFileType != "gif" ) 
{
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) 
{
    echo "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)) 
    {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } 
    else 
    {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
</body>
</html>

你需要移动这个方块

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

以及条件进入后的所有内容if(isset($_POST["submit"]))健康)状况。

当您第一次下载该页面时,$_FILES是空的,但你想使用它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 php 将文件上传到网站 的相关文章

  • 在本地 SDK 服务器上工作时,实时 Google App Engine 上出现 404

    我已经在GAE标准环境上部署了几个PHP应用程序 一切正常 现在我正在部署一个新应用程序 该应用程序位于由gcloudSDK按预期工作 终端命令 dev appserver py log level warning app yaml 问题是
  • 如何从日期中查找该月的最后一天?

    如何在 PHP 中获取该月的最后一天 Given a date 2009 11 23 我要2009 11 30 并给出 a date 2009 12 23 我要2009年12月31日 t返回给定日期所在月份的天数 请参阅的文档date ht
  • 如何改变div的值[重复]

    这个问题在这里已经有答案了 div this is div a div div this is div b div 现在我怎样才能改变文本div通过单击按钮动态地进行 这里还提到所有文本都来自数据库 当我单击按钮时它会检测到divid 并将
  • PHP 数组 - 如何将数组转换为对象? [复制]

    这个问题在这里已经有答案了 我对 PHP 还很陌生 所以请耐心等待 所以我收到这个错误 注意 尝试在此行获取非对象的属性 echo tr td row gt last name td td row gt first name td td r
  • Facebook 错误“验证验证码时出错”

    非常奇怪的错误 我用基德http developers facebook com docs authentication http developers facebook com docs authentication 所以我创建了对fb的
  • 如何使用正则表达式将多个
    标签替换为一个
    标签?

    I want br br 变成 br 正则表达式的模式是什么 注 br 标签可以连续出现两次以上 html preg replace br gt s i br html 这将捕获任何组合 br br or br 它们之间有任意数量或类型的空
  • 从按日期时间排序的 MySQL 表中获取用户的最后一个条目

    我有一张看起来像这样的桌子 USERNAME DATA DATETIME Jhon text1 2010 06 01 16 29 43 Mike text2 2010 06 01 16 29 22 Silver text3 2010 05
  • SWIG 生成的代码无法在 PHP 5.3.2 上运行未定义的符号:zend_error_noreturn

    我有一个库 在 PHP 5 1 6 生成的一些包装器代码的帮助下 我已经成功地使用了它SWIG http www swig org v1 3 40 我刚刚升级到 PHP 5 3 2 但看到以下错误 PHP Warning PHP Start
  • php 中的 cpu_get_usage ?

    我创建了一个基准类 允许用户插入例如 timer gt checkpoint 1 检查一些代码的时间 内存消耗等 如果她 他想测试它 她 他必须在代码末尾插入 result timer gt result 这会向公共函数 result 提供
  • 在 PHP 中 POST Base64 编码数据

    我需要使用 cURL 将一些数据 POST 到 PHP 页面 并且该请求包含三个参数 其中两个是常规文本值 一种是Base64编码的文件 我注意到 Base64 值在传输过程中被损坏 这是发送请求的代码 filename img2 jpg
  • Drupal 或 OSCommerce 中的定制产品?

    我看过高低 我想我没有在寻找正确的东西 我想知道 drupal 或 oscommerce 的模块或插件可以让您 构建自己的产品 IE 客户可以访问该网站 并说他想要 PC A 具有 X 容量的 RAM 和 Z 大小的硬盘等 是否有 osco
  • 最好的 php DOM 2 数组函数是什么?

    我想解析xml文件 到目前为止 我发现最好的方法是使用 DOMDocument 类 示例 xml 字符串
  • 不允许在 php 中连接到此 MariaDB 服务器

    我尝试在 php 中连接远程服务器数据库 但出现以下错误 Host xx xxx xx xx is not allowed to connect to this MariaDB server in 我的连接代码是这样的 servername
  • 从 Mailgun 表单 Post PHP 中检索附件

    如何检索并保存通过 Mailgun 的 POST 表单发送给我的附件 下面是一些参数 attachment 1 filename gt crabby gif type gt image gif name gt attachment 1 te
  • 属性“tablename.title”未定义

    我是伊比 我想做的是上传图像 照片 以及具有更新功能的数据库中的路径条目 我已点击此链接 http www yiiframework com wiki 349 how to upload image photo and path entry
  • 谁添加“_”单下划线查询参数?

    我有一个在 Apache 上运行的 PHP 服务器 我收到很多类似这样的请求 10 1 1 211 02 Sep 2010 16 14 31 0400 GET request 1283458471913 action get list HT
  • 如何在 MAMP 上显示错误?

    我有 MAMP 但我不知道如何在其上显示错误 当我的 php 代码出现错误时 它只显示空白页 我在 Google 上搜索过 我发现我必须在所有文件夹和版本上将其更改为 display errors on 并将其包含在我的页面上 错误报告 E
  • 为什么使用 LAMP 托管时避免使用 CGI for Python?

    我已经使用 PHP 多年了 最近我在论坛上看到很多帖子说PHP 已经过时了 现代编程语言更简单 更安全等等 所以 我决定开始学习Python 由于我习惯使用 PHP 因此我刚刚开始通过上传 htaccess 文件来构建页面 addtype
  • 危险的 PHP 函数

    我将用户输入的 php 函数存储到 mySQL 数据库中 这些函数需要能够被执行 正如我们所知 这可以并且将会允许黑客先生将一个漂亮的网站变成瑞士奶酪 这些函数很简单 不需要任何高级的 php 编码 它们更多地涉及处理单个数据数组 因此 如
  • 变量前面的@是什么意思? [复制]

    这个问题在这里已经有答案了 可能的重复 参考 这个符号在 PHP 中意味着什么 https stackoverflow com questions 3737139 reference what does this symbol mean i

随机推荐