❶ php 圖片上傳
首先 他們是從file空間那裡取得的值。
然後判斷參數,具體如下:
$_files主要用在當需要上傳二進制文件的地方,錄入上傳一個abc.mp3文件,則伺服器端需要獲得該文件的相關信息,則通過變數$_files來取得。
$_FILES['userfile']['name']
客戶端機器文件的原名稱。
$_FILES['userfile']['type']
文件的 MIME 類型,需要瀏覽器提供該信息的支持,例如「image/gif」。
$_FILES['userfile']['size']
已上傳文件的大小,單位為位元組。
$_FILES['userfile']['tmp_name']
文件被上傳後在服務端儲存的臨時文件名。
$_FILES['userfile']['error']
和該文件上傳相關的錯誤代碼。['error'] 是在 PHP 4.2.0 版本中增加的。
注: 在 PHP 4.1.0 版本以前該數組的名稱為 $HTTP_POST_FILES,它並不像 $_FILES 一樣是自動全局變數。PHP 3 不支持 $HTTP_POST_FILES 數組。
如果表單中沒有選擇上傳的文件,則 PHP 變數 $_FILES['userfile']['size'] 的值將為 0,$_FILES['userfile']['tmp_name'] 將為 none。
❷ 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上傳圖片怎麼做
上傳類,保存文件名稱為 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="上傳">
❹ 怎樣用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
////////上傳文件////////////
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的pdo如何將圖片路徑和內容一起上傳謝謝了!
思路,大概思路是這樣的。一般都是將圖片上傳到伺服器自己設定的一個目錄下,同時將圖片名稱等信息插入數據表,資料庫存圖片名稱路徑就好了。不要將圖片的二進制信息存入資料庫,那樣很浪費資料庫空間非常不好(本人曾經這樣看教程做過)。至於你說的怎麼在另一個php文件引用,要麼查詢資料庫取到圖片名稱路徑。具體上傳代碼可以參考:HTML代碼<form enctype="multipart/form-data" action="__URL__" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /></form>php代碼<?php print_r($_FILES['userfile']);//列印看看結果,然後查看手冊$_FILES研究下吧?>
❼ php 非同步上傳圖片幾種方法總結
代碼如下
form action="upload.php" id="form1" name="form1" enctype="multipart/form-data" method="post" target="uploadIframe"> <!--上傳圖片頁面 --> </form> <iframe name="uploadIframe" id="uploadIframe" style="display:none"></iframe>
然後後台處理完上傳圖片邏輯後返回給前台,利用ajax修改當前頁面DOM對象實現無刷新上傳圖片的友好功能。
實例
代碼如下
a.html <form enctype="multipart/form-data" action="a.php" target="ifram_sign" method="POST"> <input name="submit" id="submit" value="" type="hidden"> <label>上傳文件: <input name="test_file" type="file" id="test_file" size="48"></label> <input type="image" value="立即上傳" id="submit_btn"> </form><iframe name="ifram_sign" src="" frameborder="0" height="0" width="0" marginheight="0" marginwidth="0"></iframe>
php代碼:
代碼如下
<?php
if ($_files["test_file"]["error"] > 0)
{
echo "Error: " . $_files["test_file"]["error"] . "<br />";
}
else
{
//這里的判斷圖片屬性的方法就不寫了。自己擴展一下。
$filetype=strrchr($_files["test_file"]["name"],".");
$filetype=substr($filetype,1,strlen($filetype));
$filename="img/".time("YmdHis").".".$filetype;
move_uploaded_file($_files["test_file"]["tmp_name"],$filename);
echo '<script >alert(1)</script>';
$return="parent.document.getElementByIdx_x('mpic".$pageset_id."').innerhtml='".$dataimgpath."'";
echo "<script >alert('上傳成功')</script>";
echo "<script>{$return}</script>";
}
?>
其實jquery ajax圖片非同步上傳
html:
<!DOCTYPE html PUBLIC "-//W3C//dtd Xhtml 1.0 transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
<title>圖片非同步上傳</title>
</head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link type="text/css" rel="stylesheet" href="css/index.css">
<body>
<div class="frm">
<form name="uploadFrom" id="uploadFrom" action="upload.php" method="post" target="tarframe" enctype="multipart/form-data">
<input type="file" id="upload_file" name="upfile">
</form>
<iframe src="" width="0" height="0" style="display:none;" name="tarframe"></iframe>
</div>
<div id="msg">
</div>
</body>
</html>
index.js
$(function(){
$("#upload_file").change(function(){
$("#uploadFrom").submit();
});
});
function stopSend(str){
var im="<img src='upload/images/"+str+"'>";
$("#msg").append(im);
}
upload.php
<?php
$file=$_files['upfile'];
$name=rand(0,500000).dechex(rand(0,10000)).".jpg";
move_uploaded_file($file['tmp_name'],"upload/images/".$name);
//調用iframe父窗口的js 函數
echo "<script>parent.stopSend('$name')</script>";
?>
非同步上傳圖片幾種方法
❽ 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 如何上傳圖片和文字
直接form表單加上上傳的屬性,在php那裡判斷下 $_FILE裡面的臨時文件是否存在,存在就遍歷,然後定義一個數組。把上傳到伺服器端的臨時文件挪到指定位置,然後把路徑存到數組裡面,最終存到資料庫。就實現上傳了