【问题标题】:How can i rename multiple files on upload如何在上传时重命名多个文件
【发布时间】:2013-07-09 17:09:55
【问题描述】:

我在这里遇到了一个小问题。我使用以下表单允许用户上传一个或多个文件:

<form action="RejoinReq.php" method="post" enctype="multipart/form-data">
    Attachment 1: <input type="file" name="file[]" id="file" maxlength="500" accept="application/pdf,image/*" />
    <input type="submit" name="submit" value="Request" />
</form>

接下来,我需要重命名文件,然后再将它们保存到服务器。为此,我正在尝试使用:

RejoinReq.php

<?php

function findexts ($filename) {
    $filename = strtolower($filename);
    $exts = explode(".", $_FILES["file"]["name"]);
    $n = count($exts) - 1;
    $exts = $exts[$n];
    return $exts;
}
$ext = findexts($_FILES['file']['name']);
$target = "upload/";
$target = $target . $_SESSION['myusername'] . "Rejoin." . $ext;
if (file_exists("upload/" . $_FILES["file"]["name"])) {
    //echo $_FILES["file"]["name"] . " already exists. ";
}
else {
    move_uploaded_file($_FILES["file"]["tmp_name"],$target);
    //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}

更新 - 修改 PHP 代码

警告:findexts() 期望参数 1 是字符串,给定数组

警告:move_uploaded_file() 期望参数 1 是字符串,给定数组

【问题讨论】:

  • 首先,您的代码中无需大写所有内容。那个烦人。另外,尝试使用file inputmultiple 属性
  • 这不是全部,但无论如何我更新了我的代码,很抱歉打扰你,现在我在我的代码中添加了multiple="muliple",仍然是同样的错误。
  • 如果$_FILES["file"] 不是 empy,该代码将适用于 1 个循环(请先检查)。另外:不要在循环中定义函数,并使其findexts($name),并让它使用它的参数而不是$_FILES再次超全局..
  • “对JS代码的修改”..但是JavaScript代码在哪里??
  • 您的findext() 函数完全没用且多余。请改用pathinfo($filename, PATHINFO_EXTENSION)

标签: php file-upload


【解决方案1】:

此代码需要完全重写才能有用。

<?PHP
$filekeys=array_keys($_FILES);
foreach($filekeys as $activefile)
    {
    if($_FILES[$activefile]['error']==0)

        {
        $file= fopen($_FILES[$activefile]['tmp_name'],'r');
        $content = fread($file, filesize($_FILES[$activefile]['tmp_name']));
        fclose($file);
            //determine $target here
            $file2= fopen($target,"w");
            fwrite($file2,$content);
            fclose($file2);
            }
    }
?>

此代码适用于任意数量的文件,但您确实需要将 $target 更改为您希望它为每个文件设置的任何值。

这有点过于复杂,但它确实允许您操纵 $content。它改编自我用来将加密文件存储在数据库中的一些代码,因此需要提取文件内容。

【讨论】:

  • 如果我错了请纠正我,这个函数会打开临时文件,创建一个新文件然后关闭它???
  • 是的,你是对的,我提供的代码会打开临时文件,使其内容作为 $content 可用,然后将 $content 保存为新文件 $target。临时文件将自行删除。支持发布您自己的修复。
【解决方案2】:

这里,这应该就是你所需要的!

index.html 格式为:

<form action="RejoinReq.php" method="post" enctype="multipart/form-data">
    Attachment(s): <input type="file" name="file[]" id="file" maxlength="500" accept="application/pdf,image/*" multiple>
    <input type="submit" name="submit" value="Request">
</form>

以及接收RejoinReq.php

<?php

// config
$upload_dir = '/var/www/html/upload'; // set your upload dir
$max_size = 1048576; // max file size: 1 MB
$allow_override = FALSE; // allow uploading files overriding existing ones
$valid_exts = array( // allowed extensions
    'gif',
    'jpeg',
    'jpg',
    'png',
    'pdf',
);
$valid_types = array(
    'image/gif',
    'image/jpeg',
    'image/jpg',
    'image/pjpeg',
    'image/x-png',
    'image/png',
    'text/pdf',
    'application/pdf',
);

// reorganize files array
$files = array();
foreach ($_FILES['file'] as $attr => $arr) {
    foreach ($arr as $k => $v) {
        $files[$k][$attr] = $v;
    }
}

// loop thru files
foreach ($files as $file) {
    $status = 'Failure';

    // get extension
    $extension = pathinfo($file['name'], PATHINFO_EXTENSION);

    // make sure extension and type are not empty
    if ( ! (strlen($extension) && strlen($file['type']))) {
        $msg = 'File extension or type not found';
    }
    else {

        // make sure extension and type are allowed
        if ( ! (in_array($file['type'], $valid_types) && in_array($extension, $valid_exts))) {
            $msg = "Extension '$extension' or file type '$file[type]' is not permitted";
        }
        else {

            // make sure file is not empty
            if ( ! $file['size']) {
                $msg = 'File seems to be empty (0 KB)';
            }
            else {

                // make sure file is not too large
                if ($file['size'] > $max_size) {
                    $msg = 'File is too large (' . ceil($file['size'] / 1024) . 'kB > ' . floor($max_size / 1024) . 'kB)';
                }
                else {

                    // rename file here as you need
                    $target = "$upload_dir/$_SESSION[myusername]Rejoin.$file[name]";

                    // make sure files don't override
                    if ( ! $allow_override && file_exists($target)) {
                        $msg = "File already exists";
                    }
                    else {

                        // no other errors
                        if ($file['error'] > 0) {
                            $msg = "Unknown upload error (Code: $file[error])";
                        }
                        else {

                            // attempt uploading
                            if ( ! move_uploaded_file($file['tmp_name'], $target)) {
                                $msg = 'Upload failed. Folder issues?';
                            }
                            else {

                                // all good!
                                $msg = 'Upload successful!';
                                $status = 'Success';
                            }
                        }
                    }
                }
            }
        }
    }
    $out[] = "$file[name]: $status. $msg";
}

echo implode("\n", $out);

/* End of file */

【讨论】:

  • 不要使用 $_FILES 中的['type'] 属性以确保安全。滥用是微不足道的。并且不要使用数组操作来额外的文件扩展名。 PHP 的原生 pathinfo() 已经为您做到了。
  • @MarcB,感谢您的反馈! HTML 表单中有一个accept="application/pdf,image/*" 属性。然后我使用['type'] 结合扩展名(文件名的一部分)作为几个检查之一。如果您发现它完全没用,请随时编辑我的答案。
  • @Geo,首先感谢您的努力,我尝试运行该代码,但出现错误:Strict Standards: Only variables should be passed by reference in 在行中 ` if ( ! (strlen($extension = end(explode(' .', $file['name']))) && strlen($file['type']))) {`
  • 现在可以用了,我修改了$checkext = explode('.', $file['name']); $extension = end($checkext); $filetype = $file['type']; if ( ! (strlen($extension)) &amp;&amp; strlen($filetype)) {
  • 嗯.. 看起来严格的标准不允许将命令传递给end() 函数。谢谢!现已修复
【解决方案3】:

试试这个:

<?php

class Upload_Rename{


    const ALLOWED_TYPES = "jpg,gif,png";

    public static function generate_new_name($extension,$uppercase=true,$prefix='',$sufix=''){
        $new_name = $prefix.uniqid().'_'.time().$sufix;
        return ($uppercase ? strtoupper($new_name) : $new_name).'.'.$extension;
    }

    public static function check_and_get_extension($file){

        $file_part      = pathinfo($file);
        $allowed_types  = explode(",",Upload_Rename::ALLOWED_TYPES);

        if(!in_array($file_part['extension'], $allowed_types)){
            throw new Exception('Not ok.. bad bad file type.');
        }

        return $file_part['extension'];
    }


    public function upload($file,$target_destination){

        if(!isset($file['tmp_name'])){
            throw new Exception('Whaaaat?');
        }

        $_name   = $file['name'];
        $_tmp    = $file['tmp_name'];
        $_type   = $file['type'];
        $_size   = $file['size'];


        $file_extension = '';

        try{
            $file_extension = Upload_Rename::check_and_get_extension($_name);
        }catch(Exception $e){
            throw new Exception('Ops.. file extension? what? '.$e->getMessage());
        }

        $new_name    = Upload_Rename::generate_new_name($file_extension,true,'whaat_','_okey');
        $destination = $target_destination . DIRECTORY_SEPARATOR . $new_name;

        return move_uploaded_file($_tmp, $destination);
    }

    public function multiple_files($files,$destination){

        $number_of_files = isset($files['tmp_name']) ? sizeof($files['tmp_name']) : 0;

        $errors = array();

        for($i=0;$i<$number_of_files;$i++){
            if(isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])){
                try{
                    $this->upload(array(
                        'name'=>$files['name'][$i],
                        'tmp_name'=>$files['tmp_name'][$i],
                        'size'=>$files['size'][$i],
                        'type'=>$files['type'][$i]
                    ),$destination);
                }catch(Exception $e){
                    array_push($errors,array('file'=>$files['name'][$i],'error'=>$e->getMessage()));
                }
            }
        }

        print_r($errors);
    }

}

if($_FILES){
    $upload = new Upload_Rename();
    $destination = dirname(__FILE__);
    $upload->multiple_files($_FILES['myfile'],$destination);
}
?>

<form  method="post" enctype="multipart/form-data">
    <?php for($i=0;$i<10;$i++): ?>
    file: <input type="file" name="myfile[]"><hr>
    <?php endfor; ?>
    <input type="submit">
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2012-06-24
    • 2017-03-19
    相关资源
    最近更新 更多