2012-07-22 47 views
3

我正在使用class.upload.php,我的代码都工作,除了我想扩展我的脚本工作与多个文件上传我阅读网站上的文档,并能够想办法。不过,我需要我的图像文件输出,如m_1234_1,m_1234_3,m_1234_4等等......如何让$handle->file_new_name_body = $new_name;$new_name.'1'开头并继续为每次迭代添加1?动态文件名与多个图像上传

<?php 
    $id = $_GET['id']; 
    if(isset($_POST['submit'])) { 
     // define variables 
     $new_name = 'm_'.$id.'_'; 
     $thumb_name = 't_'.$id.'_'; 
     $ext = 'jpg'; 
     $upload_path = 'images/uploads/'.$id.'/'; // will not work with /images/ 
     $full_src = $upload_path.$new_name.'.'.$ext; 
     // end define variables 

     $files = array(); 
     foreach ($_FILES['userfile'] as $k => $l) { 
     foreach ($l as $i => $v) { 
     if (!array_key_exists($i, $files)) 
      $files[$i] = array(); 
      $files[$i][$k] = $v; 
     } 
     }  

     foreach ($files as $file) { 

      $handle = new upload($_FILES['userfile']); 
      if ($handle->uploaded) { 
       // save uploaded image 458 x 332 
       $handle->file_new_name_body = $new_name; 
       $handle->image_convert = $ext; 
       $handle->allowed = array('image/*'); 
       $handle->jpeg_quality = 95; 
       $handle->image_resize = true; 
       $handle->image_ratio_crop = true; 
        $handle->image_x = 458; 
        $handle->image_y = 332; 
       $handle->file_overwrite = true; 
       $handle->auto_create_dir = true; 
       $handle->process($upload_path); 
       if ($handle->processed) { 
        mysql_select_db($db); 
        mysql_query("UPDATE projects SET last_modified=NOW(), project_image_1 = '".$full_src."' WHERE id = $id") or die(mysql_error()); 
       } else { 
        echo '<div class="ec-messages messages-error">'; 
        echo 'Error: ' . $handle->error; 
        echo '</div>'; 
       } 
       // create thumbnail 104 x 76 
       $handle->file_new_name_body = $thumb_name; 
       $handle->image_convert = $ext; 
       $handle->allowed = array('image/*'); 
       $handle->jpeg_quality = 90; 
       $handle->image_resize = true; 
       $handle->image_ratio_crop = true; 
        $handle->image_x = 104; 
        $handle->image_y = 76; 
       $handle->file_overwrite = true; 
       $handle->auto_create_dir = true; 
       $handle->process($upload_path); 
       if ($handle->processed) { 
        echo '<div class="ec-messages messages-success">Image successfully uploaded and added to database (thumnails created)<br>Redirecting to <a href="projects.php?msg=insert">projects main</a>...</div><br><img src="'.$full_src.'" class="display-image">'; 
        echo "<script>setTimeout(\"location.href='projects.php?msg=insert';\",2000);</script>"; 
        include('Templates/footer_exit.php'); 
        $handle->clean(); 
        exit; 
       } else { 
        // no error here, error will be handled by the first script 
       } 
      } 
     } 
    } 
    ?> 

被更新(现在的工作):

 <?php 
$id = $_GET['id']; 
if(isset($_POST['submit'])) { 
    // define variables 
    $ext = 'jpg'; 
    $upload_path = 'images/uploads/'.$id.'/'; // will not work with /images/ 
    // end define variables 

    $files = array(); 
    foreach ($_FILES['userfile'] as $k => $l) { 
    foreach ($l as $i => $v) { 
    if (!array_key_exists($i, $files)) 
     $files[$i] = array(); 
     $files[$i][$k] = $v; 
    } 
    }  
    $counter = 1; 
    foreach ($files as $file) { 

     //$append = rand(100,99999); 
     $new_name = 'm_'.$id; 
     $thumb_name = 't_'.$id; 
     $handle = new upload($file); 
     if ($handle->uploaded) { 
      // save uploaded image 458 x 332 
      $count = $counter++; 
      $nn = sprintf("%s_%d", $new_name, $count); 
      $full_src = $upload_path.$nn.'.'.$ext; 
      $handle->file_new_name_body = $nn; 
      $handle->image_convert = $ext; 
      $handle->allowed = array('image/*'); 
      $handle->jpeg_quality = 95; 
      $handle->image_resize = true; 
      $handle->image_ratio_crop = true; 
       $handle->image_x = 458; 
       $handle->image_y = 332; 
      $handle->file_overwrite = true; 
      $handle->auto_create_dir = true; 
      $handle->process($upload_path); 
      if ($handle->processed) { 
       mysql_select_db($db); 
       mysql_query("UPDATE projects SET last_modified=NOW(), project_image_".$count." = '".$full_src."' WHERE id = $id") or die(mysql_error()); 
      } else { 
       echo '<div class="ec-messages messages-error">'; 
       echo 'Error: ' . $handle->error; 
       echo '</div>'; 
      } 
      // create thumbnail 104 x 76 
      $tn = sprintf("%s_%d", $thumb_name, $count); 
      $handle->file_new_name_body = $tn; 
      $handle->image_convert = $ext; 
      $handle->allowed = array('image/*'); 
      $handle->jpeg_quality = 90; 
      $handle->image_resize = true; 
      $handle->image_ratio_crop = true; 
       $handle->image_x = 104; 
       $handle->image_y = 76; 
      $handle->file_overwrite = true; 
      $handle->auto_create_dir = true; 
      $handle->process($upload_path); 
      if ($handle->processed) { 
       echo 'Done!'; 
       /* 
       echo '<div class="ec-messages messages-success">Image successfully uploaded and added to database (thumnails created)<br>Redirecting to <a href="projects.php?msg=insert">projects main</a>...</div><br><img src="'.$full_src.'" class="display-image">'; 
       echo "<script>setTimeout(\"location.href='projects.php?msg=insert';\",2000);</script>"; 
       include('Templates/footer_exit.php'); 
       $handle->clean(); 
       exit; 
       */ 
      } else { 
       // no error here, error will be handled by the first script 
      } 
     } 
    } 
} 
?> 

回答

0

你可能会在一开始定义

$new_name = 'm_'.$id.'_'; 
$counter = 1;    // File counter 

,并与

$handle->file_new_name_body = sprintf("%s.%d", $new_name, $counter++); 

让“文件替换该行'会变成'file.1.jpg',等等ñ。

+0

我想通了你,更新代码上面的作品!感谢您的帮助! – Alex 2012-07-22 21:07:08