2013-08-31 26 views
1

我正在按照在线上找到的上传图片的教程。由于我是新手,所以需要你们的帮助让我理解编码人员更好一些。PHP Array在文件上传时触发错误消息

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

我在努力理解的地方是:这些键是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

有一个维吾尔,只是埋在代码的逻辑。

借此为的exaple:

($_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']等于零。如果是,继续前进,没有错误。如果它不等于零,则运行第二行,该行调用error()函数,并传入一些索引(与抛出的错误有关)和值$uploadForm的错误数组。

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

+0

谢谢你。我想我开始消化得更好了。我是否也可以发布另一个问题?如果我想删除函数内的回声​​(替换为某个变量),但要打印错误消息,因为您如何建议我更改该函数?也许你可以添加和编辑你的优秀解释。再次感谢 –

+0

嗯,从技术上说,现在这个功能的方式是“正确的”,在我看来,这是处理事情的一种很好的方式。它向用户发送格式正确的HTML页面,并通知用户的浏览器在5秒内自动刷新,以便再次尝试重新加载。此外,当前正在回显的这个HTML块包含一个到CSS文件的链接。我将提供这个更简单的解决方案,然而,这是一个免责声明,但它是2013年,这不是最“正确”的做事方式。它仍然使用echo语句,但没有HTML块。只需'echo $ error;'离开标题调用。 – lampwins

+0

你可以看看这篇文章,以及它的解决方案http://stackoverflow.com/questions/18429174/printing-validation-message-on-the-same-screen-without-redirecting-in-php?rq=1它似乎与您正在使用的脚本类似。 – lampwins