當前位置:首頁 » 安卓系統 » androidphp上傳圖片

androidphp上傳圖片

發布時間: 2022-11-03 02:13:04

php 如何上傳圖片和文字

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

㈡ php如何實現上傳圖片文件,並替換

首先建立兩個文件: change.html 和 change.php
change.html 文件的表單代碼如下:
<html>
<head>
<title>change file example.</title>
<meta charset="UTF-8">
</head>
<body>
<form method="post" action="changefile.php" enctype="multipart/form-data">
<table border=0 cellspacing=0 cellpadding=0 align=center width="100%">
<tr>
<td width=55 height=20 align="center">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
文件:
</td>
<td>
<input name="file" type="file" />
<input type="submit" name="submit" value="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>

這里有幾個要注意的地方,首先看這句<form method="post" action="change.php" enctype="multipart/form-data">,這里我們採用POST方法,個別瀏覽器還支持PUT方法,當然這需要對腳本進行修改,我並不建議這么做。表單中必須設置enctype="multipart/form-data,這樣,伺服器就知道上傳文件帶有常規表單信息,記住,這個是必須設置的。此外還需要一個隱藏域來限制上傳文件的最大長度:<input type="hidden" name="MAX_FILE_SIZE" value="2000000">,這里name必須設置成MAX_FILE_SIZE,其值就是上傳文件的最大長度,單位是B,這里我限製成2M。再看這句:<input name="file" type="file" value="瀏覽" >,type="file"說明了文件類型,這樣一個基本的上傳文件介面就完成了,接下來講講如何用PHP來處理上傳的文件,此外你的php.ini中設置的上傳文件最大長度可能會影響到你的實際上傳,請根據實際情況修改,另PHP的上傳是先傳到臨時目錄,在移至指定目錄的,了;臨時目錄的可根據需要修改,也可使用默認值……
以下為表單提交change.php文件代碼,來看看這個文件都有什麼:
<?php
header("content-type:text/html;charset=utf-8");

/**
* @param string $oldfile 需要更換的文件名(包含具體路徑名)
*/
function changeFile($oldfile){
$newfile = $_FILES['file']['name'];//獲取上傳文件名
$fileclass = substr(strrchr($newfile, '.'), 1);//獲取上傳文件擴展名,做判斷用
$type = array("jpg", "gif", "bmp", "jpeg", "png");//設置允許上傳文件的類型
if(in_array(strtolower($fileclass), $type)){
if(file_exists($oldfile)){
unlink($oldfile);
}

if(is_uploaded_file($_FILES['file']['tmp_name'])){//必須通過 PHP 的 HTTP POST 上傳機制所上傳的
if(move_uploaded_file($_FILES['file']['tmp_name'], $oldfile)){
//輸出圖片預覽
echo "<center>您的文件已經上傳完畢 上傳圖片預覽: </center><br><center><img src='$oldfile'></center>";
}
}else{
echo "<center>上傳失敗,文件大於2M,請重新上傳!</center>";
}
}else{
$text = implode(",", $type);
echo "<center>您只能上傳以下類型文件:", $text, "</center><br>";
// echo "<script>alert('您只能上傳以下類型文件:$text')</script>";
}
}

changeFile("./files/1.png");

剛看這些你可能有點暈~~,慢慢看,你就會發現其實這玩意SO EASY!!先講下原理,該程序以上傳圖片為例,先判斷文件類型是否為圖片格式,若是則上傳文件,接著上傳文件到並替換指定文件,成功上傳則輸出上傳的圖片預覽。這里要對程序中一些函數作些解釋。先看substr(strrchr($newfile, '.'), 1), strrchar()函數有什麼作用呢,我舉個例子大家就知道,比如一個圖片文件 pic.jpg,我們用 strrchar()處理,strrchr(pic.jpg,'.'),它將返回.jpg,明白了嗎?該函數返回指定字元在該字元串最後出現的位置後的字元串。配合 substr() 我們就可以取到jpg,這樣我們就得到了文件的後綴名,來判斷上傳文件是否符合指定格式。本程序把指定的格式放在一個數組中,實際使用時可根據需要添加。
接著,我們調用判斷文件類型的函數,並將其轉化為小寫strtolower($_FILES['file']['name']),這里有個很關鍵的東東$_FILES ,這是個超級全局數組,保存了需要處理的表單數據,如果開啟了register_globals,也可以直接訪問,但這是不安全的。看剛才那個上傳介面<input name="file" type="file">,根據這個表單名稱,我們可以得到很多信息:
$_FILES['file']['name']-- 得到文件名稱
$_FILES['file']['tmp_name']--得到臨時存儲位置
$_FILES['file']['size']--得到文件大小
$_FILES['file']['type']--得到文件MIME類型
得到這些信息,就可以輕松判斷文件的信息了,是不是很方便?^_^,接下來還有一些函數需要了解,file_exists()--判斷指定目錄是否存在,不存在我們當然不能上傳(好像是廢話!),is_uploaded_file--判斷文件是否已經通過HTTP POST上傳,move_uploaded_file--將上傳文件移至指定目錄。成功上傳,我們就輸出預覽,否則輸出上傳失敗……

㈢ 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手機站,怎麼上傳圖片給伺服器(提交給介面api處理)

上傳操作可以使用FTP來實現,用php即可調用。

