具有多个图像上载的动态文件名

时间:2012-07-22 18:22:18

标签: php image upload

我正在使用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
            }
        }
    }
}   
?>

1 个答案:

答案 0 :(得分:0)

您可以定义

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

开头,用

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

这样'file'就会变成'file.1.jpg',等等。