自已往你的程序中套吧!
/**************************************************************
图片上传,数据添加功能
upload_url---->存放上传文件的网络地址
upload_dir---->存放上传文件目录
**************************************************************/
$MAX_SIZE="500000";
$FILE_MIMES=array('image/jpeg','image/jpg','image/gif','image/png','image/pjpeg','application/msword');
$FILE_POSTFIX=array('.zip','.jpg','.gif','bmp');
$DELETE_ENABLE='no';
$site_name=$_SERVER['HTTP_HOST'];
$site_url="http://".$site_name.dirname($_SERVER['PHP_SELF']);
$site_this="http://".$site_name.$_SERVER['PHP_SELF'];
$visibility=true;
if ($visibility)
{
$upload_dir = "images/upload/public/";
}
else
{
$upload_dir = "images/upload/private/";
}
$upload_url = $site_url."/".$upload_dir;
if (!is_dir($upload_dir))
{
if (!mkdir($upload_dir))
die('文件没有创建成功!');
}
if ($_FILES['GoodsPhoto'])
{
if ($_FILES['GoodsPhoto']['size']==0)
{
echo "<script language='JavaScript'>location.href='shop.php';alert('必须输入项不能为空!');</script>";
}
$file_name=$_FILES['GoodsPhoto']['name'];
$file_type=$_FILES['GoodsPhoto']['type'];
$file_postfix=substr($file_name,strrpos($file_name,"."));
if ($_FILES['GoodsPhoto']['size'] >=$MAX_SIZE)
{
echo "<script language='JavaScript'>location.href='shop.php';alert('图片太大了,换个小点的吧!');</script>";
}
elseif (!in_array($file_type,$FILE_MIMES) && ! in_array($file_postfix,$FILE_POSTFIX))
{
echo "<script language='JavaScript'>location.href='shop.php';alert('上传文件不符合规定,请重新选择上传文件!');</script>";
}
else
{
if (empty($GoodsName) || empty($SalePrice) || empty($EMS) || empty($Delivery))
{
echo "<script language='JavaScript'>location.href='shop.php';alert('必须输入项不能为空!');</script>";
}
else
{
do_upload($upload_dir,$upload_url);
}
}
}
function do_upload($upload_dir,$upload_url)
{
$temp_name=trim($_FILES['GoodsPhoto']['tmp_name']);
$file_name=$_FILES['GoodsPhoto']['name'];
$file_name=str_replace("\\","",$file_name);
$file_name=str_replace("","",$file_name);
$file_path=$upload_dir.$file_name;
if ($file_name='')
{
echo "文件名无效!";
exit;
}
if ($result=move_uploaded_file($temp_name,$file_path))
{
echo "<script type='text/JavaScript' language='JavaScript'> location.href='shop.php'; alert('您成功的添加了一件新商品!'); </script>";
}
else
{
echo "<script language='JavaScript'>alert('上传失败!');</script>";
exit;
}
}
/**************************************************************
图片上传,数据添加功能
upload_url---->存放上传文件的网络地址
upload_dir---->存放上传文件目录
**************************************************************/
$MAX_SIZE="500000";
$FILE_MIMES=array('image/jpeg','image/jpg','image/gif','image/png','image/pjpeg','application/msword');
$FILE_POSTFIX=array('.zip','.jpg','.gif','bmp');
$DELETE_ENABLE='no';
$site_name=$_SERVER['HTTP_HOST'];
$site_url="http://".$site_name.dirname($_SERVER['PHP_SELF']);
$site_this="http://".$site_name.$_SERVER['PHP_SELF'];
$visibility=true;
if ($visibility)
{
$upload_dir = "images/upload/public/";
}
else
{
$upload_dir = "images/upload/private/";
}
$upload_url = $site_url."/".$upload_dir;
if (!is_dir($upload_dir))
{
if (!mkdir($upload_dir))
die('文件没有创建成功!');
}
if ($_FILES['GoodsPhoto'])
{
if ($_FILES['GoodsPhoto']['size']==0)
{
echo "<script language='JavaScript'>location.href='shop.php';alert('必须输入项不能为空!');</script>";
}
$file_name=$_FILES['GoodsPhoto']['name'];
$file_type=$_FILES['GoodsPhoto']['type'];
$file_postfix=substr($file_name,strrpos($file_name,"."));
if ($_FILES['GoodsPhoto']['size'] >=$MAX_SIZE)
{
echo "<script language='JavaScript'>location.href='shop.php';alert('图片太大了,换个小点的吧!');</script>";
}
elseif (!in_array($file_type,$FILE_MIMES) && ! in_array($file_postfix,$FILE_POSTFIX))
{
echo "<script language='JavaScript'>location.href='shop.php';alert('上传文件不符合规定,请重新选择上传文件!');</script>";
}
else
{
if (empty($GoodsName) || empty($SalePrice) || empty($EMS) || empty($Delivery))
{
echo "<script language='JavaScript'>location.href='shop.php';alert('必须输入项不能为空!');</script>";
}
else
{
do_upload($upload_dir,$upload_url);
}
}
}
function do_upload($upload_dir,$upload_url)
{
$temp_name=trim($_FILES['GoodsPhoto']['tmp_name']);
$file_name=$_FILES['GoodsPhoto']['name'];
$file_name=str_replace("\\","",$file_name);
$file_name=str_replace("","",$file_name);
$file_path=$upload_dir.$file_name;
if ($file_name='')
{
echo "文件名无效!";
exit;
}
if ($result=move_uploaded_file($temp_name,$file_path))
{
echo "<script type='text/JavaScript' language='JavaScript'> location.href='shop.php'; alert('您成功的添加了一件新商品!'); </script>";
}
else
{
echo "<script language='JavaScript'>alert('上传失败!');</script>";
exit;
}
}
解决方案 »
- EclipsePHP Studio 问题
- *************************************GLPI中导出文件中文乱码的问题***************************
- 关于explode()的分隔符问题
- html文件中如何调用 php的 类中得方法
- PHP二维数组排序的问题,请看数组,php该如何对其排序
- 请教httpd.ini的一个写法
- Mysql4.0转到4.1之后,无法中文查询,字符设置问题?
- win2k 下 MYSQL与MSSQL是不是有冲突呢?
- 怎样上传MYSQL数据库???
- 救命!!一个网站有多个域名,如何判断用户使用哪个域名访问的?
- 对象的offsetTop指的是哪个?
- 请教关于session
/**
* 功能描述,实现上传功能,制作缩略图功能,和格式转换功能.
*
* 这里是详细说明.
* @author 钱国强
* @package lib
* @version v1.0
* @copyright juwangTech.
*
*/
class Upload
{
/**
* *
*
* @var 图片的全路径 $full_path
* @var 上传图片的基础路径 $full_path
* @var 出错信息 $error_message
* @var magic 的句柄 $magickwand
* @var 上传文件的名字. $name;
* @var 上传文件和最终路径.;
*
*/
public $full_path = null;
//通过配置文件来定义
public $base_path = UPLOAD_BAES_PATH;
public $max_size = null;
public $error_message = null;
public $magickwand = null;
public $name = null;
public $absolute_path = null;
public $dest_path = null;
public $dest_thumb_path = null;
/**
* *
*
* @param 上传路径设置. $upload_dir_name
*/
public function __construct($upload_dir_name='',$userId = '')
{
//检测路径
if (!function_exists('NewMagickWand')) {
exit();
}
//用户适配路径 xuxingPatch here for user dir.
$strAdapt = "";
//适配用户的文件路径.
if( $userId )
{
$strUid = str_pad( $userId , "6" , "0" , STR_PAD_LEFT );
$strAdapt = substr($strUid , 0 ,3)."/".substr( $strUid ,-3, 3)."/";
}
$this->full_path = $this->base_path . $strAdapt . $upload_dir_name;
}
public function check_full_path()
{
//xuxing make patch here for recursion make files
echo 'check_full_path';
if (!is_dir($this->full_path)) {
if (!mkdir($this->full_path, 0777, true)) {
if( DEBUG == "") echo '创建目录出错<br/>';
$this->error_message = '创建目录出错';
return false;
}
return true;
}
}
public function is_image($file)
{
$wand = NewMagickWand();
MagickReadImage($wand, $file);
$format = strtolower(MagickGetImageFormat($wand));
if ( $format == 'jpeg' || $format == 'png' || $format == 'gif') {
$this->magickwand = $wand;
return $format;
}
}
public function check_error($error)
{
switch ($error) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
$this->error_message = '文件超过上传最大限制';
break;
case UPLOAD_ERR_NO_FILE:
$this->error_message = '没有文件上传';
break;
case UPLOAD_ERR_PARTIAL:
$this->error_message = '文件上传不完整';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$this->error_message = '临时文件不存在';
break;
case UPLOAD_ERR_CANT_WRITE:
$this->error_message = '文件写入失败';
break;
}
}
public function add_( $l = 0, $t = 0)
{
//防止为过小的图片打标.
list($width, $height, $type, $attr) = getimagesize($this->absolute_path);
$_image = NewMagickWand();
MagickReadImage($_image, MARK_IMAGE);
MagickCompositeImage($this->magickwand, $_image, MW_OverCompositeOp, $l, $t);
MagickWriteImage($this->magickwand, $this->absolute_path);
}
/**
* *
* 功能描述.将图片按指定的格式缩略到指定的大小.
* @param 缩略图的格式. $format
* @param 缩略图的宽度 $width
* @param 缩略图的高度 $height
*
* //可能目标路径的设置根据应用程序的需求来调整.
* //可能缩略的默认规则,根据应用程序的需求来调整. 不是很重要.
*/
public function save_to_thumb($format, $width, $height, $bl = false)
{
$percent = 0.5;
if (null === $width) {
$width = MagickGetImageWidth($this->magickwand);
$new_width = $width * $percent;
} else {
$new_width = $width;
}
if (null === $height) {
$height = MagickGetImageHeight($this->magickwand);
$new_height = $height * $percent;
} else {
$new_height = $height;
}
if ($bl) {
//等比例缩放部分
$bl_image = NewMagickWand();
MagickNewImage( $bl_image, $new_width, $new_height, '#FFFFFF');
MagickSetFormat($bl_image, $format);
$bl_src_image_width = MagickGetImageWidth($this->magickwand);
$bl_src_image_height = MagickGetImageHeight($this->magickwand);
$top = 0;
$left = 0;
if ($bl_src_image_width > $bl_src_image_height) {//宽大于高的图片
if ($bl_src_image_width > $new_width) {//图宽大于缩放图的宽
$bl_width = $new_width;
$prop = $new_width/$bl_src_image_width;
$bl_height = $bl_src_image_height * $prop;
MagickScaleImage($this->magickwand, $bl_width, $bl_height);
$top = round(($new_height-$bl_height)/2);
MagickCompositeImage($bl_image, $this->magickwand, MW_OverCompositeOp, $left, $top);
$part_name = explode('.', $this->name); //得到扩展名,和基础名.
MagickWriteImage($bl_image, $this->full_path . '/' . $part_name[0] . "$bl_width$bl_heidht." . $format);
}
} elseif ($bl_src_image_width < $bl_src_image_height) {//宽小于高的图片
if ($bl_src_image_height > $new_height) {//图高大于缩放图的高
$bl_height = $new_height;
$prop = $new_height/$bl_src_image_height;
$bl_width = $bl_src_image_width * $prop;
MagickScaleImage($this->magickwand, $bl_width, $bl_height);
$left = round(($new_width-$bl_width)/2);
MagickCompositeImage($bl_image, $this->magickwand, MW_OverCompositeOp, $left, $top);
$part_name = explode('.', $this->name); //得到扩展名,和基础名.
MagickWriteImage($bl_image, $this->full_path . '/' . $part_name[0] . "$bl_width$bl_heidht." . $format);
}
}
}
//创建handle
$thumb_image = NewMagickWand(); //创建一个画布.
MagickNewImage( $thumb_image, $new_width, $new_height, '#FFFFFF');
//设置handle 的输出格式.
MagickSetFormat($thumb_image, $format); //伸缩图像.
MagickScaleImage($this->magickwand, $new_width, $new_height);
//贴到新的图片上去.
MagickCompositeImage($thumb_image, $this->magickwand, MW_OverCompositeOp, 0, 0);
$part_name = explode('.', $this->name); //得到扩展名,和基础名.
//刷出缩略图文件.
MagickWriteImage($thumb_image, $this->full_path . '/' . $part_name[0] . "$new_width$new_height." . $format);
$this->dest_thumb_path = $part_name[0] . "$new_width$new_height." . $format;
DestroyMagickWand($thumb_image); }
/**
* *
*
* @param 要保存的文件名 $name
* @param 另存为文件名. $new_name
* @param bool型是否加waterprint $use_
* @param 伸缩后的宽度 $width
* @param 伸缩后的高度 $height
* @return mix.
*
* 另存文件名的规则可能变化会很多.概要设计完成后进行一次整体的调整.
*/
public function save_to_as($name, $new_name = null, $use_=false, $width = null, $height = null, $bl=false)
{
echo 'coming HEre'; //检查上传文件是否有错.
$this->check_error($_FILES[$name]['error']);
if (null !== $this->error_message) {
echo $this->error_message;
}
$appendixArray = explode ( "." ,$_FILES[$name]['name'] );
$appendix = array_pop ( $appendixArray );
echo 'coming HEre';
//另存名称适配.
if (null === $new_name || !$new_name) {
$this->name = basename($_FILES[$name]['name']);
$this->dest_path = str_replace(' ', '', $this->name);
$this->absolute_path = $this->full_path .'/' . str_replace(' ', '', $this->name);
} else {
$this->name = $new_name;
$this->dest_path =str_replace(' ', '', $new_name).'.'.$appendix;
$this->absolute_path = $this->full_path .'/' . str_replace(' ', '', $new_name).'.'.$appendix;
}
echo 'coming HEre';
//检查全路径是否正确.没有的话,则创建.
if ($this->check_full_path() === false) {
return $this->error_message;
}
//上传图片.
if( is_file ( $this->absolute_path))
copy ( $this->absolute_path ,$this->absolute_path.date("Y-m-d_h_i_s") );
if ( !(move_uploaded_file( $_FILES[ $name ][ 'tmp_name' ] , $this->absolute_path ))) {
$this->error_message = '移动文件出错';
return false;
} else {
if ($format = $this->is_image( $this->absolute_path )) {
$this->save_to_thumb( $format , $width , $height ,$bl);
}
if ( $use_ ) {
$this->add_();
}
}
return true;
}}
?>
/**
* *单独功能函数.格式转换.
* @param 原图片. $file_path
* @param unknown_type $format
*
* 当jpg 时,要调整格式质量.
*/
function format_change($file_path, $format)
{
$image = NewMagickWand();
MagickReadImage($image, $file_path);
$ori_format = MagickGetImageFormat($image);
if ($format == 'jpg') $format = 'jpeg';
if ( $ori_format != $format ) {
$part_name = explode('.', $file_path);
array_pop($part_name);
array_push($part_name, $format);
$new_name = implode('.', $part_name);
MagickSetFormat($image, $format);
MagickWriteImage($image, $new_name);
}
}/**
* *单独功能函数.将指定文件转成flv文件..
*
* @param 原文件路径. $filePath
* @param 目标视频路径 $destPath
*
*
*/function makeFlv($filePath ,$destPath)
{
$shell = "ffmpeg -i $filePath -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 $destPath ";
echo $shell;
echo exec ( $shell );
}
/**
* *单独功能函数.将指定文件转成GIF..
*
* @param 原文件路径. $filePath
* @param 目标视频路径 $destPath
* @param 要转换的帧数.
*
*
*/function makeGif($filePath, $destPath, $frameNum = 100)
{
$shell = "ffmpeg -i $filePath -vframes $frameNum -y -f gif $destPath ";
echo $shell;
echo exec ( $shell );
}例子.
<?
require( "/home/httpd/web/global/globalPath.php" );
require( "/home/httpd/web/lib/upload.php" );
$myUp = new Upload( "user_photo" );
$date = date("Y-m-d_h_i_s");
$myUp->save_to_as( "tempUpload" , $date , false, 125, 125 );
?>