1

我正在关注网上找到的关于上传图片的教程。由于我是新手,因此需要您的帮助才能更好地理解编码人员的想法。

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

我难以理解的是:这些键 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

?>
4

1 回答 1

1

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

以此为例:

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

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

如果是这样,我们继续前进。

如果不是,我们下拉到下一行。

error(args)是对稍后在代码中定义的函数 error 的调用。这个函数有 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']等于零。如果是,请继续,没有错误。如果它不等于 0,则运行第二行,它调用该error()函数,并传入带有一些索引(与抛出的错误有关)的 errors 数组和$uploadForm.

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

于 2013-08-31T04:30:44.293 回答