图像上传脚本(php)出错

时间:2013-03-03 18:54:46

标签: php

我写了一个php脚本来上传图片,脚本和uploads文件夹都在我的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():无法在第27行的/opt/lampp/htdocs/upload_image.php中将'/ tmp / phph4iy1j'移动到'../uploads/110221110100_96.png'。

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

2 个答案:

答案 0 :(得分:0)

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

如果仍然失败,请尝试将上传的文件移至/tmp/foo之类的内容。如果可以,那么您的问题是基于您尝试将图片移动到的上传文件夹的权限。

答案 1 :(得分: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类型并重命名文件。因此,代码可以让你对恶劣开放。这是另一个讨论。