PHP Picture Upload Code

Source: Internet
Author: User
  code is as follows copy code

Define (' Max ', 2);

MySQL tutorial _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;


$batch 2 = $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 ', ' $batch 2 ')");


$id = mysql_insert_id ();





} else {





Header (' Location:./?error=3 ');





}





Fclose ($FP);





}





}





Header (' Location:./?batch= '. $batch 2);





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 tutorial/main.css" rel= "stylesheet"/>


<script src= "/mint/?js" type= "text/Web Effects" ></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= "ten" 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 ' 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
<?php

if ($_get[' p '] = = ' help ') {





?&gt;


&lt;h2&gt;help&lt;/h2&gt;


&lt;div&gt;


&lt;ul&gt;


&lt;li&gt;


&lt;strong&gt;how do I upload a image?&lt;/strong&gt;&lt;br/&gt;


Just use the Quick Upload form on the top, or goto the &lt;a href= "/" &gt;home page&lt;/a&gt; to &lt;a href= "/" &gt;upload A n Image&lt;/a&gt;


&lt;/li&gt;


&lt;li&gt;


&lt;strong&gt;what does "You must select a file to upload!" mean?&lt;/strong&gt;&lt;br/&gt;


This means is clicked &lt;strong&gt;upload&lt;/strong&gt; without selecting an image file.


&lt;/li&gt;


&lt;li&gt;


&lt;strong&gt;what does "that are a not a valid JPEG, GIF, or PNG image." Mean?&lt;/strong&gt;&lt;br/&gt;


This means so you uploaded a file, but it is not a JPEG, GIF, or PNG image.


&lt;/li&gt;


&lt;li&gt;


&lt;strong&gt;what does "there is a problem with the server, and we were unable to upload image." Your T;&lt;br/&gt;


This means your file is accepted, but it did not get saved, your would need to &lt;a href= "/" &gt;try again&lt;/a&gt;, or &L T;a href= "/" &gt;upload another image&lt;/a&gt;.


&lt;/li&gt;


&lt;li&gt;


&lt;strong&gt;what does "The file you selected were too big, &lt;em&gt;&lt;?php echo Max;?&gt;mb&lt;/em&gt; is the maximum . "Mean?&lt;/strong&gt;&lt;br/&gt;


This means is tried to upload a file of that is too big.


&lt;/li&gt;


&lt;/ul&gt;


&lt;/div&gt;


&lt;?php

} elseif ($_get[' p '] = = ' Terms-of-service ') {

?>
<div>
<strong>when you upload to Jpegr your agree to the following</strong>;
<ul>
<li>you won't use Jpegr to upload pornographic the content, any violation the this agreement may result in ban, and IM Mediate removal of content.</li>
<li>you won't abuse Jpegr ' s upload form.</li>
<li>any violation may, 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 ')). '/';





?&gt;


&lt;h2&gt;here is your image&lt;/h2&gt;


&lt;div&gt;


&lt;a href= "/&lt;?php echo $r [' file '];?&gt;" &gt;click here to view your image&lt;/a&gt;&lt;br/&gt;&lt;br


&lt;label for= "Direct" &gt;direct link to your image&lt;/label&gt;&lt;br/&gt;


&lt;input type= "text" id= "direct" value= "&lt;?php echo $root _. $r [' file '];?&gt; "onfocus=" This.select (); "/&gt;&lt;br/&gt;


&lt;label for= "Share" style= "font-weight:bold;" &gt;share with your friends&lt;/label&gt;&lt;br/&gt;


&lt;input type= "text" id= "Share" value= "&lt;?php Echo htmlspecialchars (" $root _?image= $r [id] ");?&gt;" Onfocus= " This.select (); "/&gt;&lt;br/&gt;


&lt;label for= "HTML" &gt;post link to MySpace or website&lt;/label&gt;&lt;br/&gt;


&lt;input type= "text" id= "html" value= "&lt;?php Echo htmlspecialchars (" &lt;a href= "$root _" &gt;&lt;img src= "$root _$r[ File] "alt=" visit Jpegr "/&gt;&lt;/a&gt;")?&gt; "onfocus=" This.select (); "/&gt;&lt;br/&gt;


&lt;label for= "Forum" &gt;post to a forum&lt;/label&gt;&lt;br/&gt;


&lt;input type= "text" id= "forum" value= "&lt;?php Echo htmlspecialchars (" [Url= $root _][img] $root _$r[file][/img][/url] ");?&gt;" onfocus= "This.select ();"/&gt;&lt;br/&gt;


If you are want to &lt;a href= '/' &gt;upload another image&lt;/a&gt;, can go back or use the form below!&lt;br/&gt;&lt;br /&gt;


&lt;/div&gt;


&lt;?php

}

