A. 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類,你可以參考一下。有什麼特殊需要的,你可以告訴我一下,完善一下上傳類。
B. php上傳圖片出現錯誤
配置在C盤臨時文件夾屬性,或者另外建一個臨時文件夾並且在PHP配置文件更改指向
C. php使用ftp_put上傳圖片出錯問題 圖片是上傳成功了,但是一個空白文件,為什麼為出現這狀況
你貼的代碼都是變數,實在很難確定你的錯誤到底在哪裡,希望你最好能夠貼出具體的代碼,因為這個可能是你路徑問題,或者是連接問題,也可能是轉換目錄問題。
下面是我寫的能夠成功完成文件上傳ftp的代碼,僅供參考。
<?php
$conn = ftp_connect("192.168.1.104") or die ("error");
ftp_login($conn, "benzlee", "123456");
ftp_chdir($conn, "folder");
$upload = ftp_put($conn, "test.jpg", "D:\\123.jpg", FTP_BINARY);
if($upload)
{
echo 1;
ftp_close($conn);
}
else
{
die("上傳圖片失敗------" . "D:\\123.jpg");
}
希望對你有幫助,有什麼不明白的,我盡量幫忙,謝謝^^