【问题标题】:uploading multiple files, renaming with form data上传多个文件,使用表单数据重命名
【发布时间】:2013-08-23 22:50:38
【问题描述】:

好的,首先,我是新来的,所以如果我发布的不正确,请告诉我。我确实在这个网站上进行了广泛的搜索,但没有找到我的问题。

我有一个 php 表单和上传器,它应该上传多个文件并根据表单名称字段中的响应和上传日期重命名文件(将名称和日期添加到原始文件名)。

一切正常,除了:只上传一个文件,而不是多个文件。当我写这篇文章时,它确实有效,但我最近搞砸了一些事情。

谁能告诉我哪里出错了? 谢谢。

这是表单的代码:

<?php

// make a note of the current working directory relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// location of the upload handler
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.processor.php';

// max file bytes (2mb)
$max_file_size = 2097152;

// echo 
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
   <head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <title>Upload Your Awesome Images!</title>
    </head>

    <body>
    <form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
    <span>Step #2</span>
        <h1>
            Upload Your Images!
        </h1>
        <div style="border:#999 1px solid;padding:5px">
        <p>
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
        </p>

        <p> Photographer's Name: <input type="text" name="photogname" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>"> </p>

        <p>
            <label for="file">File to upload:</label>
            <input id="file" type="file" name="file[]" class="tag" multiple/>
        </p>

        <p>
            <label for="submit">Press to...</label>
            <input id="submit" type="submit" name="submit" value="Upload me!">
        </p>
    </div>

      <p>Max file size= 2mb</p>
      <p>Max number of files uploadable = 20</p>
      <p>*Important Note: When submitting photos, please be sure that you are the owner of the images, or that you have explicit permission from the owner to use them. All copyrights will be verified before winners are announced. </p>
    </form>


    </body>

</html>

这是处理过程:

<?php  
//current local
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
//locations/dirs
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '../images/PhotoContest/';
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.form.php';
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
$fieldname = 'file';
// upload

// possible upload errors (more?)
$errors = array(1 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');

// check the upload form was actually used. no fancy injection stuff
isset($_POST['submit'])
    or error('woah. you can\'t upload images without the form', $uploadForm);

// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

// check that the file was an HTTP upload, not a sneaky pete
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $uploadForm);

// validation... 
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
    or error('only image uploads are allowed', $uploadForm);

//name this file. If not, name by time (remove time function when name works right)
$now = date('m-d-Y');
$photogname = ($_POST['photogname']);
while(file_exists($uploadFilename = $uploadsDirectory.$photogname.$now.'-'.$_FILES[$fieldname]['name']))

{
    $now++;
}

// move the file to final dir (& check perm)
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $uploadForm);

// sucess
header('Location: ' . $uploadSuccess);

// error handler
function error($error, $location, $seconds = 5)
{
    header("Refresh: $seconds; URL=\"$location\"");
    echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
    '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
    '<html lang="en">'."\n".
    '   <head>'."\n".
    '       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
    '       <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
    '   <title>Upload error</title>'."\n\n".
    '   </head>'."\n\n".
    '   <body>'."\n\n".
    '   <div id="Upload">'."\n\n".
    '       <h1>Upload failure</h1>'."\n\n".
    '       <p>An error has occured: '."\n\n".
    '       <span class="red">' . $error . '...</span>'."\n\n".
    '       The upload form is reloading</p>'."\n\n".
    '    </div>'."\n\n".
    '</html>';
    exit;
} // end error handler

?>

【问题讨论】:

    标签: php forms uploader


    【解决方案1】:

    在您的文件input 标记的末尾有一个multiple 字样。我不知道那是什么,而且绝对不是关于 HTML 5 之类的,因为您的文档类型是关于版本 4。

    检查此代码:

    HTML:

    <input type="file" name="file[]" class="tag"/>
    <input type="file" name="file[]" class="tag"/>
    <input type="file" name="file[]" class="tag"/>
    

    PHP:

    foreach($_FILES as $file)
    {
    // check for standard uploading errors
    ($file['error'] == 0)
        or error($errors[$file['error']], $uploadForm);
    
    // check that the file was an HTTP upload, not a sneaky pete
    @is_uploaded_file($file['tmp_name'])
        or error('not an HTTP upload', $uploadForm);
    
    // validation... 
    // should run a check to make sure the upload is an image
    @getimagesize($file['tmp_name'])
        or error('only image uploads are allowed', $uploadForm);
    
    //name this file. If not, name by time (remove time function when name works right)
    $now = date('m-d-Y');
    $photogname = ($_POST['photogname']);
    while(file_exists($uploadFilename = $uploadsDirectory.$photogname.$now.'-'.$file['name']))
    
    {
        $now++;
    }
    
    // move the file to final dir (& check perm)
    @move_uploaded_file($file['tmp_name'], $uploadFilename)
        or error('receiving directory insuffiecient permission', $uploadForm);
    }
    

    【讨论】:

    • "multiple" 不应该在那里,它是从我最初复制字段的不同表单中遗留下来的。但这也不应该引起任何问题,所以我也不是很担心。我不完全理解如何在不完整的情况下将 PHP 实现到我的代码中;y 重写所有内容。我还在学习,抱歉。谢谢。
    • 您必须更改与上传文件相关的部分。检查我的编辑。
    • 衷心感谢您迄今为止的所有帮助,但它还没有工作。 PHP 工作正常(正确上传单个文件,正确命名文件),但输入导致它失败(没有上传文件)。
    • 欢迎您。 the inputs cause it to fail 是什么意思?首先,您在提供的 HTML 代码中有一个文件输入标签,并且您正在谈论多个文件上传。您为什么要使用该单个标签上传多个文件?并且您的原始 PHP 代码也已针对单个文件上传实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2015-10-23
    相关资源
    最近更新 更多