當前位置:首頁 » 文件管理 » php上傳圖片實例

php上傳圖片實例

發布時間: 2022-09-01 08:03:10

php如何上傳圖片到資料庫

  1. 把圖片保存到伺服器,拼接圖片地址

  2. 保存圖片地址到資料庫

  3. 讀取圖片地址就能訪問到圖片了。

Ⅱ 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上傳圖片客戶端和伺服器端實現方法分享給大家供大家參考。具體如下:

前台表單代碼
<form name="form1" method="post" action="admin_upfile.php" enctype="multipart/form-data">
<input type="file" name="pic">
<input type="submit" name="Submit" value="開始上傳" class="button">
</form>

後端php代碼
<?php
//這里上傳 $upsize判斷上傳文件的大小
$uppath = "/attached/"; //文件上傳路徑
//轉換根目錄的路徑
if (strpos($uppath, "/") == 0)
{
$i = 0;
$thpath = $_SERVER["SCRIPT_NAME"];
$thpath = substr($thpath, 1, strlen($thpath));
while (strripos($thpath, "/") !== false)
{
$thpath = substr($thpath, strpos($thpath, "/") + 1, strlen($thpath));
$i = ++$i;
}
$pp = "";
for ($j = 0; $j < $i; ++$j)
{
$pp .= "../";
}
$uppaths = $pp . substr($uppath, 1, strlen($thpath));
}
$filename = date("y-m-d");
if (is_dir($uppaths . $filename) != TRUE)
mkdir($uppaths . $filename, 0777);
$f = $_FILES['pic'];
if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png")
{
echo "<script>alert('只能上傳圖片格式的文件');window.close()</script>";
return false;
}
//獲得文件擴展名
$temp_arr = explode(".", $f["name"]);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//新文件名
$new_file_name = md5(date("YmdHis")) . '.' . $file_ext;
echo $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" .
$new_file_name; //設置文件名為日期加上文件名避免重復 上傳目錄
echo $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" .
$new_file_name; //設置文件名為日期加上文件名避免重復
$r = move_uploaded_file($f['tmp_name'], $dest);
?>

Ⅳ 用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上傳圖片怎麼做

上傳類,保存文件名稱為 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如何把一些文件和圖片上傳到另一指定的伺服器

一個實例:

首先,在自己台式機和筆記本上都開通了ftp,這個不會的同學可以網上查serv-u,相關教程肯定不少的。

然後在台式機本地做了個測試:

$ftp_server = "192.168.1.100";
$ftp_user_name = "laohu";
$ftp_user_pass = "123456";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$file = 'test.txt';
$remote_file = '/test/a.txt';
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "文件移動成功\n";
} else {
echo "移動失敗\n";
}
ftp_close($conn_id);

運行後:文件移動成功。

要的就是這個效果了,之後用台式機做程序伺服器,上傳附件時全用ftp方法上傳至筆記本上,筆記本ip是105,相應代碼如下:

if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
$ftp_server = "192.168.1.105";
$ftp_user_name = "lesley";
$ftp_user_pass = "123456";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$file = $_FILES['uploadfile']['tmp_name'];
$remote_file = '/test/'.$_FILES['uploadfile']['name'];
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "文件:".$_FILES['uploadfile']['name']."上傳成功\n";
} else {
echo "上傳失敗\n";
}
ftp_close($conn_id);
}

對應的前台頁面代碼:

<form action="uploadfile.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile" id="uploadfile" />
<input type="submit" name="submit" value="submit" />
</form>

運行後確實成功。

需要注意:
在用ftp_put方法時,第四個參數傳送模式,需要用FTP_BINARY(二進制模式),用FTP_ASCII(文本模式)時,圖片能上傳但無法顯示,其他文件重命名、中文亂碼解決、上傳許可權控制等,就不在此提及了。

Ⅶ 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實現上傳圖片到資料庫並顯示輸出的方法

