0

我写了一个 php 脚本来上传图片,脚本和上传文件夹都在我的 htdocs 文件夹中。这是我的脚本:

<head>
<title>Upload an Image</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 0.21" />
<style type="text/css" title="text/css" media="all">
  .error {
      font-weight: bold;
      color:#Coo;
  }
</style>
    </head>

    <body>
<?php
     if($_SERVER['REQUEST_METHOD']=='POST') {
      if(isset($_FILES['upload'])) {
          $allowed = array('image/pjpeg','image/JPG','image/X-PNG','image/png','image/x-png');
          if(in_array($_FILES['upload']['type'],$allowed)) {
              if(move_uploaded_file($_FILES['upload']  ['tmp_name'],"../uploads/{_FILES['upload']['name']}")) {
                  echo '<p><em>The file has been uploaded!</em>  </p>';
              }
          }
          else {
              echo '<p class="error">Please uploaded a JPEG or PNG   image.</p>';
          }
      }
      if($_FILES['upload']['error'] > 0) {
          echo '<p class="error">The file could not be uploaded because: <strong></p>';
          switch ($_FILES['upload']['error']) {
              case 1:
                print 'The file exeeds the upload_max_filesize setting in php.ini';
                break;
              case 2:
                print 'The file exeeds the MAX_FILE_SIZE setting in the HTML form';
                break;
              case 3:
                print 'The file was only partially uploaded.';
                break;
              case 4:
                print 'No file was uploaded';
                break;
              case 6:
                print 'No tmp folder was available';
                break;
              case 7:
                print 'Unable to write on the disk';
                break;
              case 8:
                print 'File upload stopped';
                break;
              default:
                print 'A system error occured';
                break;
            }
            print '</strong>';

        }
            if(file_exists ($_FILES['upload']['tmp_name']) &&    is_file($_FILES['upload']['tmp_name']) ) {
                unlink ($_FILES['upload']['tmp_name']);
        }
    }
?>
<form enctype="multipart/form-data" action="upload_image.php" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="524288" />
  <fieldset><legend>Select a JPEG/PNG image of 512KB or smaller to be uploaded:</legend>
  <p><b>File:</b><input type="file" name="upload" /></p>

  </fieldset>
  <div align="center">
  <input type="submit" name="submit" value="Submit" />
  </div>
</form>
</body>
</html>

我收到如下错误:警告:move_uploaded_file(../uploads/110221110100_96.png):无法打开流:第 27 行的 /opt/lampp/htdocs/upload_image.php 中没有此类文件或目录

警告:move_uploaded_file(): Unable to move '/tmp/phph4iy1j' to '../uploads/110221110100_96.png' in /opt/lampp/htdocs/upload_image.php on line 27.

但我已经设置了所有必需的权限.....期待帮助,谢谢

4

2 回答 2

0

为了排除与相对路径的任何混淆,我首先使用绝对路径(即/home/foo/project/uploadsmove_updated_file()

如果仍然失败,请尝试将上传的文件移动到类似/tmp/foo. 如果可行,那么您的问题是基于您尝试将图像移动到的上传文件夹的权限。

于 2013-03-03T18:59:52.423 回答
0

你应该让你的上传目录绝对,就像詹姆斯说的那样。除了我可能会使用服务器文档根方式。

if(move_uploaded_file($_FILES['upload'] ['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['upload']['name'])) {
    echo '<p><em>The file has been uploaded!</em>  </p>';
}

这假设$_SERVER['DOCUMENT_ROOT']解析为/opt/lampp/htdocs- 我会通过查看您的路径来假设。

您可以print_r($_SERVER['DOCUMENT_ROOT'])查看实际路径是什么。

请记住,在将文件放入公共文件夹之前,您可能需要验证 mime 类型并重命名文件。照原样,代码可能会让您面临坏事。这是另一个讨论。

于 2013-03-04T06:38:47.423 回答