Home php教程 PHP源码 Php文件上传类class.upload.php用法示例

Php文件上传类class.upload.php用法示例

Jun 08, 2016 pm 05:23 PM
file image nbsp quot

本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。

<script>ec(2);</script>

简介

Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能。不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处理,其图片处理功能比较 强大,包括图片的缩放、旋转、剪裁、类型转换、应用滤镜、添加边框文字和水印等。支持的图片类型是png、jpg、gif和bmp。


使用方法

首先我们要有一个用于提交上传文件的表单,如下,注意表单要写上enctype="multipart/form-data"。

 代码如下 复制代码

  

  

 

然后创建表单处理脚本文件upload.php,在脚本中添加如下程序。含义请看注释

 代码如下 复制代码

$handle = new upload($_FILES['image_field']);

//image_field是表单中上传控件的name属性,创建一个类的实例并用$_FILES[' image_field ']初始化,现在这个类已经知道你要处理哪个文件,并对文件的位置大小等信息了如指掌。

 代码如下 复制代码

   if ($handle->uploaded) {

//这个判断是个安全选项,看一下这个文件真的是正规途径过来的上传文件吗。

       $handle->file_new_name_body   = 'image_resized';//给这个文件起个名字吧, 注意这个是不带扩展名的部分,如果有与之同名的文件,默认的行为是重命名。

       $handle->image_resize         = true;//我们传了一张图片,需要对它进行缩放,设置缩放属性为true

       $handle->image_x                  = 100;//缩放要有标准,这里我们以宽度为基准,宽度必须是100像素

       $handle->image_ratio_y        = true;//你也可以指定一个具体的长度数值,写法是$handle->image_y = [数值],这里是告诉程序以宽度为基准,高度按比例缩放。

       $handle->process('/home/user/files/');//在执行这句话之前,你还可以设定更多的处理选项,比如是否对图 片进行旋转剪裁,设定好了,就用这句话来执行图片处理和上传。图片处理时会创建一个原图片的副本,不会修改原始文件,原始文件存在于php的临时文件夹 中,在linux服务器上可能是/tmp目录。这样你可以对同一张上传的图片进行多次处理,并指定上传到不同的位置。这里'/home/user /files/'指定了文件要被复制到的位置。

       if ($handle->processed) {

           echo 'image resized';

           $handle->clean();//如果文件成功上传,就清除掉这个对源文件的引用,此后将不能再对刚才上传到临时文件夹的图片进行处理和复制了。

       } else {

           echo 'error : ' . $handle->error;

       }

   }

 

如何直接下载图片,如果你是做在线图片处理程序,这个很有用。

 代码如下 复制代码

$handle = new upload($_FILES['image_field']);

header('Content-type: ' . $handle->file_src_mime);

header("Content-Disposition:attachment; filename=".rawurlencode($handle->file_src_name).";");

   echo $handle->Process();

 

如何将图片直接输出到浏览器

 代码如下 复制代码

$handle = new upload($_FILES['image_field']);

header('Content-type: ' . $handle->file_src_mime);

echo $handle->Process();

 

比较常用的处理参数,这些参数可以在调用$handle->process()之前设定处理方式

 代码如下 复制代码

$handle->file_new_name_body = 'new name'

指定文件被上传到指定位置后的名字,不包含扩展名的部分

 代码如下 复制代码

$handle->file_new_name_ext = 'txt'

指定文件扩展名

 代码如下 复制代码

$handle->mime_check = true;

指定是否对文件扩展名进行安全性检查,默认是true,即进行检查,注意这可能导致某些类型的文件无法上传,比如.zip文件

 代码如下 复制代码

$handle->allowed = array('application/pdf','application/msword', 'image/*');

指定允许上传的文件mime类型

 代码如下 复制代码

$handle->image_max_width = 200;

指定最大允许的图片宽度,如果图片超过此宽度将被视为不符合要求无法上传,类似参数有$handle->image_max_height,,$handle->image_max_pixels,$handle->image_max_ratio等

 在处理文件之前你可以读取的文件信息

file_src_name:上传文件的原始名称,包括扩展名

file_src_name_body:原始名称,不包括扩展名

file_src_name_ext:扩展名

file_src_pathname:文件完整的路径和名称

file_src_mime:mime类型

file_src_size: 文件大小

file_src_error: 上传错误

file_is_image: 布尔类型,是否是图片

 如果文件是图片,你还可以读到

image_src_x,image_src_y,image_src_pixels,image_src_type,image_src_bits

 文件处理了,也上传了,能返回什么,这个也是很有用的,如果你要记录文件信息到数据库或者显示给用户的话。

file_dst_path:上传后的文件路径

file_dst_name_body: 上传后的文件名,不包括扩展名

file_dst_name_ext:上传后的文件扩展名

file_dst_name:上传后的文件全名

file_dst_pathname:上传后的文件完整的路径和名称

如果文件是图片,你还可以读到

image_dst_x,image_dst_y,image_convert

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

See all articles