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
            }
        }
    }
}   
?>
4

1 回答 1

0

你可以定义

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

在开头,并将该行替换为

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

这样“文件”就会变成“file.1.jpg”,依此类推。

于 2012-07-22T18:59:09.953 回答