0

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

我有一个 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

?>
4

1 回答 1

0

文件标签multiple末尾有一个单词。input我不知道那是什么,而且绝对与 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);
}
于 2013-08-24T00:04:53.983 回答