Home >Backend Development >PHP Tutorial >How to upload images and files in PHP
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; }
Calling method:
$subdirectory = 'File';//区分目录 $file = uploadFile2($subdirectory);
Page:
<input type="text" name="file"><br>//这是一个标签,我们要实现文件的上传,必须指定为multipart/form-data,否则服务器将不知道要干什么。
Note that
enctype="multipart/form-data"
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!