<?
/*
hi i having a short source code solution for images uploading that i’m going to used im my site that required php, mysql .. the process are input forms for entering file images located on your pc then upload to server the function used here will send all information you enter to another function that will insert data to mysql.. ( notices i have cuted the code and data shortess as i can to easy view and people can help me here)
fistly this is structures of table: stories with many fields but i just show here 4 for easy viewing..?
[ sid | hometext | images | imgtext ]
[B]CREATE TABLE `stories
`sid
`hometext
ALTER TABLE stories ADD COLUMN images varchar(100) NOT NULL default ”;
ALTER TABLE stories ADD COLUMN imgtext varchar(150) default NULL;
How them work!!! the function adminStory() have [form] for submit informaiton; then funtion uploadimg() to be used for testing images upload detail then return $images to the funtion postadminStory() which send information to data sql.
( notices i have test the site with this code and it run good. all other field insert on form is imported to data but images only. ? so $images not return anything; the $imgtext is depent on $images it can return NULL if the $imgtext is NULL too. but we can test work or not it by enable in code -> look at my code;
[code=php] */
mysql_connect(“localhost”, “root”, “pass”) ;
mysql_select_db(“mydata”) ;
$sizecatnewshomeimg = 120;
$sizeimgskqa = 200;
$newsarticleimg = “left”;
$sizenewsarticleimg = 150;
$temp_path = “images/News/temp_pic”;
$path = “images/News/Image”;
$width = 600;
$max_size = 2097152;
// the .INCLUDE_PATH. is setup in other file but it can be root of this file = “./” not important
switch($op) {
case “adminStory”:
adminStory($sid);
break;
case “PostAdminStory”:
postAdminStory($hometext, $images, $imgtext, $delpic);
break;
}
function adminStory() {
// have many code here but i make it now simple…
echo “<center><font class=”option”><b>”._ADDARTICLE.”</b></font></center><br>”
.”<form action=”index.php” method=”post”>”;
echo “<br><br><b>”._STORYTEXT.”</b><br>”
.”<textarea wrap=”virtual” cols=”100″ rows=”15″ name=”hometext”>$hometext</textarea><br><br>”
.”<b>”._EXTENDEDTEXT.”</b><br>”;
////////images for stories
echo “<br><br><b>”._STPIC.”:</b> ”
.”<input name=”userfile” type=”file”><br><br>”
.”<b>”._IMGTEXT.”</b>: <input type=”text” name=”imgtext” value=”” size=”44″><br>”;
echo “</form>”;
}
function postAdminStory($hometext, $images, $imgtext, $delpic) {
global $max_size, $width, $height, $path, $sizenewsarticleimg;
/// call the funtion to process the imagages before insert data..
$images = @uploadimg($images, $delpic, 1, $sizenewsarticleimg, $path);
if(!file_exists(“”.INCLUDE_PATH.””.$path.”/”.$images.””)) { $images=””; }
if($images == “”) $imgtext = “”;
// $imgtext = “aabbcc”; //enable this to test the data is accesable to send to data
// some other funtion for process text and other fields here ////
/// the following is query insert data to stories table //
mysql_query(“insert into stories values (NULL, ‘$hometext’, ‘$images’, ‘$imgtext’)”);
//end query
}
/// the $delpic using to check box incase want to delete the exiting images on data and change it to ohter images upload file( yes/no)
function uploadimg($images, $delpic, $thumb, $thumb_width, $upath) {
global $max_size, $width, $sizeimgskqa;
if ($delpic == “yes”) {
@unlink(“”.INCLUDE_PATH.””.$upath.”/”.$images.””);
@unlink(“”.INCLUDE_PATH.””.$upath.”/small_”.$images.””);
$images = “”;
}
if (is_uploaded_file($_FILES[‘userfile’][‘tmp_name’])) {
@unlink(“”.INCLUDE_PATH.””.$upath.”/”.$images.””);
@unlink(“”.INCLUDE_PATH.””.$upath.”/small_”.$images.””);
$images = “”;
$realname = $_FILES[‘userfile’][‘name’];
$file_size = $_FILES[‘userfile’][‘size’];
$file_type = $_FILES[‘userfile’][‘type’];
$f_name = end(explode(“.”, $realname));
$f_extension = strtolower($f_name);
$loaiimg_ext = array(“gif”,”jpg”,”jpeg”,”pjpeg”,”png”,”bmp”);
$loaiimg_mime = array(“image/gif”, “image/pjpeg”, “image/jpeg”, “image/png”, “image/bmp”);
if ($file_size > $max_size) {
info_exit(“<br><br><center>”._EROR1.” “.$file_size.” “._EROR2.” $max_size byte.<br><br>”._GOBACK.”</center><br><br>”);
}
if(!in_array($file_type,$loaiimg_mime) || !in_array($f_extension,$loaiimg_ext)) {
info_exit(“<br><br><center>”._EROR6.”<br><br>”._GOBACK.”</center><br><br>”);
}
$datakod = date(U);
$picname = “”.$datakod.”.nv.”.$f_extension.””;
if(! @copy($_FILES[‘userfile’][‘tmp_name’], “”.INCLUDE_PATH.””.$upath.”/”.$picname.””) ) {
if (! move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], “”.INCLUDE_PATH.””.$upath.”/”.$picname.””)) {
info_exit(“<br><br>”._UPLOADFAILED.”<br>”);
}
}
if (file_exists(“”.INCLUDE_PATH.””.$upath.”/”.$picname.””)) {
$images = $picname;
if ($f_extension == “jpg” AND extension_loaded(“gd”)) {
$size = @getimagesize(“”.INCLUDE_PATH.””.$upath.”/”.$images.””);
$thc = 0;
if ($size[0] > $width) {
$thc = 1; $sizemoi = $width;
} elseif($size[0] > $thumb_width AND $size[0] < ($thumb_width+20)) {
$thc = 1; $sizemoi = $thumb_width;
}
if ($thc == 1) {
$src_img= ImageCreateFromJpeg(“”.INCLUDE_PATH.””.$upath.”/”.$images.””);
$src_width= ImagesX($src_img);
$src_height= ImagesY($src_img);
$dest_width = $sizemoi;
$dest_height = $src_height/($src_width/$dest_width);
$dest_img=ImageCreateTrueColor($dest_width, $dest_height);
ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
ImageJpeg($dest_img, “”.INCLUDE_PATH.””.$upath.”/”.$images.””, 90);
ImageDestroy($dest_img);
}
$size = @getimagesize(“”.INCLUDE_PATH.””.$upath.”/”.$images.””);
if($thumb==1 AND $size[0] > $thumb_width) {
$picname_thumb = “small_”.$picname.””;
if(! @copy($_FILES[‘userfile’][‘tmp_name’], “”.INCLUDE_PATH.””.$upath.”/”.$picname_thumb.””) ) {
@move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], “”.INCLUDE_PATH.””.$upath.”/”.$picname_thumb.””);
}
if (file_exists(“”.INCLUDE_PATH.””.$upath.”/”.$picname_thumb.””)) {
$src_img= ImageCreateFromJpeg(“”.INCLUDE_PATH.””.$upath.”/”.$picname_thumb.””);
$src_width= ImagesX($src_img);
$src_height= ImagesY($src_img);
$dest_width = $thumb_width;
$dest_height = $src_height/($src_width/$dest_width);
$dest_img=ImageCreateTrueColor($dest_width, $dest_height);
ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
ImageJpeg($dest_img, “”.INCLUDE_PATH.””.$upath.”/”.$picname_thumb.””, 90);
ImageDestroy($dest_img);
}
}
}
}
}
// $images = “testreturn.gif”; this test return work
return($images);
}
function info_exit($info) {
include(“header.php”);
echo “<br><center><b>”.$info.”</b></center><br>”;
include(“footer.php”);
exit();
}
// this folowing funtion for viewing images on thumbles but i have not tested yet!! by upload funtion not solved yet!!
function thumbs($outpath,$inpath,$outimg,$inimg,$insize) {
if(@copy(“”.$outpath.”/”.$outimg.””, “”.$inpath.”/”.$inimg.””)) {
$f_name = end(explode(“.”, $inimg));
$f_extension = strtolower($f_name);
if ($f_extension == “jpg” AND extension_loaded(“gd”)) {
$src_img= ImageCreateFromJpeg(“”.$inpath.”/”.$inimg.””);
$src_width= ImagesX($src_img);
$src_height= ImagesY($src_img);
$dest_width = $insize;
$dest_height = $src_height/($src_width/$dest_width);
$dest_img=ImageCreateTrueColor($dest_width, $dest_height);
ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
ImageJpeg($dest_img, “”.$inpath.”/”.$inimg.””, 90);
ImageDestroy($dest_img);
}
}
}//////////////
//
?>