我正在按照在线上找到的上传图片的教程。由于我是新手,所以需要你们的帮助让我理解编码人员更好一些。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
?>
谢谢你。我想我开始消化得更好了。我是否也可以发布另一个问题?如果我想删除函数内的回声(替换为某个变量),但要打印错误消息,因为您如何建议我更改该函数?也许你可以添加和编辑你的优秀解释。再次感谢 –
嗯,从技术上说,现在这个功能的方式是“正确的”,在我看来,这是处理事情的一种很好的方式。它向用户发送格式正确的HTML页面,并通知用户的浏览器在5秒内自动刷新,以便再次尝试重新加载。此外,当前正在回显的这个HTML块包含一个到CSS文件的链接。我将提供这个更简单的解决方案,然而,这是一个免责声明,但它是2013年,这不是最“正确”的做事方式。它仍然使用echo语句,但没有HTML块。只需'echo $ error;'离开标题调用。 – lampwins
你可以看看这篇文章,以及它的解决方案http://stackoverflow.com/questions/18429174/printing-validation-message-on-the-same-screen-without-redirecting-in-php?rq=1它似乎与您正在使用的脚本类似。 – lampwins