本文實例講述了PHP實現上傳圖片到資料庫並顯示輸出的方法。分享給大家供大家參考,具體如下:
1.
創建數據表
CREATE
TABLE
ccs_image
(
id
int(4)
unsigned
NOT
NULL
auto_increment,
description
varchar(250)
default
NULL,
bin_data
longblob,
filename
varchar(50)
default
NULL,
filesize
varchar(50)
default
NULL,
filetype
varchar(50)
default
NULL,
PRIMARY
KEY
(id)
)engine=myisam
DEFAULT
charset=utf8
2.
用於上傳圖片到伺服器的頁面
upimage.html
<!doctype
html>
<html
lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport"
content="width=device-width,
user-scalable=no,
initial-scale=1.0,
maximum-scale=1.0,
minimum-scale=1.0">
<meta
http-equiv="X-UA-Compatible"
content="ie=edge">
<style
type="text/css">
*{margin:
1%}
</style>
<title>Document</title>
</head>
<body>
<form
method="post"
action="upimage.php"
enctype="multipart/form-data">
描述:
<input
type="text"
name="form_description"
size="40">
<input
type="hidden"
name="MAX_FILE_SIZE"
value="1000000">
<br>
上傳文件到資料庫:
<input
type="file"
name="form_data"
size="40"><br>
<input
type="submit"
name="submit"
value="submit">
</form>
</body>
</html>
3.
處理圖片上傳的php
upimage.php
<?php
if
(isset($_POST['submit']))
{
$form_description
=
$_POST['form_description'];
$form_data_name
=
$_FILES['form_data']['name'];
$form_data_size
=
$_FILES['form_data']['size'];
$form_data_type
=
$_FILES['form_data']['type'];
$form_data
=
$_FILES['form_data']['tmp_name'];
$dsn
=
'mysql:dbname=test;host=localhost';
$pdo
=
new
PDO($dsn,
'root',
'root');
$data
=
addslashes(fread(fopen($form_data,
"r"),
filesize($form_data)));
//echo
"mysqlPicture=".$data;
$result
=
$pdo->query("INSERT
INTO
ccs_image
(description,bin_data,filename,filesize,filetype)
VALUES
('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
if
($result)
{
echo
"圖片已存儲到資料庫";
}
else
{
echo
"請求失敗,請重試";
注:圖片是以二進制blob形式存進資料庫的,像這樣
4.
顯示圖片的php
getimage.php
<?php
$id
=2;//
$_GET['id'];
為簡潔,直接將id寫上了,正常應該是通過用戶填入的id獲取的
$dsn='mysql:dbname=test;host=localhost';
$pdo=new
PDO($dsn,'root','root');
$query
=
"select
bin_data,filetype
from
ccs_image
where
id=2";
$result
=
$pdo->query($query);
$result=$result->fetchAll(2);
//
var_mp($result);
$data
=
$result[0]['bin_data'];
$type
=
$result[0]['filetype'];
Header(
"Content-type:
$type");
echo
$data;
到瀏覽器查看已經上傳的圖片,看是否可以顯示
是沒有問題的,證明圖片已經以二進制的形式存儲到資料庫了
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php+mysql資料庫操作入門教程》、《php+mysqli資料庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《php字元串(string)用法總結》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:php實現上傳圖片保存到資料庫的方法php上傳圖片存入資料庫示例分享php上傳圖片到指定位置路徑保存到資料庫的具體實現php中如何將圖片儲存在資料庫里php下將圖片以二進制存入mysql資料庫中並顯示的實現代碼php
從資料庫提取二進制圖片的處理代碼php將圖片保存入mysql資料庫失敗的解決方法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寫一個上傳圖片的程序 謝謝

<?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>

熱點內容
二級java培訓 發布:2024-10-12 20:22:17 瀏覽:430
安卓的重子長什麼樣 發布:2024-10-12 20:22:10 瀏覽:644
java訪問資料庫的步驟 發布:2024-10-12 20:21:29 瀏覽:379
加密話語 發布:2024-10-12 19:53:36 瀏覽:479
找文案腳本工具 發布:2024-10-12 19:51:50 瀏覽:17
v編程語言 發布:2024-10-12 19:22:48 瀏覽:655
sqlserver2008還原 發布:2024-10-12 19:20:59 瀏覽:31
保險怎麼配置合適 發布:2024-10-12 19:17:40 瀏覽:990
o2o生活源碼 發布:2024-10-12 19:14:40 瀏覽:785
電腦硬體配置是什麼 發布:2024-10-12 18:33:58 瀏覽:259