PHP数组在文件上载时触发错误消息
我正在学习在线上传图片的教程。我是个新手,需要你帮助我更好地理解程序员的想法 你看,有一个数组,其中的键和值被分配给可能的上传错误。我想知道这些错误将如何触发,因为我在代码的其他地方找不到关系 我很难理解的是:这些键是1,2,3,。。。描述PHP上传错误的标准值,还是仅仅是数字 第二个问题,是否有另一种方法可以根据此场景打印错误消息,而不使用PHP函数并避免回声 代码如下:PHP数组在文件上载时触发错误消息,php,Php,我正在学习在线上传图片的教程。我是个新手,需要你帮助我更好地理解程序员的想法 你看,有一个数组,其中的键和值被分配给可能的上传错误。我想知道这些错误将如何触发,因为我在代码的其他地方找不到关系 我很难理解的是:这些键是1,2,3,。。。描述PHP上传错误的标准值,还是仅仅是数字 第二个问题,是否有另一种方法可以根据此场景打印错误消息,而不使用PHP函数并避免回声 代码如下: <?php // filename: upload.processor.php // first let's
<?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
?>
代码逻辑中隐藏着一种真实感
以此为例:
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
$\u FILES[$fieldname]['error']
的计算结果将是一个整数。
==0
是一个布尔逻辑检查;$\u文件[$fieldname]['error']
的结果是否等于零
如果是这样,我们就继续前进
如果没有,我们就转到下一行
error(args)
是对函数error的调用,该函数稍后在代码中定义。此函数接受3个参数,其中2个参数在发生错误时传入
让我们来分析一下:
error($errors[$\u FILES[$fieldname]['error']],$uploadForm)
,我们已经确定是对error()
函数的调用$errors[$\u FILES[$fieldname]['error']]
是传递给该函数的第一个参数$uploadForm
是传递的第二个参数
让我们来分解第一个:
如果我问你,$errors[1]
等于什么,你会怎么说?[1]
是数组errors
的索引,在本例中,我要求的是第一个索引号或键值,corse中的哪个是,'php.ini max file size excelled'
知道了这一点,我们现在对$errors[$\u FILES[$fieldname]['error']]]
的含义有了新的认识。如果您还记得我前面所说的,$\u文件[$fieldname]['error']
将计算为某个整数值,由于该整数值在[]
中,那么该值将成为errors
数组的索引或键值
总之,我们有以下代码块:
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
在运行时,我们检查$\u文件[$fieldname]['error']
是否等于零。如果是,请继续,没有错误。如果不等于零,则运行第二行,该行调用error()
函数,并将带有一些索引(与抛出的错误相关)和$uploadForm
值的errors数组传入
如果不完整地查看代码,或者不了解项目的完整上下文,很难说是否需要回音。我可以确切地告诉你这个脚本在做什么,但是,如果这有帮助的话?如果没有错误,它会将用户重定向到上载成功页面。如果出现错误,即调用了error()函数,此脚本将生成一个全新的页面,其中包含错误的详细信息。当然,还有其他方法可以做到这一点,但这很快也很简单。代码的逻辑中隐藏着一种真实感
以此为例:
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
$\u FILES[$fieldname]['error']
的计算结果将是一个整数。
==0
是一个布尔逻辑检查;$\u文件[$fieldname]['error']
的结果是否等于零
如果是这样,我们就继续前进
如果没有,我们就转到下一行
error(args)
是对函数error的调用,该函数稍后在代码中定义。此函数接受3个参数,其中2个参数在发生错误时传入
让我们来分析一下:
error($errors[$\u FILES[$fieldname]['error']],$uploadForm)
,我们已经确定是对error()
函数的调用$errors[$\u FILES[$fieldname]['error']]
是传递给该函数的第一个参数$uploadForm
是传递的第二个参数
让我们来分解第一个:
如果我问你,$errors[1]
等于什么,你会怎么说?[1]
是数组errors
的索引,在本例中,我要求的是第一个索引号或键值,corse中的哪个是,'php.ini max file size excelled'
知道了这一点,我们现在对$errors[$\u FILES[$fieldname]['error']]]
的含义有了新的认识。如果您还记得我前面所说的,$\u文件[$fieldname]['error']
将计算为某个整数值,由于该整数值在[]
中,那么该值将成为errors
数组的索引或键值
总之,我们有以下代码块:
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
在运行时,我们检查$\u文件[$fieldname]['error']
是否等于零。如果是,请继续,没有错误。如果不等于零,则运行第二行,该行调用error()
函数,并将带有一些索引(与抛出的错误相关)和$uploadForm
值的errors数组传入
如果不完整地查看代码,或者不了解项目的完整上下文,很难说是否需要回音。我可以确切地告诉你这个脚本在做什么,但是,如果这有帮助的话?如果没有错误,它会将用户重定向到上载成功页面。如果出现错误,即调用了error()函数,此脚本将生成一个全新的页面,其中包含错误的详细信息。当然还有其他的方法,但这很简单。谢谢。我想我开始消化得更好了。我也可以发布一个关于另一个问题的帖子吗?如果我想移除回音(更换