很多站长朋友们都不太清楚php图片上传模板,今天小编就来给大家整理php图片上传模板,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP怎样上传图片以及预览图片? 2、 php上传图片文件常用的几个方法 3、 PHP上传图片怎么做? 4、 phpcms v9后台上传图片功能前台调用方法 5、 简单PHP上传图片写法 6、 php怎样上传图片 PHP怎样上传图片以及预览图片?本地图片,就搞个img,设置他的src就可以实现;
参考如下:
<div class="column " style="width: 400px; margin-left: 200px;" id="imageShow">
<div id="productImageNew">@*用于图片预览*@
</div>
<div id="productImage">
<div class="widget the-common-margin-top" style="height: 400px; border: 1px solid #eeeeee;
padding: 3px;">
<img id="imgHolder" style="max-height: 390px; max-width: 390px;" />
</div>
</div>
</div>
<form id="formImageUpload" name="formImageUpload" method="post" action="/DocTeam/ProductsImage/UploadImage"
enctype="multipart/form-data">
<div id="fileDiv">
<input type="file" id="theFile" name="theFile" size="20" style="cursor: pointer;
width: 65px; height: 60px; position: absolute; filter: alpha(opacity:1); -moz-opacity: 0;
opacity: 0; z-index: 102;" />
</div>
<input type="hidden" name="imageId_hide" id="imageId_hide" />
</form>
<div id="cover" style="position: absolute; background-color: White; z-index: 10;
filter: alpha(opacity=100); -moz-opacity: 1; opacity: 1; overflow: auto; width: 400px;">
<input id="selectImage" type="button" style="width: 65px; height: 60px;" value="Select" />
<br />
<br />
<input type="button" value="Upload" id="imageUpload" style="width: 65px; height: 60px;"
disabled="disabled" onclick="javascript:uploadImage();" />
</div>
//js本地图片预览,兼容ie[6-9]、火狐、Chrome17+、Opera11+、Maxthon3
function PreviewImage(fileObj, imgPreviewId, divPreviewId) {
var allowExtention = ".jpg,.bmp,.gif,.png"; //允许上传文件的后缀名document.getElementById("hfAllowPicSuffix").value;
var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".") + 1).toLowerCase();
var browserVersion = window.navigator.userAgent.toUpperCase();
if (allowExtention.indexOf(extention) > -1) {
if (fileObj.files) {//HTML5实现预览,兼容chrome、火狐7+等
if (window.FileReader) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById(imgPreviewId).setAttribute("src", e.target.result);
}
reader.readAsDataURL(fileObj.files[0]);
} else if (browserVersion.indexOf("SAFARI") > -1) {
alert("不支持Safari6.0以下浏览器的图片预览!");
}
} else if (browserVersion.indexOf("MSIE") > -1) {
if (browserVersion.indexOf("MSIE 6") > -1) {//ie6
document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);
} else {//ie[7-9]
fileObj.select();
if (browserVersion.indexOf("MSIE 9") > -1)
fileObj.blur(); //不加上document.selection.createRange().text在ie9会拒绝访问
var newPreview = document.getElementById(divPreviewId + "New");
if (newPreview == null) {
newPreview = document.createElement("div");
newPreview.setAttribute("id", divPreviewId + "New");
}
var a = document.selection.createRange().text;
// newPreview.style.width = document.getElementById(imgPreviewId).width + "px";
// newPreview.style.height = document.getElementById(imgPreviewId).height + "px";
//newPreview.style.width = 390 + "px";
newPreview.style.height = 390 + "px";
newPreview.style.border = "solid 1px #eeeeee";
newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + document.selection.createRange().text + "')";
var tempDivPreview = document.getElementById(divPreviewId);
// tempDivPreview.parentNode.insertBefore(newPreview, tempDivPreview);
newPreview.style.display = "block";
tempDivPreview.style.display = "none";
}
} else if (browserVersion.indexOf("FIREFOX") > -1) {//firefox
var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox\/([\d.]+)/)[1]);
if (firefoxVersion < 7) {//firefox7以下版本
document.getElementById(imgPreviewId).setAttribute("src", fileObj.files[0].getAsDataURL());
} else {//firefox7.0+
document.getElementById(imgPreviewId).setAttribute("src", window.URL.createObjectURL(fileObj.files[0]));
}
} else {
document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);
}
} else {
alert("仅支持" + allowExtention + "为后缀名的文件!");
fileObj.value = ""; //清空选中文件
if (browserVersion.indexOf("MSIE") > -1) {
fileObj.select();
document.selection.clear();
}
fileObj.outerHTML = fileObj.outerHTML;
}
}
function setTheFileButton_Cover_SelectImageButton() {
// debugger;
// var position = $("#selectImage", "#cover").position();
// var css = { top: position.top, left: position.left };
// $("#theFile", "#fileDiv").css(css);
}
var $imgHolder = $('#imgHolder', "#productImage");
var tempDiv = $("#temp_div");
$("#select", "#cover").click(function () {
$("#theFile", "#fileDiv").click().select();
});
$("#theFile", "#fileDiv").click(function () {
$(this).blur();
});
$("#theFile", "#fileDiv").change(function () {
PreviewImage(this, 'imgHolder', 'productImage');
setTheFileButton_Cover_SelectImageButton();
// alert("预览已生成!");
$("#imageUpload").prop("disabled", false);
});
php上传图片文件常用的几个方法你好,要先建立一个html代码
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
然后创建upload_file文件用$_FILE判断文件,下面是判断文件的具体信息
$_FILES["file"]["name"] - 被上传文件的名称
$_FILES["file"]["type"] - 被上传文件的类型
$_FILES["file"]["size"] - 被上传文件的大小,以字节计
$_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
$_FILES["file"]["error"] - 由文件上传导致的错误代
希望对你有帮助!
PHP上传图片怎么做?上传类,保存文件名称为 uppoo.php:
<?php
class upphoto{
public $previewsize=0.125 ; //预览图片比例
public $preview=0; //是否生成预览,是为1,否为0
public $datetime; //随机数
public $ph_name; //上传图片文件名
public $ph_tmp_name; //图片临时文件名
public $ph_path="./userimg/"; //上传文件存放路径
public $ph_type; //图片类型
public $ph_size; //图片大小
public $imgsize; //上传图片尺寸,用于判断显示比例
public $al_ph_type=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'); //允许上传图片类型
public $al_ph_size=1000000; //允许上传文件大小
function __construct(){
$this->set_datatime();
}
function set_datatime(){
$this->datetime=date("YmdHis");
}
//获取文件类型
function get_ph_type($phtype){
$this->ph_type=$phtype;
}
//获取文件大小
function get_ph_size($phsize){
$this->ph_size=$phsize."<br>";
}
//获取上传临时文件名
function get_ph_tmpname($tmp_name){
$this->ph_tmp_name=$tmp_name;
$this->imgsize=getimagesize($tmp_name);
}
//获取原文件名
function get_ph_name($phname){
$this->ph_name=$this->ph_path.$this->datetime.strrchr($phname,"."); //strrchr获取文件的点最后一次出现的位置
//$this->ph_name=$this->datetime.strrchr($phname,"."); //strrchr获取文件的点最后一次出现的位置
return $this->ph_name;
}
// 判断上传文件存放目录
function check_path(){
if(!file_exists($this->ph_path)){
mkdir($this->ph_path);
}
}
//判断上传文件是否超过允许大小
function check_size(){
if($this->ph_size>$this->al_ph_size){
$this->showerror("上传图片超过2000KB");
}
}
//判断文件类型
function check_type(){
if(!in_array($this->ph_type,$this->al_ph_type)){
$this->showerror("上传图片类型错误");
}
}
//上传图片
function up_photo(){
if(!move_uploaded_file($this->ph_tmp_name,$this->ph_name)){
$this->showerror("上传文件出错");
}
}
//图片预览
function showphoto(){
if($this->preview==1){
if($this->imgsize[0]>2000){
$this->imgsize[0]=$this->imgsize[0]*$this->previewsize;
$this->imgsize[1]=$this->imgsize[1]*$this->previewsize;
}
echo("<img src=\"{$this->ph_name}\" width=\"{$this->imgsize['0']}\" height=\"{$this->imgsize['1']}\">");
}
}
//错误提示
function showerror($errorstr){
echo "<script language=java script>alert('$errorstr');location='java script:history.go(-1);';</script>";
exit();
}
function save(){
$this->check_path();
$this->check_size();
$this->check_type();
$this->up_photo();
$this->showphoto();
}
}
?>
这里是使用的方法:
<?php
header("Content-Type:text/html; charset=utf-8");
//类的实例化:
include("uppoo.php");//类的文件名是upoop.php
$up=newupphoto;
$submit=$_POST['submit'];
if($submit=="上传"){
$up->get_ph_tmpname($_FILES['photo']['tmp_name']);
$up->get_ph_type($_FILES['photo']['type']);
$up->get_ph_size($_FILES['photo']['size']);
$up->get_ph_name($_FILES['photo']['name']);
$up->save();
}
?>
//上传图片的HTML:
<form action="upphoto.php?action=act" method="post" enctype="multipart/form-data">
图片来源:<input type="file" name="photo">
<input type="submit" name="submit" value="上传">
phpcms v9后台上传图片功能前台调用方法上传模板$this->isadmin改为1,swf2ckeditor.js flashupload方法index.php改为/index.php;attachment xjtUpload.php function swfupload注释else第一行就OK了。
简单PHP上传图片写法上传的最后部分代码 这里 如果$ok为 0 那么这个 条件式就不能成立了
因为数字 0 等于false 其他的数字全部是 true
你把$ok 改成 0以外的任何数字 都是 一样的
if ($ok $error=='0'){
move_uploaded_file($tmp_name,'up/'. $name);
echo "上传成功";
}
php怎样上传图片一下代码可以实现简单的(单个的)文件上传,你看看吧:有更好的方法可以讨论
<?php
////////上传文件////////////
if(isset($_POST["sub"])){
//$upfile="upload_file/".$_FILES["file"]["name"];
$name=time();//定义变量,保存图片名,以防图片的名字相同
echo $name;
$name.=strrchr($_FILES["file"]["name"],".");//上传文件的名称
echo $name;
$num=rand(1,10);
$type=$_FILES["file"]["type"];
$size=$_FILES["file"]["size"];
$tmp_name=$_FILES["file"]["tmp_name"];
if($_FILES["fiel"]["error"]>0){
echo "上传文件有误:".$_FILES["file"]["error"]."<br/>";
}else{
echo "上传文件名为:".$name."<br>";
echo "上传文件类型为:".$type."<br>";
echo "上传文件大小为:".($size/1024)."<br>";
echo "上传到:".$tmp_name."<br>";
if(file_exists("upload_file/$name")){
echo "已经存在";
}else{
if(move_uploaded_file($tmp_name,"upload_file/$name")){
echo $name."上传成功";
}else{
echo $name."上传失败";
}
}
}
}
?>
<form method="post" action="php_upload.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="sub" value="upload">
<input type="reset" name="res" value="reset"/>
</form>
关于php图片上传模板的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php图片上传模板 php实现图片上传数据库的详细内容...