『壹』 php怎樣上傳圖片以及預覽圖片
本地圖片,就搞個img,設置他的src就可以實現;
參考如下:
<div class="column " style="width: 400px; margin-left: 200px;" id="imageShow">
<div id="proctImageNew">@*用於圖片預覽*@
</div>
<div id="proctImage">
<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/ProctsImage/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', "#proctImage");
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', 'proctImage');
setTheFileButton_Cover_SelectImageButton();
// alert("預覽已生成!");
$("#imageUpload").prop("disabled", false);
});
『貳』 在php類型網站的後台是如何上傳視頻文件呢
大的視頻文件只能同多ftp工具上傳到伺服器上,然後後台手動添加地址的。一般伺服器的默認配置允許上傳的最大文件2M。
『叄』 php上傳圖片文件常用的幾個方法
你好,要先建立一個html代碼
<formaction="upload_file.php"method="post"
enctype="multipart/form-data">
<labelfor="file">Filename:</label>
<inputtype="file"name="file"id="file"/>
<br/>
<inputtype="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怎麼上傳視頻
對於PHP來說,上傳視頻就是當一個普通的文件來處理的.比如一個圖片的上傳.
可能出現錯誤的地方是 一般PHP的配置中都限定了上傳文件最大的值.
而視頻相對來說是比較大的文件,所以你需要去修改配置文件.
『伍』 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."上傳失敗";
}
}
}
}
?>
<formmethod="post"action="php_upload.php"enctype="multipart/form-data">
<inputtype="file"name="file">
<inputtype="submit"name="sub"value="upload">
<inputtype="reset"name="res"value="reset"/>
</form>
『陸』 php怎麼添加視頻教程
上傳前選擇上傳文件類型時,通過表單的file控制項是無法限制上傳文件類型的,上傳時通過$_FILES["表單控制項名"]["name"]取得上傳文件的文件名,再來取他的擴展名
例如:
<?
if($_POST['addsubmit'])
{
if($_FILES["c_filename"]["name"])
{
$c_name_zh=$_FILES["c_filename"]["name"];
$exnt=substr($c_name_zh, -4);//這個變數存的就是擴展名
/////通過擴展名再來判斷是否進行move_uploaded_file操作
}
}
?>
<form name="form_add" id="form_add" target="_self" method="post" enctype="multipart/form-data" >
<input type="file" name="c_filename" id="c_filename" size="30">
<input type="subimt" name="addsubmit" value="上傳">
</form>
『柒』 用php上傳圖片怎麼做
上傳圖片原理:首先判斷文件類型是否為圖片格式,若是則上傳文件,然後重命名文件(一般都是避免上傳文件重名,現在基本上都是以為時間來命名),接著把文件上傳到指定目錄,成功上傳後輸出上傳圖片的預覽。
1.首先我們開始判斷文件類型是否為圖片類型用到的函數
{
strrchr:查找字元串在另一個字元串中最後一次出現的位置,並返回從該位置到字元串結尾的所有字元。
substr: 取部份字元串。
$HTTP_POST_FILES['file']['name']:獲取當前上傳的文件全稱。
}
圖片類型就是「.」後面的字元(比如:一個文件名稱為XXX.JPG 那麼它的類型就是「.」後面的JPG)。 我們可以用PHP中的函數來截取上傳者文件名字的。我們來寫個獲取文件類型的函數
<?
function type()
{
return substr(strrchr($HTTP_POST_FILES['file']['name'],'.'),1);
}
?>
2.若是則上傳文件,然後重命名文件用到的函數
{ strtolower:把字元串的字母全部轉換為小寫字母. in_array: 函數在數組中搜索給定的值。 implode:函數把數組元素組合為一個字元串 random:隨機生成的數 $_FILES['userfile']['name']:上傳文件名稱 $uploaddir:自己定義的變數。比如在同一個文件夾裡面,你想把上傳的文件放到這個文件夾的FILE文件夾下,你可以這樣定義$uploaddir="./file/";注意寫法 } 這邊會出現很多問題,第一先寫一個能上傳類型的數組。第二判斷文件合法性。第三給文件重名。*(這邊判斷文件大小就不寫了)先定義允許上傳文件的類型數組:$type=array("jpg","gif","bmp","jpeg","png");第二用一個IF。。else。。寫一個判斷文件合法性的控制流語句。if(!in_arry(strtolower(type()),$type))//如果不存在能上傳的類型 { $text=implode('.',$type); echo "您只能上傳以下類型文件: ",$text,"<br>"; } 下面就是給他們重新命名了,else { $filename=explode(".",$_FILES['userfile']['name']);//把上傳的文件名以「.」好為准做一個數組。 $time=date("m-d-H-i-s");//去當前上傳的時間 $filename[0]=$time;//取文件名t替換 name=implode(".",$filename); //上傳後的文件名 $uploadfile=$uploaddir.$name;//上傳後的文件名地址 } 3.最後把文件上傳到指定目錄,成功上傳後輸出上傳圖片的預覽用到的函數{ move_uploaded_file:執行上傳文件 } if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) { echo "<center>您的文件已經上傳完畢 上傳圖片預覽: </center><br><center><img src='$uploadfile'></center>"; echo"<br><center><a href='javascrīpt:history.go(-1)'>繼續上傳</a></center>"; } else { echo"傳輸失敗!"; }
『捌』 怎樣用php實現上傳圖片到資料庫
php實現上傳圖片保存到資料庫的方法。具體分析如下:
php 上傳圖片,一般都使用move_uploaded_file方法保存在伺服器上。但如果一個網站有多台伺服器,就需要把圖片發布到所有的伺服器上才能正常使用(使用圖片伺服器的除外)
如果把圖片數據保存到資料庫中,多台伺服器間可以實現文件共享,節省空間。
首先圖片文件是二進制數據,所以需要把二進制數據保存在mysql資料庫。
mysql資料庫提供了BLOB類型用於存儲大量數據,BLOB是一個二進制對象,能容納不同大小的數據。
BLOB類型有以下四種,除存儲的最大信息量不同外,其他都是一樣的。可根據需要使用不同的類型。
TinyBlob 最大 255B
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
數據表photo,用於保存圖片數據,結構如下:
CREATETABLE`photo`(
`id`int(10)unsignedNOTNULLauto_increment,
`type`varchar(100)NOTNULL,
`binarydata`mediumblobNOTNULL,
PRIMARYKEY(`id`)
)ENGINE=MyISAMDEFAULTCHARSET=latin1AUTO_INCREMENT=1;
upload_image_todb.php代碼如下:
<?php
//連接資料庫
$conn=@mysql_connect("localhost","root","")ordie(mysql_error());
@mysql_select_db('demo',$conn)ordie(mysql_error());//判斷action
$action=isset($_REQUEST['action'])?$_REQUEST['action']:'';
//上傳圖片
if($action=='add'){
$image=mysql_escape_string(file_get_contents($_FILES['photo']['tmp_name']));
$type=$_FILES['photo']['type'];
$sqlstr="insertintophoto(type,binarydata)values('".$type."','".$image."')";
@mysql_query($sqlstr)ordie(mysql_error());
header('location:upload_image_todb.php');
exit();
//顯示圖片
}elseif($action=='show'){
$id=isset($_GET['id'])?intval($_GET['id']):0;
$sqlstr="select*fromphotowhereid=$id";
$query=mysql_query($sqlstr)ordie(mysql_error());
$thread=mysql_fetch_assoc($query);
if($thread){
header('content-type:'.$thread['type']);
echo$thread['binarydata'];
exit();
}
}else{
//顯示圖片列表及上傳表單
?>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="content-type"content="text/html;charset=utf-8">
<title>uploadimagetodbdemo</title>
</head>
<body>
<formname="form1"method="post"action="upload_image_todb.php"enctype="multipart/form-data">
<p>圖片:<inputtype="file"name="photo"></p>
<p><inputtype="hidden"name="action"value="add"><inputtype="submit"name="b1"value="提交"></p>
</form>
<?php
$sqlstr="select*fromphotoorderbyiddesc";
$query=mysql_query($sqlstr)ordie(mysql_error());
$result=array();
while($thread=mysql_fetch_assoc($query)){
$result[]=$thread;
}
foreach($resultas$val){
echo'<p><img
src="upload_image_todb.php?action=show&id='.$val['id'].'&t='.time().'"
width="150"></p>';
}
?>
</body>
</html>
<?php
}
?>
程序運行截圖和資料庫截圖:
『玖』 請大家幫忙,PHP 圖片上傳怎麼做 需要建資料庫嗎謝謝~~~~~~~~~~·
只是上傳顯示的話,就不用建資料庫。如果要是在前台頁面調用的話,就要建立資料庫了。你可以到php100視頻教程裡面有一節是專門講圖片上傳到
『拾』 如何實現用php上傳視頻文件
<?php
date_default_timezone_set('PRC');
functionuploadFile($file)
{
if($file["error"]>0){
echo"ReturnCode:".$file["error"]."<br/>";
}else{
$filename=md5(uniqid().$file["name"]).'.'.getExt($file['name']);
move_uploaded_file($file["tmp_name"],"upload/".$filename);
return$filename;
}
}
functiongetExt($file)
{
$arr=explode('.',$file);
returnend($arr);
}
$file=$_FILES['fileName'];
uploadFile($file);
?>