Home > Backend Development > PHP Tutorial > How to upload images and files in PHP

How to upload images and files in PHP

不言
Release: 2023-03-24 07:28:02
Original
5250 people have browsed it

The content of this article is about the method of uploading pictures and files in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to the

upload method:

function uploadFile2($subdirectory){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728000000000000 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','zip');// 设置附件上传类型
    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
    $upload->savePath  =     "$subdirectory/"; // 设置附件上传(子)目录
    // 上传文件 
    $info   =   $upload->upload();
    // var_dump($info);
    //  echo $upload->getError();
    //  die();
    $img= $info['file']['savepath'].$info['file']['savename'];
    return $img;
}
Copy after login

Calling method:

$subdirectory = 'File';//区分目录

$file = uploadFile2($subdirectory);
Copy after login

Page:

<input type="text" name="file"><br>//这是一个标签,我们要实现文件的上传,必须指定为multipart/form-data,否则服务器将不知道要干什么。
Copy after login


Note that

enctype="multipart/form-data"
Copy after login

must be added to the form. If you really don’t know how, you can Copy the file directly to the folder you want to put it, and then save the path to the database. When querying, query the file path, and spell the specified path with the file name you want to check (all your files only have different file names). You can copy the path to the browser and access it. If you can access the file description, it will be successful.

Related recommendations:

Tips for PHP to upload and save to a folder

##How to solve the problem that the temporary folder cannot be found when PHP uploads files

The above is the detailed content of How to upload images and files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template