当前位置:首页 » 文件管理 » php临时文件夹

php临时文件夹

发布时间: 2025-07-25 01:45:37

php 图片上传move_uploaded_file 出错

上传文件不存在。很可能是PHP临时文件夹不存在导致的。

<?php
/*
* 文件上传类
*/
class Uploads{

//上传文件
private $uploadFile;

//上传文件扩展名
private $ext = array('jpeg','jpg','gif','png');

//上传文件大小
private $size = 5000000;

//上传文件目录
private $uploadDir = './uploads/';

//是否自定义名称,默认FALSE
private $newName = '';

//上传文件是否可读,默认为TRUE
private $isRead = TRUE;

//上传文件是否可写,默认为TRUE
private $isWrite = TRUE;

//上传信息
private $info;

/*
* 文件上传类初始化
*/
public function __construct($newName='',$ext='',$size='',$dir='',$isRead=TRUE,$isWrite=TRUE){
$this->ext = empty($ext)?$this->ext:$ext;
$this->size = empty($size)?$this->size:$size;
$this->dir = empty($dir)?$this->uploadDir:$dir;
$this->newName = $newName;
$this->isRead = $isRead?TRUE:FALSE;
$this->isWrite = $isWrite?TRUE:FALSE;
}
/*
* 处理上传文件
*/
public function doUpload(){
$this->checkData();
$this->checkFile() or $this->error();
$this->checkExt() or $this->error();
$this->checkSize() or $this->error();
$this->checkError() or $this->error();
$this->checkDir() or $this->error();
$this->upload() or $this->error();
return $this->info['msg'];
}
/*
* 处理上传文件数据
*/
public function checkData(){
$num = 0;
$newArr = array();
foreach($_FILES as $v){
if(is_array($v['name'])){
$count = count($v['name']);
for($i=0; $i<$count; $i++){
foreach($v as $m=>$n){
$newArr[$num][$m] = $n[$i];
}
$num++;
}
}else{
$newArr[$num] = $v;
$num++;
}

}
$endArr = array();
foreach($newArr as $v){
if($v['name'] != ''){
$endArr[]=$v;
}
}
$this->uploadFile = $endArr;
}
/*
* 检测上传文件是否存在
*/
private function checkFile(){
if(empty($this->uploadFile)){
$this->info['error'] = '上传文件不得为空!!!';
return FALSE;
}
return TRUE;
}
/*
* 检测上传文件类型是否合法
*/

private function checkExt(){
if(!is_array($this->ext)){
$this->ext = explode(',', $this->ext);
}
foreach($this->uploadFile as $v){
$ext = strtolower(substr(strrchr(basename($v['name']),'.'),1));
if(!in_array($ext,$this->ext)){
$this->info['error'] = '上传文件类型非法,禁止上传!!!';
return FALSE;
}
}
return TRUE;
}

/*
* 检测上传文件大小
*/

private function checkSize(){
foreach($this->uploadFile as $v){
if($v['size']>$this->size){
$this->info['error'] = '上传文件体积过大,上传失败!!!';
return FALSE;
}
}
return TRUE;
}

/*
* 检测文件上传错误代码
*/
private function checkError(){
foreach($this->uploadFile as $v){
switch($v['error']){
case 0:
return TRUE;
break;
case 1:
$this->info['error'] = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值,上传失败!!!';
return FALSE;
break;
case 2:
$this->info['error'] = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值,上传失败!!!';
return FALSE;
break;
case 3:
$this->info['error'] = '文件只有部分被上传!!!';
return FALSE;
break;
case 4:
$this->info['error'] = '没有文件上传!!!';
return FALSE;
break;
}
}
return TRUE;
}

/*
* 检测上传文件夹是否存在
*/

private function checkDir(){
if(!file_exists($this->uploadDir)){
mkdir($this->uploadDir,0777,true);
}
if(!is_writeable($this->uploadDir)){
$this->info['error'] = '上传目录没有写入权限,上传失败!!!';
return FALSE;
}
return TRUE;
}

/*
* 上传文件
*/

private function upload(){

date_default_timezone_set('PRC');

//检测文件是否自定义名称
$name = empty($this->newName)?date('Ymd_His'):$this->newName;
foreach($this->uploadFile as $k=>$v){
$upload_path = $this->uploadDir.$name.'_'.($k+1).strrchr(basename($v['name']),'.');
$upload_path = iconv('UTF-8','gbk',$upload_path);
if(is_uploaded_file($v['tmp_name'])){
if(move_uploaded_file($v['tmp_name'], $upload_path)){
if($this->isRead && $this->isWrite){
chmod($upload_path,0777);
}else if($this->isRead && !$this->isWrite){
chmod($upload_path,0444);
}else if(!$this->isRead && $this->isWrite){
chmod($upload_path,0222);
}else{
chmod($upload_path,0000);
}
$this->info['msg']=array('type'=>1,'success'=>'文件上传成功','path'=>iconv('gbk','UTF-8',$upload_path));
}else{
$this->info['error'] = '文件上传失败!!!';
return FALSE;
}
}
}
return TRUE;

}
/*
* 上传成功的方法
*/
public function success(){
echo $this->info['msg']['success'];
}

/*
* 上传文件错误方法
*/
public function error(){
echo $this->info['error'];
die;
}

这是我写的PHP类,你可以参考一下。有什么特殊需要的,你可以告诉我一下,完善一下上传类。

㈡ PHP中上传文件的临时目录的问题

move_uploaded_file(“临时目录”,“新文件”)

这个临时目录是用$_FILES['userfile']['tmp_name']获取的

新文件 就是:
新文件目录+文件名

这个自己定义 。。。。。比如 我写个例子

$upfile="D:/aaa/" 目录名
$filename=$_FILES['userfile']['name'] 获取的原文件名
新文件就是 $newfile=$upfile.$filename;
用.连接起来。
move_uploaded_file($_FILES['userfile']['tmp_name'],$newfile))
执行之后就会移动的D:/aaa/下面

㈢ phpcms v9后台上传图片时出现缺少图像源文件地址

解决步骤如下:
1.在安装PHP环境的目录下面,找到 php.ini文件。
2. 打开php.ini ,查找upload_tmp_dir ,把这个词前面的“;”号去掉,使其生效,然后在“=”号后面加上临时文件夹的目录,比如: c:/windows/temp,这个可以自设。
3.在这个文件夹的安全属性里添加 "everyone"用户,赋予读写权限。

㈣ php上传文件时为什么要用到临时文件名tmp_name

上传都是先保存在临时文件夹中,需要程序来移动或复制文件。

热点内容
ie脚本错误找不到成员 发布:2025-07-26 07:29:45 浏览:503
勿忘我最低配置是什么 发布:2025-07-26 07:29:45 浏览:291
c语言fdset 发布:2025-07-26 07:29:03 浏览:970
php购物车的实现 发布:2025-07-26 07:25:29 浏览:598
国内最大电脑服务器 发布:2025-07-26 07:24:47 浏览:402
小程序源码论坛 发布:2025-07-26 07:15:23 浏览:81
ac语言表达式是 发布:2025-07-26 06:50:51 浏览:822
linux中安装软件 发布:2025-07-26 06:45:33 浏览:453
responsejava 发布:2025-07-26 06:45:31 浏览:599
tcpserverlinux 发布:2025-07-26 06:44:09 浏览:887