php上传图片到服务器
php文件上传中会用到$_FILES系统函数
一、$_FILES系统函数
PHP编程语言中的常见的$_FILES系统函数用法有:
$_FILES['myFile']['name'] 显示客户端文件的原名称。
$_FILES['myFile']['type'] 文件的 MIME 类型,例如"image/gif"。
$_FILES['myFile']['size'] 已上传文件的大小,单位为字节。
$_FILES['myFile']['tmp_name'] 储存的临时文件名,一般是系统默认。
二、常见函数
PHP编程语言中的常见的$_FILES系统函数用法有:
$_FILES['myFile']['name'] 显示客户端文件的原名称。
$_FILES['myFile']['type'] 文件的 MIME 类型,例如"image/gif"。
$_FILES['myFile']['size'] 已上传文件的大小,单位为字节。
$_FILES['myFile']['tmp_name'] 储存的临时文件名,一般是系统默认。
$_FILES['myFile']['error'] 该文件上传相关的错误代码。以下为不同代码代表的意思:
0; 文件上传成功。
1; 超过了文件大小php.ini中即系统设定的大小。
2; 超过了文件大小MAX_FILE_SIZE 选项指定的值。
3; 文件只有部分被上传。
4; 没有文件被上传。
5; 上传文件大小为0。
三、php文件上传通过form上传文件,首先需要标记
php代码如下:
<?php /** * Created by PhpStorm. * User: funmi * Date: 14-9-10 * Time: 下午6:57 */include_once('./common.php');if($_SGLOBAL['login']==true){ $cover_pic = upload_store_pic(); $store_name = $_POST['sjname']; $tel_number = $_POST['sjphone']; $ad_words = $_POST['sjad']; $store_introduce = $_POST['sjbrief']; $qq_number = $_POST['sjqq']; $setarr=array( 'cover_pic'=>$cover_pic, 'store_name'=>$store_name, 'tel_number'=>$tel_number, 'ad_words'=>$ad_words, 'store_introduce'=>$store_introduce, 'qq_number'=>$qq_number ); updatetable(tname('open_member_weixin_ap'),$setarr,array('apid'=>$_COOKIE['apid'],'id'=>$_COOKIE['id'])); $smarty->assign('loginuser',$_COOKIE['loginuser']); $smarty->display('setting.dwt'); exit();}gourl('binding.php');function upload_store_pic(){ //文件保存路径 $save_path = './uploads/store_pic/'; if (!file_exists($save_path)) { mkdir($save_path); } //定义允许上传的文件扩展名 $ext_arr = array( 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp') ); //最大文件大小 $max_size = 1000000; //PHP上传失败 if (!empty($_FILES['img_file']['error'])) { switch($_FILES['img_file']['error']){ case '1': $error = '超过php.ini允许的大小。'; break; case '2': $error = '超过表单允许的大小。'; break; case '3': $error = '图片只有部分被上传。'; break; case '4': $error = '请选择图片。'; break; case '6': $error = '找不到临时目录。'; break; case '7': $error = '写文件到硬盘出错。'; break; default: $error = '未知错误。'; } showmessage($error); return ''; } //有上传文件时 if (empty($_FILES) === false) { //原文件名 $file_name = $_FILES['img_file']['name']; //服务器上临时文件名 $tmp_name = $_FILES['img_file']['tmp_name']; //文件大小 $file_size = $_FILES['img_file']['size']; //检查文件名 if (!$file_name) { showmessage("请选择文件。"); return ''; } //检查目录 if (@is_dir($save_path) === false) { showmessage("上传目录不存在。"); return ''; } //检查目录写权限 if (@is_writable($save_path) === false) { showmessage("上传目录没有写权限。"); return ''; } //检查是否已上传 if (@is_uploaded_file($tmp_name) === false) { showmessage("上传失败。"); return ''; } //检查文件大小 if ($file_size > $max_size) { showmessage("上传文件大小超过限制。"); return ''; } //检查目录名 $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']); if (empty($ext_arr[$dir_name])) { showmessage("目录名不正确。"); return ''; } //获得文件扩展名 $temp_arr = explode(".", $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //检查扩展名 if (in_array($file_ext, $ext_arr[$dir_name]) === false) { showmessage("上传文件扩展名是不允许的扩展名。"); return ''; } //新文件名 $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '_apid_' . $_COOKIE['apid'] . '.' . $file_ext; //移动文件 $file_path = $save_path . $new_file_name; if (move_uploaded_file($tmp_name, $file_path) === false) { showmessage("上传文件失败。"); return ''; } @chmod($file_path, 0777); return $file_path; }else{ return ''; }}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
