For those of us who have been using frameworks, native code is already very unfamiliar. Let’s go straight to the code without further ado.
Front code
<?php
//var_dump(gd_info());die();
header("Content-Type:text/html;charset=gbk");
$filepatch=$_GET['img_path'];
$type=$_GET['type'];
function my_scandir($dir){
$files=array();
if(is_dir($dir)){
if($handle=opendir($dir)){
while(($file=readdir($handle))!==false){
if($file!='.' && $file!=".."){
if(is_dir($dir."/".$file)){
$files[$file]=my_scandir($dir."/".$file);
}else{
$extend = explode("." , $file);
$attr = strtolower($extend[count($extend) - 1]);
if(in_array($attr,array('jpg','png','jpeg','gif'))){
$files[]=$dir."/".$file;
}
}
}
}
}
closedir($handle);
}
return $files;
}
?>
<body>
<?php
if($type=='1'){
?>
<form action="action.php?img_path=<?php echo $filepatch;?>" method="post" enctype="multipart/form-data" name="upload_form">
<label>请选择图片/label>
<input name="file" type="file" accept="image/*"/>
<input id="upload" name="upload" type="submit" value="上传" />
</form>
<?php
}else{ ?>
<form action="action.php?img_path=<?php echo $filepatch;?>" method="post" enctype="multipart/form-data" name="upload_form">
<label>请选择图片</label>
<input name="file" type="file" accept="image/*"/>
<input id="upload" name="upload" type="submit" value="上传" />
</form>
<?php
$picArr = my_scandir("./ItemCode/".$filepatch);
foreach ($picArr as $k=>$v){
?>
<img src="<?php echo $v;?>" title="" width="200px" height="300px">
<br/>
<?php
}
}
?>
</body>
backend code
<?php
$img_path=trim($_GET['img_path']);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp); // 获取文件后缀名
if (in_array($extension, $allowedExts)){
$timename=date('YmdHis',time()).rand(0,9999).".".$extension;
if (!file_exists("./Product/".$img_path)) {
mkdir ("./Product/".$img_path, 0777, true );
}
elseif(!file_exists('ItemCode')){
mkdir ("./ItemCode/".$img_path, 0777, true );
}
elseif(!file_exists('FNname')){
mkdir ("./FNname/".$img_path, 0777, true );
}
elseif(!file_exists('seq')){
mkdir ("./seq/".$img_path, 0777, true );
}
$name="./Image/".$img_path.'/'.$timename;
if(move_uploaded_file($_FILES["file"]["tmp_name"], $name)){
// $arr=array('status'=>'1','msg'=>'上传成功','pic'=>$timename);
// echo "javascript:alert(''执行成功!'');";
echo "<script> alert('上传成功'); </script>";
}else{
// $arr=array('status'=>'0','msg'=>'上传失败');
echo '上传失败';
}
}else
{
$arr=array('status'=>'0','msg'=>'文件格式不合法');
echo json_encode($arr);
}
$name="./Image".$img_path.'/'.$timename;
//缩略图保存地址
$smallfile = "./ItemCode".$img_path.'/'.$timename;
if($_FILES['file']['type'] == $allowedExts)
{
echo '文件类型错误';
}
else {
move_uploaded_file($_FILES['file']['tmp_name'], $smallfile); //上传文件
$dstW = 200;//缩略图宽
$dstH = 200;//缩略图高
$src_image = imagecreatefrompng($name);
$srcW = ImageSX($src_image); //获得图片宽
$srcH = ImageSY($src_image); //获得图片高
$dst_image = ImageCreateTrueColor($dstW, $dstH);
ImageCopyResized($dst_image, $src_image, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH);
ImageJpeg($dst_image, $smallfile);
echo '文件上传成功<br>';
echo "<img src='$smallfile' />";
}