简体   繁体   English

图片上传脚本错误(PHP)

[英]Error in image uploading script(php)

I have written a php script to upload an image,both the script and uploads folder are in my htdocs folder.This is my script: 我写了一个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>

I am getting error like: Warning: move_uploaded_file(../uploads/110221110100_96.png): failed to open stream: No such file or directory in /opt/lampp/htdocs/upload_image.php on line 27 我收到以下错误消息:警告:move_uploaded_file(../ uploads / 110221110100_96.png):无法打开流:第27行的/opt/lampp/htdocs/upload_image.php中没有此类文件或目录

Warning: move_uploaded_file(): Unable to move '/tmp/phph4iy1j' to '../uploads/110221110100_96.png' in /opt/lampp/htdocs/upload_image.php on line 27. 警告:move_uploaded_file():无法在第27行的/opt/lampp/htdocs/upload_image.php中将'/ tmp / phph4iy1j'移动到'../uploads/110221110100_96.png'。

but i have set all the required permissions.....Looking forward for helps,Thanks 但是我已经设置了所有必需的权限.....感谢您的帮助,谢谢

To rule out any confusion with relative paths I'd use an absolute path (ie /home/foo/project/uploads ) for move_updated_file() first. 为了排除与相对路径的任何混淆,我首先将绝对路径(即/home/foo/project/uploads )用于move_updated_file()

If that still fails try moving the uploaded file to something like /tmp/foo . 如果仍然失败,请尝试将上传的文件移动到/tmp/foo If that works then your problem is permissions based on the uploads folder you're trying to move the image to. 如果可行,那么您的问题是基于您要将图像移至的上载文件夹的权限。

You should make your upload directory absolute, like James said. 您应该像James一样将上传目录设为绝对目录。 Except I would probably go with the server document root way. 除了我可能会采用服务器文档的根目录方式。

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>';
}

This assumes $_SERVER['DOCUMENT_ROOT'] resolves to /opt/lampp/htdocs - I would assume so by looking at your path. 假设$_SERVER['DOCUMENT_ROOT']解析为/opt/lampp/htdocs我会通过查看您的路径来假设。

You can print_r($_SERVER['DOCUMENT_ROOT']) to see what the actual path is. 您可以使用print_r($_SERVER['DOCUMENT_ROOT'])查看实际路径。

Keep in mind, you may want to validate mime types and rename your files before dropping them in a public folder. 请记住,您可能需要验证MIME类型并重命名文件,然后再将它们放入公用文件夹。 As is, the code can leave you open to badness. 照原样,该代码可能会让您无所适从。 That's for another discussion though. 不过,这是另一个讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM