PHP Array用于在文件上载时触发错误消息

时间:2013-08-31 04:01:53

标签: php

我正在关注上传图片的在线教程。因为我是新手,所以需要你的帮助让我更好地理解编码员的心态。

您会看到,有一个数组,其中的键和值已分配给可能的上传错误。我想知道这些错误将如何触发,因为我在代码的其他地方找不到关系。

我正在努力理解的是:这些键是1,2,3,......描述PHP上传错误的标准值还是仅仅是数字?

第二个问题,是否有另一种方法可以根据这种情况打印错误消息而不使用PHP函数并避免回显?

代码如下:

<?php  

// filename: upload.processor.php

// first let's set some variables

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

// make a note of the directory that will recieve the uploaded files
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';

// make a note of the location of the upload form in case we need it
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.form.php';

// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';

// name of the fieldname used for the file in the HTML form
$fieldname = 'file';

// Now let's deal with the upload

// possible PHP upload errors
$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 submitted else print form
isset($_POST['submit'])
    or error('the upload form is neaded', $uploadForm);

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

// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $uploadForm);

// validation... since this is an image upload script we 
// 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);

// make a unique filename for the uploaded file and check it is 
// not taken... if it is keep trying until we find a vacant one
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
    $now++;
}

// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $uploadForm);

// If you got this far, everything has worked and the file has been successfully saved.
// We are now going to redirect the client to the success page.
header('Location: ' . $uploadSuccess);

// make an error handler which will be used if the upload fails
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

?>

1 个答案:

答案 0 :(得分:1)

有一个现实,只是埋在代码的逻辑中。

以此为例:

($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

$_FILES[$fieldname]['error']将评估某种东西,在本例中是一个整数。 == 0是一个布尔逻辑检查; $_FILES[$fieldname]['error']的结果是否等于零?

如果确实如此,我们继续前进。

如果没有,我们将下拉到下一行。

error(args)是对函数的调用,错误,稍后在代码中定义。这个函数有3个参数,其中2个是我们在发生错误时传入的。

让我们打破这个: error($errors[$_FILES[$fieldname]['error']], $uploadForm),我们已经确定是对error()函数的调用。 $errors[$_FILES[$fieldname]['error']]是传递给该函数的第一个参数。 $uploadForm是传递的第二个参数。

让我们打破第一个:

如果我问你$errors[1]等于什么,你会说什么?那么[1]是数组errors的索引,在这种情况下,我要求第一个索引号或键值,即corse的值'php.ini max file size exceeded'

知道这一点,我们现在对$errors[$_FILES[$fieldname]['error']]的含义有了新的认识。如果你回忆一下我前面所说的,$_FILES[$fieldname]['error']将评估为某个整数值,并且因为它在[]内,那么该值是什么,成为我们的索引或关键值errors数组。

总之,我们有这段代码:

($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

在运行时,我们检查$_FILES[$fieldname]['error']是否等于零。如果是,继续前进,没有错误。如果它不等于零,则运行调用error()函数的第二行,并使用一些索引(与引发的错误有关)和{{1}的值传入errors数组。 }。

如果没有完全看到代码,或者知道项目的完整上下文,很难说天气或者不需要回声。我可以准确地告诉你这个脚本在做什么,如果这有帮助的话?如果没有错误,则会将用户重定向到上传成功页面。如果存在错误,即调用了error()函数,则此脚本会生成一个全新的页面,其中包含错误的详细信息。当然还有其他方法,但这很快捷。