if (Is_numeric ($_get[' batch ')) {

$q = mysql_query ("select * from ' uploads ' where ' batch ' = ' $_get[batch]");

?>
<div>
To view a image in full size, just click it.<br/><br/>

<?php

while ($r = Mysql_fetch_assoc ($q)) {

?>
<a href= "<?php echo $r [' file '];?>" > "border=" 0 "style=" max-width:75px; "/></a>
<?php

}





?&gt;


&lt;br/&gt;&lt;br/&gt;





&lt;div id= "Slider" &gt;


&lt;h3&gt;beta Image slideshow&lt;/h3&gt;


&lt;noscript&gt;please turn on JavaScript to view our slideshows.&lt;/noscript&gt;


&lt;div id= "Slide" &gt;&lt;/div&gt;


&lt;/div&gt;





&lt;script type= "Text/javascript" &gt;





&lt;?php





$QQ = mysql_query ("select * from ' uploads ' where ' batch ' = ' $_get[batch]");





while ($RR = Mysql_fetch_assoc ($qq)) {





?&gt;


Nb.slideshow.addimage (' &lt;?php echo $rr [' file '];?&gt; ');


&lt;?php





}





?&gt;


Nb.slideshow.start ();





&lt;/script&gt;





&lt;/div&gt;





&lt;h2&gt;share This batch with your friends&lt;/h2&gt;


&lt;div&gt;





&lt;label for= "Share" style= "font-weight:bold;" &gt;batch viewer&lt;/label&gt;&lt;br/&gt;


&lt;input type= "text" id= "Share" value= "http://jpeg.sn8.us/?batch=&lt;?php Echo ($_get[' batch ')"? $_get[' Batch ']: 0;?&gt; "onfocus=" This.select (); "/&gt;&lt;br/&gt;


If you are want to &lt;a href= '/' &gt;upload another batch&lt;/a&gt;, can go back or use the form below!&lt;br/&gt;&lt;br /&gt;


&lt;/div&gt;


&lt;?php

}

?>
<form method= "POST" action= "/" enctype= "Multipart/form-data" >
<input type= "hidden" name= "action" value= "upload"/>
<?php

Switch ($_get[' ERROR ']) {


Case 1:


$error = ' must select a file to Upload!&lt;br/&gt; ';


Break


Case 2:


$error = ' is a not a valid JPEG, GIF, or PNG image.&lt;br/&gt; ';


Break


Case 3:


$error = ' There is a problem with the server, and we were unable to upload your ' image.&lt;br ';


Break


Case 4:


$error = ' The file you selected is too big, &lt;strong&gt; '. Max. ' Mb&lt;/strong&gt; is the maximum.&lt;br/&gt; ';


Break


}





?&gt;


&lt;font color= "#ff0004" &gt;&lt;?php echo $error;?&gt;&lt;/font&gt;


You can upload a &lt;strong&gt;jpeg&lt;/strong&gt;, &lt;strong&gt;gif&lt;/strong&gt;, or &lt;strong&gt;png&lt;/strong &gt; Image. (Max &lt;strong&gt;&lt;?php echo max;?&gt;mb&lt;/strong&gt;) &lt;br/&gt;&lt;br/&gt;





You can also upload a &lt;strong&gt;zip&lt;/strong&gt; named &lt;strong&gt;upload.zip&lt;/strong&gt; containing Multiple images.&lt;br/&gt;&lt;br/&gt;





&lt;label for= "File" &gt;choose your file&lt;/label&gt;&lt;br/&gt;


&lt;input type= "File" name= "file" size= "id=" "File"/&gt;&lt;br/&gt;


&lt;input type= "Submit" value= "Upload" class= "button"/&gt;


&lt;/form&gt;


&lt;?php

}

?>
</div>

</div>
</body>

??????


&lt;?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


}


?&gt;


&lt;!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd "&gt;


&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/&gt;


&lt;title&gt;jpegr administration&lt;/title&gt;


&lt;style type= "Text/css" &gt;


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;


}


&lt;/style&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;?php


$sql = ' Select Ceil (count (' id ')/' as ' count ' from ' uploads ';


$q = mysql_query ($sql);


$r = Mysql_fetch_assoc ($q);


$pages = $r [' count '];


$offset = ($_get[' page '] &gt; 0 &amp;&amp; $_get[' page '] &lt;= $pages)? ($_get[' page ']-1) * 20:0;


for ($i = 1; $i &lt;= $pages; $i + +) {


$class = ($_get[' page '] = = $i | | $i = = 1 &amp;&amp;!$_get[' page '])? ' class= ': ' class= ' "page";


?&gt;


&lt;a href= "page=&lt;?php echo $i;?&gt;" &lt;?php echo $class; &gt;&gt;&lt;?php echo $i;?&gt;&lt;/a&gt;


&lt;?php


}


?&gt;


&LT;BR clear= "All"/&gt;


&lt;table width= "100%" &gt;


&lt;tr class= "Head" &gt;


&LT;TD class= "One" &gt;id&lt;/td&gt;


&LT;TD class= "Two" &gt;filename&lt;/td&gt;


&LT;TD class= "three" &gt;action&lt;/td&gt;


&lt;/tr&gt;


&lt;?php


$sql = "Select * from ' uploads ' ORDER by ' ID ' desc limit $offset, 20";


$q = mysql_query ($sql);


while ($r = Mysql_fetch_assoc ($q)) {


?&gt;


&LT;TR class= "Item" &gt;


&LT;TD class= "One" &gt;&lt;?php Echo number_format ($r [' id ']);?&gt;&lt;/td&gt;


&LT;TD class= "Two" &gt;&lt;?php echo $r [' file '];?&gt;&lt;/td&gt;


&LT;TD class= "three" &gt;&lt;a href= "/&lt;?php echo $r [' file '];?&gt;" target= "_blank" "&gt;view&lt;/a&gt, &lt;a href="? delete=&lt;?php echo $r [' id '];?&gt; "onclick=" return confirm (' Are your sure you want to delete &amp;quot;&lt;?php echo $r [ ' file ']; &gt;&amp;quot;? '); &gt;delete&lt;/a&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;?php


}


?&gt;


&lt;/table&gt;


&lt;/body&gt;


&lt;/html&gt;

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;
}

SOURCE Download Address

Http://down.111cn.net/php/2010/0927/20956.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.