php - 具有多个图像上传的动态文件名

标签 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
            }
        }
    }
}   
?>

最佳答案

你可以定义

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

在开头,并将该行替换为

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

因此"file"将变为“file.1.jpg”,依此类推。

关于php - 具有多个图像上传的动态文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602844/

相关文章:

java - JSP:上传文件到服务器的最佳实践

text - 我怎样才能像这两个网址一样在博客上创建或上传纯文本文件?

php - 使用 simpleXML 编辑 XML

php - Joomla 对禁用缓存的请求执行的数据库查询总量是多少?

java - 将文本内容转换为图像

c# - 字符串或二进制数据将被截断。该语句已终止。上传个人资料时

php - 在基于WEB的应用程序中使用PHP进行MY SQL数据库连接

php - DOMXPath - 获取节点

html - 如何防止在调整大小时更改父级的宽度

c# - FTP 无法正确上传文件