我怎样才能重新上传多个文件 [英] How can i rename multiple files on upload

查看:173
本文介绍了我怎样才能重新上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里面临一个小问题。我有以下的形式,我用它来允许用户上传一个或多个文件:

 < form action =RejoinReq。 phpmethod =postenctype =multipart / form-data> 
附件1:< input type =filename =file []id =filemaxlength =500accept =application / pdf,image / */>
< input type =submitname =submitvalue =Request/>
< / form>

接下来,我需要在将文件保存到服务器之前重命名文件。为此,我试图使用:
$ b $ p 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']。 归队。 。 $ EXT;
if(file_exists(upload /。$ _FILES [file] [name])){
// echo $ _FILES [file] [name]。 已经存在。 ;
}
else {
move_uploaded_file($ _ FILES [file] [tmp_name],$ target);
// echoStored in:。 上传/。 $ _FILES [ 文件] [ 名称];

更新 - 修改PHP代码


警告: findexts()期望参数1是字符串, / em>



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



解决方案

在这里,这应该是你需要的!
$ b

index.html 的形式:

 < form action =RejoinReq.phpmethod =postenctype =multipart / form-data> 
附件:< input type =filename =file []id =filemaxlength =500accept =application / pdf,image / *multiple>
< input type =submitname =submitvalue =Request>
< / form>

并接收 RejoinReq.php

 <?php 

// config
$ upload_dir ='/ var / www / html / upload'; //设置你的上传目录
$ max_size = 1048576; //最大文件大小:1 MB
$ allow_override = FALSE; //允许上传文件覆盖现有文件
$ valid_exts = array(//允许的扩展名
'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',
);

//重组文件数组
$ files = array();
foreach($ _FILES ['file'] as $ attr => $ arr){
foreach($ arr as $ k => $ v){
$ files [$ k ] [$ attr] = $ v;



循环遍历文件
foreach($ files as $ file){
$ status ='Failure';

//获取扩展名
$ extension = pathinfo($ file ['name'],PATHINFO_EXTENSION);

//确保扩展名和类型不为空
if(!(strlen($ extension)&& strlen($ file ['type']))){
$ msg ='文件扩展名或类型未找到';

$ {

//确保扩展名和类型是允许的
if(!(in_array($ file ['type'],$ valid_types)& ;& in_array($ extension,$ valid_exts))){
$ msg =扩展名'$ extension''或文件类型'$ file [type]'是不允许的;

$ {b
$ b //确保文件不为空
if(!$ file ['size']){
$ msg = '文件似乎是空的(0 KB)';

else {

//确认文件不是太大
if($ file ['size']> $ max_size){
$ msg ='文件过大('.mem($ file ['size'] / 1024)。'kB>'。floor($ max_size / 1024)。'kB)';
}
else {

//在这里重命名文件,因为您需要
$ target =$ upload_dir / $ _ SESSION [myusername]重新加入$ file [name] ;
$ b $ //确保文件不会覆盖
if(!$ allow_override&& file_exists($ target)){
$ msg =文件已经存在;

$ {b
$ b //没有其他错误
if($ file ['error']> 0){
$ msg =未知的上传错误(代码:$文件[错误]);
}
else {

//尝试上传
if(!move_uploaded_file($ file ['tmp_name'],$ target)){
$ msg ='上传失败。文件夹问题?
}
其他{

//都好!
$ msg ='上传成功!';
$ status ='成功';






$ out [] =$ file [name] :$ status。$ msg;
}

echo implode(\\\
,$ out);

/ *文件结尾* /


I am facing a small issue here. I have the following form which I use to allow users to upload one or more files:

<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>

Next, I need to rename the file(s) before saving them to the server. For that I am trying to use:

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"];
}

Updated - Modification on PHP code

Warning: findexts() expects parameter 1 to be string, array given

Warning: move_uploaded_file() expects parameter 1 to be string, array given

解决方案

Here, this should be all you need!

index.html with the form:

<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>

And the receiving 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 */

这篇关于我怎样才能重新上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