當前位置:首頁 » 編程語言 » php發送圖片

php發送圖片

發布時間: 2022-04-20 13:23:20

php 如何上傳圖片和文字

直接form表單加上上傳的屬性,在php那裡判斷下 $_FILE裡面的臨時文件是否存在,存在就遍歷,然後定義一個數組。把上傳到伺服器端的臨時文件挪到指定位置,然後把路徑存到數組裡面,最終存到資料庫。就實現上傳了

❷ 用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
header('Content-type:text/html;charset=UTF-8');
if(!empty($_FILES)){
$fileInfo=$_FILES['myfile'];
print_r($_FILES);
if($fileInfo['error']>0){
switch($fileInfo['error']){
case 1:
$msg_error='上傳文件超過了php配置文件中UPLOAD_MAX_FILESIZE選項的值';
break;
case 2:
$msg_error='超過了表單MAX_FILE_SIZE限制的大小';
break;
case 3:
$msg_error='文件部分上傳';
break;
case 4:
$msg_error='沒有文件上傳';
break;
case 6:
$msg_error='沒有找到臨時目錄';
break;
case 7:
case 8:
$msg_error='系統錯誤';
break;

}
exit($msg_error);
}
$filename=$fileInfo['name'];
$ext=strtolower(substr($filename,strrpos($filename,'.')+1));
$allowExt=array('txt','html','png','gif','jpeg');
if(!in_array($ext,$allowExt)){
exit('上傳文件類型錯誤');
}
$maxSize=2097152;
if($fileInfo['size']>$maxSize){
exit('上傳文件過大');
}
if(!is_uploaded_file($fileInfo['tmp_name'])){

exit('文件不是通過HTTP POST方式提交上來的');
}

//確保文件名字唯一,防止同名文件被覆蓋
$uniqName=md5(uniqid(microtime(true),true)).'.'.$ext;

$path="uploads";
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$destination=$path.'/'.$uniqName;
if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
exit('文件上傳失敗');
}
echo '上傳成功';

}

❹ 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聊天怎麼發送圖片

說白了圖片路徑存儲在資料庫中,然後你使用php開發聊天的時候,給他發路徑過去,只要他那可以顯示成<img src="路徑">即可顯示圖片

❻ 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如何將圖片傳到文本域中

文本域是指textarea嗎?直接不可以,需要做一層轉換,如將文本域的內容同步到一個iframe里。另外可以用現成的插件如:ckeditor,kindeditor,xheditor,ueditor這樣的富媒體編輯器才可以。
上傳插件可以uploadify。

❽ 怎樣用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寫一個上傳圖片的程序 謝謝

<?php
$uptypes=array('image/jpg', //上傳文件類型列表
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'application/x-shockwave-flash',
'image/x-png');
$max_file_size=5000000; //上傳文件大小限制, 單位BYTE
$destination_folder="upload/"; //上傳文件路徑
$watermark=0; //是否附加水印(1為加水印,其他為不加水印);
$watertype=1; //水印類型(1為文字,2為圖片)
$waterposition=1; //水印位置(1為左下角,2為右下角,3為左上角,4為右上角,5為居中);
$waterstring="newphp.site.cz"; //水印字元串
$waterimg="xplore.gif"; //水印圖片
$imgpreview=1; //是否生成預覽圖(1為生成,其他為不生成);
$imgpreviewsize=1/2; //縮略圖比例
?>
<html>
<head>
<title>M4U BLOG - fywyj.cn</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}
strong{font-size:12px;}
aink{color:#0066CC;}
a:hover{color:#FF6600;}
aisited{color:#003366;}
a:active{color:#9DCC00;}
table.itable{}
td.irows{height:20px;background:url("index.php?i=dots" repeat-x bottom}</style>
</head>
<body>
<center><form enctype="multipart/form-data" method="post" name="upform">
上傳文件: <br><br><br>
<input name="upfile" type="file" style="width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17">
<input type="submit" value="上傳" style="width:30;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17"><br><br><br>
允許上傳的文件類型為:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf <br><br>
<a href="index.php">返回</a>
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "<font color='red'>文件不存在!</font>";
exit;
}

$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//檢查文件大小
{
echo "<font color='red'>文件太大!</font>";
exit;
}

if(!in_array($file["type"], $uptypes))
//檢查文件類型
{
echo "<font color='red'>只能上傳圖像文件或Flash!</font>";
exit;
}

if(!file_exists($destination_folder))
mkdir($destination_folder);

$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
echo "<font color='red'>同名文件已經存在了!</a>";
exit;
}

if(!move_uploaded_file ($filename, $destination))
{
echo "<font color='red'>移動文件出錯!</a>";
exit;
}

$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
echo " <font color=red>已經成功上傳</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
echo " 寬度:".$image_size[0];
echo " 長度:".$image_size[1];
if($watermark==1)
{
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($destination);
break;
case 2:
$simage =imagecreatefromjpeg($destination);
break;
case 3:
$simage =imagecreatefrompng($destination);
break;
case 6:
$simage =imagecreatefromwbmp($destination);
break;
default:
die("<font color='red'>不能上傳此類型文件!</a>");
exit;
}

image($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);

switch($watertype)
{
case 1: //加水印字元串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印圖片
$simage1 =imagecreatefromgif("xplore.gif");
image($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}

switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case 2:
imagejpeg($nimage, $destination);
break;
case 3:
imagepng($nimage, $destination);
break;
case 6:
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}

//覆蓋原上傳文件
imagedestroy($nimage);
imagedestroy($simage);
}

if($imgpreview==1)
{
echo "<br>圖片預覽:<br>";
echo "<a href=\"".$destination."\" target='_blank'><img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
echo " alt=\"圖片預覽:\r文件名:".$destination."\r上傳時間:\" border='0'></a>";
}
}
?>
</center>
</body>
</html>

熱點內容
解壓喪屍片 發布:2024-04-27 01:02:28 瀏覽:369
編程師加班 發布:2024-04-27 00:49:24 瀏覽:909
lol四川伺服器雲空間 發布:2024-04-27 00:42:08 瀏覽:933
卡宴怎麼看配置 發布:2024-04-27 00:41:08 瀏覽:941
央視影音緩存視頻怎麼下載視頻 發布:2024-04-27 00:25:55 瀏覽:583
手機緩存的視頻怎麼看 發布:2024-04-27 00:11:05 瀏覽:57
shell腳本平方計算公式 發布:2024-04-26 23:29:26 瀏覽:187
比較實惠的雲伺服器 發布:2024-04-26 23:24:57 瀏覽:974
怎麼增加電腦緩存 發布:2024-04-26 23:23:46 瀏覽:451
android調試gdb 發布:2024-04-26 23:22:27 瀏覽:99