很多站长朋友们都不太清楚php图片服务器,今天小编就来给大家整理php图片服务器,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php当中,图片是上传到服务器好点还是数据库? 2、 PHP中图片上传到服务器的问题 3、 php文件上传,如何实现独立的图片服务器的,该move_uploaded_file()函数能实现吗? 4、 php上传图片到服务器的前端和php代码 5、 php 编写 实现上传图片至服务器的函数 php当中,图片是上传到服务器好点还是数据库?你理解错了, 图片只能上传到服务器,服务器就是一台电脑,相当于你把这张图片放在这个电脑的硬盘里,怎么可能放入数据库,放入数据库的只是这个图片的信息,如文件名。。。
因为只有这样,你下次才能调用到你这次上传的图片,否则你根本就不知道你上传的是哪张图片,就无法调用这张图片,上传也就没有意义了。
PHP中图片上传到服务器的问题第一种情况:可能是服务器没有链接上,服务器的设置有问题。
第二种情况:可能是图片的路径有问题,你可以右击查看图片属性,确定路径。
第三种情况:也就是你说的内存不足。
php文件上传,如何实现独立的图片服务器的,该move_uploaded_file()函数能实现吗?可以的。图片也是文件的一种形式,主要上传的时候注意限制文件的后缀为.jpg,.png等等,并使用move_uploaded_file把图片转移到服务器中一个特定的文件夹中。为了方便集中管理这些图片,可以建立一个数据库,用一个数据表记录该图片的路径、名称、上传日期、格式等等信息。访问图像的时候,就可以根据图片的路径获得了。
php上传图片到服务器的前端和php代码前端 代码 使用 extjs 3.4
uploadPhotoWindow=Ext.extend(Ext.Window,{
title:" 上传图片 Upload Photo",
height:420 ,
width:600,
closeAction:'close',
modal : true,
iconCls:'btn-setting',
buttonAlign: 'center',
upload_ok:false,
haveUpload:false,
initComponent : function() {
Ext.form.Field.prototype.msgTarget = 'side';
var po_no=new Ext.form.TextField({name:'Po_no',fieldLabel: '单号 Po No',itemId:'Po_no', width:120,
allowBlank: false, value:this.cur_sele_po_no, hidden:true});
var OP=new Ext.form.TextField({name:'OP',itemId:'OP', width:12,
allowBlank: false, value:"uploadphoto", hidden:true});
var file_name=new Ext.form.TextField({name:'photo_file_name',itemId:'photo_file_name', width:180,
allowBlank: false, value:"",hidden:true,});
var imagebox = new Ext.BoxComponent({
itemId:'imagebox',
autoEl: {
tag: 'img', //指定为img标签
style: 'height:100%;margin:0px auto;border:1px solid #ccc; text-align:center;margin-bottom:10px',
src: 'img/userimg/nophoto.jpg' , //指定url路径
}
});
var form_set_field = new Ext.FormPanel({
frame:true,
itemId:'form_set_field',
layout:'form',
//tableAttrs: {border: 1},
defaults:{labelAlign:'right',labelWidth:110,bodyStyle: 'padding:0 30px 0 0;',frame:false,layout:'form'},
items:[po_no,OP,file_name,imagebox],
});
var file = new Ext.form.TextField({
name: 'imgFile',
fieldLabel: '文件上传',
inputType: 'file',
allowBlank: false,
blankText: '请浏览图片'
});
var form_set_file = new Ext.FormPanel({
frame:true,
fileUpload: true,
itemId:'form_set_file',
layout:'form',
//tableAttrs: {border: 1},
defaults:{labelAlign:'right',labelWidth:110,bodyStyle: 'padding:0 30px 0 0;',frame:false,layout:'form'},
items:[file],
});
var btnOK= new Ext.Button({text: '上传 Upload ', iconCls:'btn-save',width:70,handler: function(){
var form_set=this.ownerCt.ownerCt.getComponent('form_set_file');
var form_set_field=this.ownerCt.ownerCt.getComponent('form_set_field');
var po_no=form_set_field.getComponent('Po_no').getValue();
var file_name=form_set_field.getComponent('photo_file_name');
//alert(po_no);
var imgbox=form_set_field.getComponent('imagebox');
if (form_set.getForm().isValid()){
form_set.getForm().submit({
waitMsg : '正在上传数据 Uploading....',waitTitle:'请稍候 waiting....',
url:'php/toolsfile/photoUpload.php',
method : 'post',
success : function(form, action){
var out = action.result.success;
if (out != true){
Ext.Msg.alert('提示 Tips ', '上传数据失败,错误信息 Save failure :'+action.result.msg);
//alert(action.result.msg);
} else{
//Ext.Msg.alert('提示 Tips ', '上传数据成功,服务器信息: Save success '+action.result.msg);
file_name.setValue(action.result.file_name);
imgbox.getEl().dom.src=action.result.file_scr;
form_set.ownerCt.savePhoto();
//form_set.ownerCt.grid.store.load();
//form_set.ownerCt.dateChang=true;
//form_set.ownerCt.destroy( );
}
},
failure: function(form, action) {
switch (action.failureType) {
case Ext.form.Action.CLIENT_INVALID:
Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
break;
case Ext.form.Action.CONNECT_FAILURE:
Ext.Msg.alert('Failure', 'Ajax communication failed');
break;
case Ext.form.Action.SERVER_INVALID:
Ext.Msg.alert('Failure', action.result.msg);
break;
}
},
});
}else{
Ext.Msg.alert('提示 Tips :', '请选择文件! \n Please select Img file ');
}
}
});
var btnCancel = new Ext.Button({text: ' 关闭 Close ', iconCls:'btn-cancel',width:70,handler: function(){this.ownerCt.ownerCt.destroy( )}});
Ext.apply(this,{
items: [form_set_field,form_set_file],
buttons: [btnOK, btnCancel],
});
uploadPhotoWindow.superclass.initComponent.call(this);
},
savePhoto:function (){
//alert(this.cur_sele_po_no);
var form_set_field=this.getComponent('form_set_field');
var form_set_file=this.getComponent('form_set_file');
form_set_field.getForm().submit({
waitMsg : '上传成功,正在存储 saveing....',waitTitle:'请稍候 waiting....',
url:'php/jsonfile/po_nophotolist_json.php',
method : 'post',
success : function(form, action){
var out = action.result.success;
if (out != true){
Ext.Msg.alert('提示 Tips ', '存储失败,错误信息 Save failure :'+action.result.msg);
} else{
Ext.Msg.alert('提示 Tips ', '存储成功,服务器信息: Save success '+action.result.msg);
form_set_file.getForm().reset();
form_set_file.ownerCt.haveUpload=true;
}
},
failure: function(form, action) {
switch (action.failureType) {
case Ext.form.Action.CLIENT_INVALID:
Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
break;
case Ext.form.Action.CONNECT_FAILURE:
Ext.Msg.alert('Failure', 'Ajax communication failed');
break;
case Ext.form.Action.SERVER_INVALID:
Ext.Msg.alert('Failure', action.result.msg);
break;
}
},
});
},
isUpload:function(){
return this.haveUpload;
}
});
后台php photoUpload.php'
<?
require_once('classfile/guid.class.php');
if(!isset($_FILES['imgFile'])){
echo json_encode(array("success"=>false, 'msg'=>"Not get Imgfile"));
return;
}
$upfile=$_FILES['imgFile'];
$name=$upfile["name"];//上传文件的文件名
$type=$upfile["type"];//上传文件的类型
$size=$upfile["size"];//上传文件的大小
$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径
$error_cod=$upfile["error"];
if ($error_cod>0){
echo json_encode(array("success"=>false, 'msg'=>$error_cod));
}
$ext_file_name="";
switch ($type){
case 'image/pjpeg':
$okType=true;
$ext_file_name =".jpg";
break;
case 'image/jpeg':
$okType=true;
$ext_file_name =".jpg";
break;
case 'image/gif':
$okType=true;
$ext_file_name =".gif";
break;
case 'image/png':
$okType=true;
$ext_file_name =".png";
break;
}
if(!$okType){
echo json_encode(array("success"=>false, 'msg'=>"Not image "));
return;
}
$web_root="D:".DIRECTORY_SEPARATOR."Easy2PHP5".DIRECTORY_SEPARATOR."webSiteJfz".DIRECTORY_SEPARATOR;
$photo_tmp_path=$web_root."img".DIRECTORY_SEPARATOR."userimg".DIRECTORY_SEPARATOR."temp";
$temp_file_name= creat_guid(0).$ext_file_name;
$photo_tmp_file_name=$photo_tmp_path.DIRECTORY_SEPARATOR.$temp_file_name;
$photo_tmp_file_scr="img".DIRECTORY_SEPARATOR."userimg".DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR.$temp_file_name;
move_uploaded_file($tmp_name,$photo_tmp_file_name);
echo json_encode(array("success"=>true, 'msg'=> "ok","file_name"=>$photo_tmp_file_name,"file_scr"=>$photo_tmp_file_scr));
//echo json_encode(array("success"=>false, 'msg'=> json_encode($_FILES['imgFile'])));
return;
?>
guid.class.php // 生成唯一的图片文件名
<?
function creat_guid($long){
$uuid="";
if (function_exists('com_create_guid')){
$uuid=com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
//return $uuid;
}
if (!isset($long) || $long==0 ){
return substr($uuid,1, strlen($uuid)-2);
}else{
return $uuid;
}
}
php 编写 实现上传图片至服务器的函数<?php
class FileUpload{
private $filepath; //指定上传文件保存的路径
private $allowtype=array("gif","jpg","jpeg","png");//允许上传文件的类型
private $maxsize=1000000;//允许上传文件的最大值
private $israndname=true;//是否随机重命名,
private $originName;//源文件名字
private $tmpFileName;//临时文件名字
private $fileType;//上传后的文件类型,主要是文件后缀名
private $fileSize;//文件尺寸
private $newFileName;//新文件名字
private $errorName=0;//错误号
private $errorMess="";//用来提供错误报告
//用于对上传文件初始化
//指定上传路径 2·允许的类型 3·限制大小 4·是否使用随机文件名称
//让用户可以不用换位置传参数,后面参数给值不用按照位置或者必须有值
function __construct($options=array()){
foreach($options as $key=>$val){
$key = strtolower($key);
//查看用户参数中的数组下标是否和成员属性名相同
//get_class_vars(get_class($this))得到类属性的数组
//如果$key下标不在这个类属性的数组中,则退出for循环
if (!in_array($key,get_class_vars(get_class($this)))){
continue;
}
$this -> setOption($key,$val);
}
}
private function setOption($key,$val){
//让实例化后获取过来的数组下标 = 数组下标的值,这里即为构造函数初始化
//构造函数中调用,等于把所有属性初始化,将来可以直接访问
$this -> $key=$val;
}
private function getError(){
$str="上传文件{$this->originName}时出错";
switch($this -> errorNum){
case 4: $str.="没有文件被上传";
break;
case 3: $str.="文件只有部分上传";
break;
case 2: $str.="上传文件超过了表单的值";
break;
case 1: $str.="上传文件超过phpini的值";
break;
case -1: $str.="未允许的类型";
break;
case -2: $str.="文件过大上传文件不能超过{$this->maxsize}字节";
break;
case -3: $str.="上传文件失败";
break;
case -4: $str.="建立存放上传文件目录失效,请重新上传指定目录";
break;
case -5: $str.="必须指定上传文件的路径";
break;
default: $str.="未知错误";
}
return $str.'<br>';
}
//用来检查文件上传路径
private function checkFilePath(){
if(empty($this -> filepath)){
$this -> setOption("errorNum",-5);
return false;
}
if(!file_exists($this -> filepath) || !is_writable($this -> filepath)){
if(!@mkdir($this -> filepath,0755)){
$this -> setOption("errorNum",-4);
return false;
}
}
return true;
}
//用来检查上传文件尺寸大小
private function checkFileSize(){
if($this -> fileSize > $this ->maxsize){
$this -> setOption("errorNum",-2);
return false;
}else{
return true;
}
}
//用来检查文件上传类型
private function checkFileType(){
if(in_array(strtolower($this->fileType),$this -> allowtype)){
return true;
}else{
//如果$this->fileType这个类型 不在$this -> allowtype这个数组中,则把错误号变成-1
$this -> setOption("errorNum",-1);
return false;
}
}
private function setNewFileName(){
if($this -> israndname){
$this -> setOption("newFileName",$this->preRandName());
}else{
$this -> setOption("newFileName",$this -> originName);
}
}
//用于检查文件随机文件名
private function preRandName(){
$fileName=date("Ymdhis").rand(100,999);
return $fileName.".".$this -> fileType;
}
//用来上传一个文件
function uploadFile($fileField){
//检查文件路径
$return = true;
if(!$this -> checkFilePath()){
$this -> errorMess=$this -> getError();
return false;
}//获取文件信息
$name = $_FILES[$fileField]['name'];
$tmp_name = $_FILES[$fileField]['tmp_name'];
$size = $_FILES[$fileField]['size'];
$error = $_FILES[$fileField]['error'];
if(is_array($name)){//判断获取过来的文件名字是否为数组
$errors=array();//如果为数组则设置为一个数组错误号
for($i=0;$i<count($name);$i++){
//循环每个文件即每个类属性赋值或者说初始化属性值 或者初始化构造函数
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
if(!$this->checkFileSize() || !$this->checkFileType()){
//如果上面尺寸或者类型不对,则调用这个错误信息
$errors[$i]=$this->getError();
$return=false;
}
}else{
//这里是
$error[]=$this->getError();
$return=false;
}
if(!$return)
$this->setFiles();
}
if($return){
$fileNames=array();
for($i=0;$i<count($name);$i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
$this->setNewFileName();
if(!$this->copyFile()){
$errors=$this->getError();
$return=false;
}else{
$fileNames[$i]=$this->newFileName;
}
}
}
$this->newFileName=$fileNames;
}
$this->errorMess=$errors;
return $return;
}else{
//看看$name,$tmp_name,$size,$error这些是否赋值成功 否则返回FALSE
if($this -> setFiles($name,$tmp_name,$size,$error)){
//看看文件大小尺寸是否匹配,不匹配返回FALSE
if($this -> checkFileSize() $this -> checkFileType()){
//获取新文件名
$this->setNewFileName();
if($this->copyFile()){
return true;
}else{
return false;
}
}else{
$return=false;
}
}else{
$return=false;
}
if(!$return){
$this -> errorMess = $this ->getError();
return $return;
}
}
}
function copyFile(){//将文件从临时目录拷贝到目标文件夹
if(!$this->errorNum){
//如果传递来的路径有斜杠,则删除斜杠再加上斜杠
//./upload+./
$filepath=rtrim($this->filepath,'/').'/';
//./upload+./+加上随机后的新文件名和后缀
//这里指创建一个新的$filepath.这个文件 像占位符但是为空的
$filepath.=$this->newFileName;
//尝试着把临时文件$this->tmpFileName移动到$filepath下哪里覆盖原来的这个文件
if(@move_uploaded_file($this->tmpFileName,$filepath)){
return true;
}else{
$this->setOption('errorNum',-3);
return false;
}
}else{
return false;
}
}
//这里是为了其他剩余的属性进行初始化操作!
private function setFiles($name="",$tmp_name="",$size=0,$error=0){
//这里给错误号赋值
$this -> setOption("errorNum",$error);
//如果这里有错误,直接返回错误
if ($error){
return false;
}
$this -> setOption("originName",$name);//复制名字为源文件名
$this -> setOption("tmpFileName",$tmp_name);
$arrstr = explode(".",$name);//按点分割文件名,
//取分割后的字符串数组最后一个 并转换为小写,赋值为文件类型
$this -> setOption("fileType",strtolower($arrstr[count($arrstr)-1]));
$this -> setOption("fileSize",$size);
return true;
}
//用来获取上传后的文件名
function getNewFileName(){
return $this -> newFileName;
}
//上传失败,后则返回这个方法,就可以产看报告
function getErrorMsg(){
return $this -> errorMess;
}
}
?>
============================调用====================================
<?php
require("FileUpload.class.php");
//这里实例化后赋值为数组,数组的下标要对应类中属性的值,否则不能传递值,可以不分先后但是必须一致
$up = new FileUpload(array('israndname'=>'true',"filepath"=>"./upload/",'allowtype'=>array('txt','doc','jpg','gif'),"maxsize"=>1000000));
echo '<pre>';
if($up -> uploadFile("pic")){
print_r($up -> getNewFileName());
} else{
print_r($up -> getErrorMsg());
}
echo '<pre>';
?>
-------------------HTML-------------------------
<html>
<head>
<meta http-quive="content-type" content="text/html;charset=utf-8" ></meta>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
shoppic:<input type="file" name="pic[]"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="submit" name="sub" value="添加商品">
</form>
</body>
</html>
-------------------或者HTML-------------------------
<html>
<head>
<meta http-quive="content-type" content="text/html;charset=utf-8" ></meta>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
//区别在这里
shoppic:<input type="file" name="pic[]"><br>
shoppic:<input type="file" name="pic[]"><br>
shoppic:<input type="file" name="pic[]"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="submit" name="sub" value="添加商品">
</form>
</body>
</html>
=====================================================================
以上是自己总结的 还没有怎么精简加工过,仅供参考
以上不止可以上传图片,可以上自定义任何文件
关于php图片服务器的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php图片服务器 php图片管理系统的详细内容...