PHP文件上传问题 [英] PHP File Upload Issues

查看:170
本文介绍了PHP文件上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些PHP文件上传的帮助。我试图上传一个图片,使用下面的代码(在w3schools教程中提供):
$ b $ pre $ if((($ _FILES [file] [type] ==image / gif)
||($ _FILES [file] [type] ==image / jpeg)
||($ _FILES [file] [type] ==image / pjpeg))
&&($ _FILES [file] [size]< 20000))
{
if($ _FILES [file] [error]> 0)
{
echo返回码:。 $ _FILES [file] [error]。 < br />;
}
else
{
echoUpload:。 $ _FILES [file] [name]。 < br />;
回显类型:。 $ _FILES [file] [type]。 < br />;
回声大小:。 ($ _FILES [file] [size] / 1024)。 Kb< br />;
回显临时文件:。 $ _FILES [file] [tmp_name]。 < br />;
$ b $ if(file_exists(upload /。$ _FILES [file] [name]))
{
echo $ _FILES [file] [名称] 。 已经存在。 ;


$ $ b $ move_uploaded_file($ _ FILES [file] [tmp_name],
upload /。$ _FILES [file] [名称]);
回声存储在:。 上传/。 $ _FILES [ 文件] [ 名称];





echoInvalid file;



$ b显然我对代码做了一些修改,但是没有做它所说的应该做的事情。所以我问:


  1. 为什么这不会创建一个名为upload的新文件夹?我得到以下错误:
    警告:move_uploaded_file(上传/ donkeykong.jpg)[function.move-uploaded-file]:无法打开流:没有这样的文件或目录在...等等


  2. 我应该如何编写要上传图片的网址?

  3. $ b


解决方案

它不会创建文件夹,因为 mkdir() 永远不会被调用。该代码假定 upload / 目录已经存在。创建目录如果它不存在:

  if(!file_exists('upload')){
MKDIR( './上传');





$ b

另外,开头语句可以用数字的方式。一个可能是使用 in_array()而不是一堆 || 条件:

  if(in_array($ _ FILES ['file'] ['type'],array(image / gif,image / jpeg,image / pjpeg)
&&($ _FILES [file] [size]< 20000)

要将上传放在相对于服务器文档根目录的 upload / 中,可以使用 $ _SERVER ['DOCUMENT_ROOT'] 。但是,您应该手动创建 upload / 目录,并将其设置为可由Web服务器写入。通过PHP的目录,整个文档根需要由Web服务器写入,这是一个巨大的安全漏洞。

  $ uploads_dir = $ _SERVER ['DOCUMENT_ROOT']。/ upload /; 
move_uploaded_file($ _ FILES [file] [tmp_name],$ uploads_dir。$ _FILES [file] [ ]);


I'm looking for some help with PHP File Upload. I'm trying to upload an image, using the following code (provided in the w3schools tutorial):

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
 else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}
        }
          else
            {
           echo "Invalid file";
  }

Obviously I have some changes to make to their code, but it doesn't do what it says it's supposed to do as it is. So I'm asking:

  1. Why does this not make a new folder called 'upload' as it claims it will? I get the following error: Warning: move_uploaded_file(upload/donkeykong.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in ... etc.

  2. How should I write the URL where I want the image uploaded?

解决方案

It doesn't create a folder because mkdir() is never called. The code assumes the upload/ directory already exists. Create the directory if it doesn't exist with:

if (!file_exists('upload')) {
  mkdir('./upload');
}

As an aside, the statement at the beginning can be cleaned up in a number of ways. One possibilty is to use in_array() rather than a bunch of || conditions:

if (in_array($_FILES['file']['type'], array("image/gif", "image/jpeg", "image/pjpeg")
  && ($_FILES["file"]["size"] < 20000)
)

To place the uploads in upload/ relative to the server document root, you can use $_SERVER['DOCUMENT_ROOT']. However, you should create the upload/ directory manually and make it writable by the web server. In order to create the directory through PHP, the whole document root would need to be writable by the web server, which is a massive security flaw.

$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
move_uploaded_file($_FILES["file"]["tmp_name"], $uploads_dir . $_FILES["file"]["name"]);

这篇关于PHP文件上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