FTP 是File Transfer Protocol(文件傳輸協議)的英文簡稱,而中文簡稱為「文傳協議」。用於Internet上的控制文件的雙向傳輸。同時,它也是一個應用程序(Application)。基於不同的操作系統有不同的FTP應用程序,而所有這些應用程序都遵守同一種協議以傳輸文件。在FTP的使用當中,用戶經常遇到兩個概念:"下載"(Download)和"上傳"(Upload)。"下載"文件就是從遠程主機拷貝文件至自己的計算機上;"上傳"文件就是將文件從自己的計算機中拷貝至遠程主機上。用Internet語言來說,用戶可通過客戶機程序向(從)遠程主機上傳(下載)文件。

㈤ 安卓上傳的圖片,PHP伺服器怎麼接收

sybase_connect連上資料庫。
語法: int sybase_connect(string [servername], string [username], string [password]);
返回值: 整數函數種類: 資料庫功能 本函數用來打開與 Sybase 資料庫的連接。
參數 servername 為欲連上的資料庫伺服器名稱。
參數 username 及 password 可省略,分別為連接使用的帳號及密碼。
使用本函數需注意早點關閉資料庫,以減少系統的負擔。
連接成功則返回資料庫的連接代號,失敗返回 false 值。

㈥ php怎樣上傳圖片

多文件非同步上傳最好用swfupload 啦。。。上傳圖片還可以窗口多選。。。PHP+swfupload 兼容十分好的。。。你可以去官網看DEMO演示。。。

㈦ php怎麼接收安卓上傳的圖片

print_r($_FILES); print_r($_POST); echo file_get_contents('php://input'); $arr = $GLOBALS["HTTP_RAW_POST_DATA"]; print_r($arr); 是不會看到什麼結果的 因為你似乎並沒用顯示返回數據的代碼,也不知道返回的數據格式是否符合要求(不合要求也可能不顯示) 但你這樣 file_put_contents('test.txt', print_r($_FILES, 1)); file_put_contents('test.txt', print_r($_POST, 1), FILE_APPEND); file_put_contents('test.txt', file_get_contents('php://input'), FILE_APPEND); $arr = $GLOBALS["HTTP_RAW_POST_DATA"]; file_put_contents('test.txt', print_r($arr, 1), FILE_APPEND); 在 test.txt 中是一定有結果的

㈧ php上傳文件(上傳後顯示圖片)

如果你能上傳成功得話下面得

upload.php
<?
include_once ('admin_global.php');
if(isset($_POST['upload'])){
$name=$_FILES["userfile"]["name"];
$updir="../common/images/";
//$uploadfile=$uploaddir.$_FILES['userfile']['name']; //新文件
$type=$_FILES["userfile"]["type"];
$size=$_FILES["userfile"]["size"];
if($name==""){echo"<script>alert('請先選擇要上傳的圖片文件!');window.history.back();</script>";}
$tmp_name=$_FILES["userfile"]["tmp_name"];
if($type!="image peg" && $type!="image/jpeg" && $type!="image/gif"){echo"<script>alert('上傳文件只可以是JPEG或GIF類型的!');window.history.back();</script>";exit;}
if(file_exists($updir.$name)){echo"<script>alert('伺服器上已有同名文件!');window.history.back();</script>";exit;}
if(move_uploaded_file($tmp_name,$updir.$name)){echo"<script>alert('圖片上傳完成!');</script>";}
echo"<script>window.location.href('admin_tu_add.php?n=$name');</script>";
//echo"$name";
}

?>

admin_tu_add.php
<img src="../common/images/<? echo $_GET['n']; ?>">

如果上傳不了得話就是你寫得上傳程序可能有問題

㈨ android上傳圖片到伺服器,為什麼接受到的類型是application/octet-stream

ctet-stream代表的是文件的形式傳輸的,這樣做的好處是可以傳輸多種格式的文件,不管你是jpeg還是png都可以通過這種方式傳送過去
octet-stream代表的是文件的形式傳輸的,這樣做的好處是可以傳輸多種格式的文件,不管你是jpeg還是png都可以通過這種方式傳送過去
你建立HttpUrlConntion的時候沒有指定Content-Type頭域吧

㈩ 求教php如何接收文件流,,ios android上傳的圖片

請問你的客戶端是將數據流編碼了之後傳遞的么?
客戶端可以直接使用流上傳,不需要進行編碼,然後php獲取後直接保存就可以了,如:
$byte = file_get_contents('php://input');
file_put_contents($filename,$byte);

這樣客戶端不用進行處理,直接向伺服器端寫入數據流就可以了。

熱點內容
查看系統信息linux 發布:2024-05-01 12:03:48 瀏覽:718
腳本竊取 發布:2024-05-01 11:55:19 瀏覽:808
天龍八部撿包腳本 發布:2024-05-01 11:55:16 瀏覽:619
mf推薦演算法 發布:2024-05-01 11:39:34 瀏覽:859
破解阿里雲伺服器 發布:2024-05-01 11:11:07 瀏覽:958
伺服器錯誤16999什麼意思 發布:2024-05-01 10:55:38 瀏覽:551
python中count是什麼意思 發布:2024-05-01 10:46:06 瀏覽:906
ssc網站源碼 發布:2024-05-01 10:28:53 瀏覽:636
php的redis手冊 發布:2024-05-01 09:54:26 瀏覽:174
永生之物安卓用什麼模擬器 發布:2024-05-01 09:48:51 瀏覽:621