【问题标题】:Where does move_uploaded_file store the file?move_uploaded_file 将文件存储在哪里?
【发布时间】:2013-07-25 23:17:18
【问题描述】:

我正在尝试通过网络表单上传文件。该脚本输出以下内容,并且没有错误。我仍然无法在服务器上找到目录。我指定的位置错误吗?我已经尝试过 /upload 以及在我的 ftp 客户端中看到的完整路径:/cygdrive/i/home/antor/dance/upload

   Upload: 1.jpg
    Type: image/jpeg
    Size: 107.9072265625 kB
    Stored in: C:\WINDOWS\Temp\php767.tmpUpload: 1.jpg
    Type: image/jpeg
    Size: 107.9072265625 kB
    Temp file: C:\WINDOWS\Temp\php767.tmpStored in: upload/1.jpgUpload: 1.jpg
    Type: image/jpeg
    Size: 107.9072265625 kB
    Temp file: C:\WINDOWS\Temp\php767.tmp
    Stored in: /cygdrive/i/home/antor/dance/upload/1.jpg 

这是我使用的代码:

    <form action="upload_file.php" method="post"
    enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
    </form>

和:

    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
      }
      $allowedExts = array("gif", "jpeg", "jpg", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 2000000)
    && in_array($extension, $allowedExts))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
        }
      }
    else
      {
      echo "Invalid file";
      }


      $allowedExts = array("gif", "jpeg", "jpg", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 2000000)
    && in_array($extension, $allowedExts))
      {
      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";
      }

    ?> 

文件夹上传在主uploader.php文件下面,有777权限。 知道文件存储在哪里吗?或者更好的是,我怎样才能把它放在正确的目录中。

--更新 update/ 文件夹似乎是正确的位置。文件仍然没有出现,我也在另一台服务器上尝试过。我在脚本中做错了吗?我可以做一些额外的错误检查吗?

.htaccess 与它有什么关系吗?在dance的上一层有一个drupal站点,里面有.htaccess:

#
# Apache/PHP/Drupal settings:
#

# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|svn-base)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$">
  Order allow,deny
</FilesMatch>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Force simple error message for requests for non-existent favicon.ico.
<Files favicon.ico>
  # There is no end quote below, for compatibility with Apache 1.3.
  ErrorDocument 404 "The requested file favicon.ico was not found.
</Files>

# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

# PHP 4, Apache 1.
<IfModule mod_php4.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 4, Apache 2.
<IfModule sapi_apache2.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  # Do not cache dynamically generated pages.
  ExpiresByType text/html A1
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

# $Id: .htaccess,v 1.90.2.3 2008/12/10 20:04:08 goba Exp $

【问题讨论】:

  • 您的代码将数据存储在 Temp 中,并将其移动到上传文件夹。我建议使用 755
  • 他们现在有 755 个权限,没有区别

标签: php file-upload


【解决方案1】:

如果文件夹在.php文件下面,那么

 move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);

正在尝试将其上传到与运行此文件的文件处于同一级别的“上传”目录。

试试

move_uploaded_file($_FILES["file"]["tmp_name"],
  "../upload/" . $_FILES["file"]["name"]);

【讨论】:

  • 这似乎是有道理的。然而,我试过了,仍然没有文件。我确实注意到权限跳回了 755,但由于我被列为所有者,我认为这没关系。有什么方法可以在移动过程中检查错误?
  • 我会在里面放一个名为 testfile.php 的文件,然后从那个脚本中 if (file_exists("upload/testfile.php")) {echo 'correct path';}else {echo ' Incorrect Path';} ,它将告诉您路径是否正确。如果是,那么您已经缩小了搜索范围 - 尝试 777 权限以及如果文件是由 apache 创建的,它可能不属于您,如果可行,那么您知道它的权限,您可以调整为更安全的权限和也可以让你写
  • 它说正确的路径。我无法将权限更改为 777,它们始终默认为 755,无法完全控制网络服务器。至少我知道它只是“上传/”,但是为什么文件没有出现?
  • 您是否检查过文件是否已上传,只是没有出现在 FTP 中?通过您的表单上传文件,然后使用我上面提到的相同方法检查该文件是否存在。通常它们不会立即出现在 FTP 中。
  • 试过了,文件不存在。
【解决方案2】:

您错过了表单的 enctype 属性。将此添加到您的表单标签

enctype='multipart/form-data'

【讨论】:

    【解决方案3】:

    创建一个info.php文件,将以下内容放入其中。

    <?php
    phpinfo();
    

    将其上传到您的上传脚本所在的服务器上,在浏览器中打开并找到:

    _SERVER["SCRIPT_FILENAME"]
    

    这将显示文件的完整路径,并有助于找出上传文件夹的路径。

    正确的路径是:

    move_uploaded_file($_FILES["file"]["tmp_name"], "I:\home\antor\dance\upload\\" . $_FILES["file"]["name"]);
    

    【讨论】:

    • 好的,输出“I:\home\antor\dance\upload\”,所以它看起来是一个windows的服务器。由于 \ 而不是 / move_uploaded_file($_FILES["file"]["tmp_name"], "I:\home\antor\dance\upload\\" . $_FILES["file "]["名称"]);
    • 也试过antor/dance/upload/,但没有成功。或许是别的地方出了差错,而且不是搬家?
    • 再添加一个斜线。正确的是: move_uploaded_file($_FILES["file"]["tmp_name"], "I:\home\antor\dance\upload\\" . $_FILES["file"]["name"]);跨度>
    • 谢谢,但我想这就是我写的
    • 复制/粘贴它,看看它是否有效。 \ 你最后有额外的斜线吗?
    【解决方案4】:

    假设你运行 http://somelink.com/app/upload.php 然后上传文件夹将仅在 /app/upload 下

    【讨论】:

      【解决方案5】:

      请务必在您的源目录或主目录中创建上传目录

      【讨论】:

      • 上传目录创建完毕,在我家/antor/dance下
      猜你喜欢
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2019-05-15
      • 2014-07-24
      相关资源
      最近更新 更多