php上传文件

$_FILES

作用:用来接收前端上传的文件,并且存储到服务器上。

前端

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="./upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="ccc">
        <input type="submit" name="ddd" value="上传">
</form>
</body>
</html>

后端文件:方法一

php 复制代码
<?php
    // 1) 获取到前端元素对象
    $fileObject = $_FILES["ccc"];  

	// 2) 获取文件名字
    $filename = $fileObject["name"];  # 获取文件名字
    echo $filename;

	// 3) 服务器拿到文件之后,是放到内存中,放到tmp_name键的值就是内存中的路径
	$file_tmpname = $_FILES["ccc"]["tmp_name"];

	// 4) 移动文件
    move_uploaded_file($file_tmpname, '/opt/lampp/htdocs/wh069/crm/demo2/'.$filename); 
?>

后端文件:方法二

php 复制代码
<?php

    // 1) 获取对象
    $fileObject = $_FILES["ccc"];  # 对象类型是一个数组

    // 2) 获取文件对象的属性
    $filename = $fileObject["name"];  # 获取文件名字
    $filesize = $$fileObject["size"]; # 获取文件大小
    $file_tmpname = $_FILES["ccc"]["tmp_name"]; # 获取上传文件在服务器中的临时文件名(系统默认的)

    // 3) 读文件
    $file_obj = fopen("$file_tmpname", 'r');
    $file_content = fread($file_obj, $filesize);
    fclose($file_obj);

    // 4) 写文件
    $fielpath = "/opt/lampp/htdocs/wh069/crm/demo2/".$filename;
    $file_obj = fopen($fielpath, 'w');
    fwrite($file_obj, $file_content);
    fclose($file_obj);
?>

后端文件:方法三

php 复制代码
<?php

    // 1) 获取对象
    $fileObject = $_FILES["ccc"];  # 对象类型是一个数组

    // 2) 获取文件对象的属性
    $filename = $fileObject["name"];  # 获取文件名字
    $filesize = $$fileObject["size"]; # 获取文件大小
    $file_tmpname = $_FILES["ccc"]["tmp_name"]; # 获取上传文件在服务器中的临时文件名(系统默认的)

    // 3) 获取内容
    $file_contnets = file_get_contents("$file_tmpname");

    // 4) 写文件
    $fielpath = "/opt/lampp/htdocs/wh069/crm/demo2/".$filename;
    file_put_contents($fielpath, $file_contnets);
?>
相关推荐
JaguarJack1 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
QQ5110082852 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe2 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js