php图片上传代码
php教程图片上传代码
/*
这款国外的php图片上传代码是一款好的文件与图片上传代码,并且还支持文件在线管理哦,是一款比较的好图片管理系统哦。
*/
define('max', 2);
mysql教程_connect('localhost', 'your mysql username', 'your mysql password');
mysql_select_db('your mysql database');
switch ($_post['action']) {
case 'upload':
$file = $_files['file']['tmp_name'];
$filename = $_files['file']['name'];
if($file) {
$max = max * 1024 * 1024;
$q = mysql_query("select * from `uploads` order by `batch` desc limit 1");
$r = mysql_fetch_assoc($q);
$batch = $r['batch'];
if($filename == 'upload.zip') {
$zip = zip_open($file);if ($zip) {
while ($zip_entry = zip_read($zip)) {
$size = zip_entry_filesize($zip_entry);
$name = zip_entry_name($zip_entry);
$type = substr(strrchr($name, '.'), 1);
if (zip_entry_open($zip, $zip_entry, "r")) {
$content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
if ($size > $max) {
header('location: ./?error=4');
exit;
}
$error = true;
if ($type == 'gif' && $error) {
$error = false;
}
if ($type == 'png' && $error) {
$error = false;
}
if ($type == 'jpg' && $error) {
$error = false;
}
if ($type == 'jpeg' && $error) {
$error = false;
}
if ($error) {
header('location: ./?error=2');
} else {
$id = 1;
$batch2 = $batch + 1;
while (file_exists("uploads/$id/$name")) {
$id++;
}
@mkdir("uploads/$id");
$fp = @fopen("uploads/$id/$name", "w");
if (@fwrite($fp, $content)) {
$q = mysql_query("insert into `uploads` (`file`, `batch`) values ('uploads/$id/$name', '$batch2')");
$id = mysql_insert_id();
} else {
header('location: ./?error=3');
}
fclose($fp);
}
}
header('location: ./?batch=' . $batch2);
zip_close($zip);
}
} else {
if (filesize($file) > $max) {
header('location: ./?error=4');
exit;
}
$error = true;
if (@imagecreatefromjpeg($file) && $error) {
$error = false;
}
if (@imagecreatefromgif($file) && $error) {
$error = false;
}
if (@imagecreatefrompng($file) && $error) {
$error = false;
}
if ($error) {
header('location: ./?error=2');
} else {
$id = 1;
$batch = $batch + 1;
while (file_exists("uploads/$id/$filename")) {
$id++;
}
@mkdir("uploads/$id");
if (@move_uploaded_file($file, "uploads/$id/$filename")) {
$q = mysql_query("insert into `uploads` (`file`, `batch`) values ('uploads/$id/$filename', '$batch')");
$id = mysql_insert_id();
header('location: ./?image=' . $id);
} else {
header('location: ./?error=3');
}
}
}
} else {
header('location: ./?error=1');
}
exit;
break;
}
header('content-type: text/html; charset=iso-8859-1');
ob_start('rewrite');
function rewrite ($buffer) {
$host = $_server['http_host'];
$path = dirname($_server['php_self']);
$absolute = "http://$host$path/";
return preg_replace('#(href|src|action)="/#', "\1="$absolute", $buffer);
}
?>
<!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">
<head>
<title>jpegr - share photos instantly</title>
<meta name="description" content="upload and share photos and images instantly, on jpegr.com" />
<meta name="keywords" content="upload, upload images, share photos, photo sharing, image uploader" />
<link href="/css教程/main.css" rel="stylesheet" />
<script src="/mint/?js" type="text/网页特效"></script>
<script src="/js/nb-object.js" type="text/javascript"></script>
</head>
<body>
<h1><a href="http://jpeg.sn8.us/" title="goto jpegr home"><img src="/img/logo.gif" alt="jpegr" /></a></h1>
<div id="menu">
<form method="post" action="/" enctype="multipart/form-data" class="right">
<input type="hidden" name="action" value="upload" />
<label for="quick">quick upload</label>
<input type="file" name="file" size="10" id="quick" />
<input type="submit" value="upload" class="button" />
</form>
<a href="/">upload</a>
<a href="/help/">help</a>
<a href="/terms-of-service/">terms of service</a>
</div>
<?php// recently uploaded query
//$q = mysql_query('select * from `uploads` order by `id` desc limit 15');$q = mysql_query('select count(`id`) as `count` from `uploads`');
$r = mysql_fetch_assoc($q);
?>
<div id="main">
<h2 class="right"><?php echo number_format($r['count']); ?> <strong>images hosted</strong></h2>
<?phpif ($_get['p'] == 'help') {
?>
<h2>help</h2>
<div>
<ul>
<li>
<strong>how do i upload an image?</strong><br />
just use the quick upload form on the top, or goto the <a href="/">home page</a> to <a href="/">upload an image</a>.
</li>
<li>
<strong>what does "you must select a file to upload!" mean?</strong><br />
this means that you clicked <strong>upload</strong> without selecting an image file.
</li>
<li>
<strong>what does "that is a not a valid jpeg, gif, or png image." mean?</strong><br />
this means that you uploaded a file, but it was not a jpeg, gif, or png image.
</li>
<li>
<strong>what does "there was a problem with the server, and we were unable to upload your image." mean?</strong><br />
this means your file was accepted, but it did not get saved, you will need to <a href="/">try again</a>, or <a href="/">upload another image</a>.
</li>
<li>
<strong>what does "the file you selected was too big, <em><?php echo max; ?>mb</em> is the maximum." mean?</strong><br />
this means that you tried to upload a file that was too big.
</li>
</ul>
</div>
<?php} elseif ($_get['p'] == 'terms-of-service') {
?>
<h2>terms of service</h2>
<div>
<strong>when you upload to jpegr you agree to the following</strong>;
<ul>
<li>you will not use jpegr to upload 易做图ographic content, any violation of this agreement may result in ban, and immediate removal of content.</li>
<li>you will not abuse jpegr's upload form.</li>
<li>any violation may result in permanent ban.</li>
</ul>
</div>
<?php} else {
if (is_numeric($_get['image'])) {
$q = mysql_query("select * from `uploads` where `id` = '$_get[image]'");
$r = mysql_fetch_assoc($q);
$root_ = 'http://' . $_server['http_host'] . dirname($_server['php_self']) . '/';
?>
<h2>here is your image</h2>
<div>
<a href="/<?php echo $r['file']; ?>">click here to view your image</a><br /><br />
<label for="direct">direct link to your image</label><br />
<input type="text" id="direct" value="<?php echo $root_ . $r['file']; ?>" onfocus="this.select();" /><br />
<label for="share" style="font-weight: bold;">share with your friends</label><br />
<input type="text" id="share" value="<?php echo htmlspecialchars("$root_?image=$r[id]"); ?>" onfocus="this.select();" /><br />
<label for="html">post link to myspace or website</label><br />
<input type="text" id="html" value="<?php echo htmlspecialchars("<a href="$root_"><img src="$root_$r[file]" alt="visit jpegr" /></a>"); ?>" onfocus="this.select();" /><br />
<label for="forum">post to a forum</label><br />
<input type="text" id="forum" value="<?php echo htmlspecialchars("[url=$root_][img]$root_$r[file][/img][/url]"); ?>" onfocus="this.select();" /><br />
if you want to <a href="/">upload another image</a>, you can go back or use the form below!<br /><br />
</div>
<?php}
if (is_numeric($_get['batch'])) {
$q = mysql_query("select * from `uploads` where `batch` = '$_get[batch]'");
?>
<h2>viewing batch #<?php echo ($_get['batch']) ? $_get['batch'] : 0; ?></h2>
<div>
to view an image in full size, just click it.<br /><br />
<?phpwhile($r = mysql_fetch_assoc($q)) {
?>
<a href="<?php echo $r['file']; ?>"><img src="<?php echo $r['file']; ?>" alt="image #<?php echo $r['id']; ?>" border="0" style="max-width: 75px;" /></a>
<?php}
?>
<br /><br />
<div id="slider">
<h3>beta image slideshow</h3>
<noscript>please turn on javascript to view our slideshows.</noscript>
<div id="slide"></div>
</div>
<script type="text/javascript">
<?php
$qq = mysql_query("select * from `uploads` where `batch` = '$_get[batch]'");
while($rr = mysql_fetch_assoc($qq)) {
?>
nb.slideshow.addimage('<?php echo $rr['file']; ?>');
<?php
}
?>
nb.slideshow.start();
</script>
</div>
<h2>share this batch with your friends</h2>
<div>
<label for="share" style="font-weight: bold;">batch viewer</label><br />
<input type="text" id="share" value="http://jpeg.sn8.us/?batch=<?php echo ($_get['batch']) ? $_get['batch'] : 0; ?>" onfocus="this.select();" /><br />
if you want to <a href="/">upload another batch</a>, you can go back or use the form below!<br /><br />
</div>
<?php}
?>
<h2>upload an image or photo</h2>
<form method="post" action="/" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload" />
<?phpswitch ($_get['error']) {
case 1:
$error = 'you must select a file to upload!<br />';
break;
case 2:
$error = 'that is a not a valid jpeg, gif, or png image.<br />';
break;
case 3:
$error = 'there was a problem with the server, and we were unable to upload your image.<br />';
break;
case 4:
$error = 'the file you selected was too big, <strong>' . max . 'mb</strong> is the maximum.<br />';
break;
}
?>
<font color="#ff0004"><?php echo $error; ?></font>
you can upload a <strong>jpeg</strong>, <strong>gif</strong>, or <strong>png</strong> image. (max <strong><?php echo max; ?>mb</strong>)<br /><br />
you can also upload a <strong>zip</strong> named <strong>upload.zip</strong>, containing multiple images.<br /><br />
<label for="file">choose your file</label><br />
<input type="file" name="file" size="40" id="file" /><br />
<input type="submit" value="upload" class="button" />
</form>
<?php}
?>
</div>
</div>
</body>
</html>??????
<?php
mysql_connect('localhost', 'your mysql username', 'your mysql password');
mysql_select_db('your mysql database');
if ($_get['delete']) {
$sql = "select * from `uploads` where `id` = '$_get[delete]'";
$q = mysql_query($sql);
$r = mysql_fetch_assoc($q);
unlink($_server['document_root'] . '/' . $r['file']);
$sql = "delete from `uploads` where `id` = '$_get[delete]'";
$q = mysql_query($sql);
header('location: ' . $_server['http_referer']);
exit;
}
?>
<!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">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>jpegr administration</title>
<style type="text/css">
body {
font-family: sans-serif;
font-size: 12px;
width: 800px;
margin: 40px auto;
}
a {
color: #105cb6;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
td {
padding: 4px;
}
.head {
font-weight: bold;
color: #ffffff;
background-color: #222222;
}
.item {
background-color: #f2f2f2;
}
.one {
width: 60px;
text-align: center;
}
.three {
width: 80px;
text-align: center;
}
.page, .current {
display: block;
float: left;
padding: 2px 4px;
margin: 0px 4px 8px 0px;
}
.page {
color: #000000;
background-color: #eeeeee;
}
.current {
font-weight: bold;
color: #ffffff;
background-color: #222222;
}
</style>
</head>
<body>
<?php
$sql = 'select ceil(count(`id`) / 20) as `count` from `uploads`';
$q = mysql_query($sql);
$r = mysql_fetch_assoc($q);
$pages = $r['count'];
$offset = ($_get['page'] > 0 && $_get['page'] <= $pages) ? ($_get['page'] - 1) * 20 : 0;
for ($i = 1; $i <= $pages; $i++) {
$class = ($_get['page'] == $i || $i == 1 && !$_get['page']) ? ' class="current"' : ' class="page"';
?>
<a href="?page=<?php echo $i; ?>"<?php echo $class; ?>><?php echo $i; ?></a>
<?php
}
?>
<br clear="all" />
<table width="100%">
<tr class="head">
<td class="one">id</td>
<td class="two">filename</td>
<td class="three">action</td>
</tr>
<?php
$sql = "select * from `uploads` order by `id` desc limit $offset,20";
$q = mysql_query($sql);
while ($r = mysql_fetch_assoc($q)) {
?>
<tr class="item">
<td class="one"><?php echo number_format($r['id']); ?></td>
<td class="two"><?php echo $r['file']; ?></td>
<td class="three"><a href="/<?php echo $r['file']; ?>" target="_blank">view</a>, <a href="?delete=<?php echo $r['id']; ?>" onclick="return confirm('are you sure you want to delete "<?php echo $r['file']; ?>"?');">delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>css??
html, body {
font-family: sans-serif;
font-size: 12px;
width: 800px;
margin: 40px auto;
}
a {
color: #105cb6;
text-decoration: none;
}
h1 {
margin: 0px 0px 10px 0px;
}
h1 a {
-moz-outline-width: 0px;
}
h1 a img {
border: 0px;
}
h3 {
margin: 4px;
}
a:hover {
text-decoration: underline;
}
#menu {
background-color: #e5f5ff;
padding: 8px;
border: 1px solid #0099ff;
position: relative;
}
#menu a {
font-weight: bold;
margin: 0px 8px 0px 0px;
}
#menu a:hover {
text-decoration: underline;
}
#menu .right {
margin: 0px;
padding: 0px;
position: absolute;
top: 4px;
right: 4px;
}
#menu .input {
background-color: #ffffff;
padding: 2px;
border: 1px solid #0066ff;
}
#menu .button {
font-family: sans-serif;
padding: 2px;
cursor: pointer;
}
#menu label {
cursor: pointer;
}
#ads {
background-color: #fde5f3;
margin: 8px 0px;
border: 1px solid #ec008c;
}
#recent {
background-color: #e6fec9;
margin: 8px 0px;
padding: 2px;
border: 1px solid #9dca68;
}
#main {
background-color: #fffee5;
padding: 8px;
border: 1px solid #fff200;
}
#main h2 {
font-size: 16px;
color: #222222;
margin: 0px;
}
#main form {
margin: 4px 8px;
}
#main label {
cursor: pointer;
}
#main .button {
font-family: sans-serif;
margin: 2px 0px 0px 0px;
padding: 2px;
cursor: pointer;
}
#main .right {
font-size: 14px;
float: right;
padding: 0px 0px 2px 0px;
border-bottom: 1px solid #444444;
}
#main div {
margin: 4px 8px;
}
#main div label {
font-weight: bold;
}
#main div input {
font-family: sans-serif;
font-size: 12px;
width: 680px;
padding: 2px;
margin: 2px 0px 4px 4px;
}
#main div li {
margin-bottom: 8px;
}
#links {
background-color: #e9e8e8;
margin: 8px 0px 0px 0px;
padding: 4px 0px;
border: 1px solid #231f20;
}
.spacer {
height: 4px;
overflow: hidden;
}#slider {
color: #ffffff;
background-color: #232323;
height: 400px;
margin: 10px;
padding: 5px;
border: 1px solid #121212;
}#slider h3 {
color: #ffffff;
font-size: 14px;
line-height: 20px;
background-color: #343434;
height: 20px;
margin: -5px -5px 15px -5px;
padding: 5px;
}#slider #slide #image {
max-height: 350px;
}
源码下载地址
http://down.zzzyk.com/php/2010/0927/20956.html
补充:Php教程,Php高级应用