<?
$upload_file=$_FILES["img"]["name"];        //获取文件名
$upload_tmp_file_1=$_FILES["img"]["tmp_name"];      //获取临时文件名
$upload_tmp_file=$_FILES["img"]["tmp_name"];      //获取临时文件名
$upload_filetype=$_FILES["img"]["type"];    //获取文件类型
$upload_size=$_FILES["img"]["size"];    //获取文件大小
$upload_status=$_FILES["img"]["error"];   //获取文件出错情况//定义允许上传的文件扩展名
$ext_arr = array('jpg');
$temp_arr = explode(".", $upload_file);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
$size2_1 = @getimagesize($upload_tmp_file);
$size2 = @getimagesize($upload_tmp_file);
//下面生成第一个310大小的缩略图
if(is_uploaded_file($upload_tmp_file_1) )
{
 $upload_file_1   =   $time."_1.".$file_ext; 
 $upload_path_1=$dir."/".$upload_file_1;   //定义文件最终的存储路径和名称
if(move_uploaded_file($upload_tmp_file_1,$upload_path_1)){$image_p_1 = imagecreatetruecolor($srcW_1, $srcH_1);
$image_1 = imagecreatefromjpeg($upload_path_1);
imagecopyresampled($image_p_1, $image_1, 0, 0, 0, 0, $srcW_1, $srcH_1, 310, 310);
imagejpeg($image_p_1,$upload_path_1);
}}//再生成第二个大小为620的缩略图
if(is_uploaded_file($upload_tmp_file) )
{
 $upload_file   =   $time.".".$file_ext; 
 $upload_path=$dir."/".$upload_file;   //定义文件最终的存储路径和名称
if(move_uploaded_file($upload_tmp_file,$upload_path)){
$image_p = imagecreatetruecolor($srcW, $srcH);
$image = imagecreatefromjpeg($upload_path);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $srcW, $srcH, 620, 620);
imagejpeg($image_p,$upload_path);
}
}//虽然重复了二步,但只生成了第一个缩略图,第二个没有生成,不知为什么,删除生成第一个缩略图的代码,又能生成第二个的,不知为什么不能同时生成二个,如何实现?
?>

解决方案 »

  1.   

    if( move_uploaded_file($upload_tmp_file_1,$upload_path_1)){
    请注意,第一次你已经把上传的图片移动到 $upload_path_1,那么第二次
    if( move_uploaded_file($upload_tmp_file,$upload_path)){ // $upload_tmp_file也就不存在了
    就当然会出错了
    解决方法当然是 两次操作共用一个图片地址,而不是每次都要move_uploaded_file();
      

  2.   


    去掉 if( move_uploaded_file($upload_tmp_file_1,$upload_path_1)){
    上传的缩略图成全黑了,不对啊,请帮忙!
      

  3.   

    帮你整理了一下,完了建议你最好在一开始写代码前先计划计划,能合并的最好合并,那么后面的活接着就很轻松了<?php
    $upload_file=$_FILES["img"]["name"]; //获取文件名
    $upload_tmp_file=$_FILES["img"]["tmp_name"]; //获取临时文件名
    $upload_filetype=$_FILES["img"]["type"]; //获取文件类型
    $upload_size=$_FILES["img"]["size"]; //获取文件大小
    $upload_status=$_FILES["img"]["error"]; //获取文件出错情况//定义允许上传的文件扩展名
    $ext_arr = array('jpg');
    $temp_arr = explode(".", $upload_file);
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext);
    // 这前面没动// 把文件移动到 $upload_path, $upload_path是原图文件的地址
    $upload_src = $_SERVER['DOCUMENT_ROOT'].'image/'.$upload_file;
    move_uploaded_file( $upload_tmp_file, $upload_src);$image_size = getimagesize( $upload_src); // 原图大小
    $image = imagecreatefromjpeg( $upload_src);function creatImage( $new_image_src , $newW, $newH ) //创建缩略图
    {
    global $image_size; // 上传原图的大小
    global $image;      // 上传的原图$image_p = imagecreatetruecolor($newW, $newH);imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $image_size[0], $image_size[1]);
    imagejpeg($image_p, $new_image_src);
    }// 然后调用
    // 缩略图存放的位置我给写的就是WWW目录下的image目录,和原图都在一起
    $newImageSrc = $_SERVER['DOCUMENT_ROOT']."/image/small".$upload_file;
    // 尺寸是310*310
    creatImage( $newImageSrc, 310, 310);$newImageSrc = $_SERVER['DOCUMENT_ROOT']."/image/big".$upload_file;
    creatImage( $newImageSrc, 620, 620);?>