当前位置:首页 » 安卓系统 » 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);

这样客户端不用进行处理,直接向服务器端写入数据流就可以了。

热点内容
怀旧服玩小号去什么服务器 发布:2024-05-22 04:52:22 浏览:558
网页无法上传照片 发布:2024-05-22 04:34:04 浏览:193
奇特解压图解大全 发布:2024-05-22 04:04:38 浏览:648
学php8 发布:2024-05-22 03:56:56 浏览:739
紫米的充电宝wifi密码是多少 发布:2024-05-22 03:25:12 浏览:71
车内搞笑配置有哪些 发布:2024-05-22 03:16:03 浏览:892
php当前时间时间戳 发布:2024-05-22 02:59:58 浏览:200
dota未加密 发布:2024-05-22 02:55:09 浏览:928
java程序试题 发布:2024-05-22 02:50:09 浏览:57
android查看数据库工具mac 发布:2024-05-22 02:35:43 浏览:376