在JavaScript中使用PHP不能上传文件 [英] cannot upload a file in javascript using php

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

问题描述

我想上传使用PHP文件。但会产生以下错误。 错误:文件上传过程中出现的问题。我使用的Ubuntu操作系统。我认为,试图保存文件时产生的错误。 我用下面的code

I am trying to upload a file using php. But the following error is generated. Error: A problem occurred during file upload! . I am using ubuntu OS. I think the error is generated while trying to save that file. I used the following code

<html>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    Choose a file to upload: <input name="uploaded_file" type="file" />
    <input type="submit" value="Upload" />
  </form>
</body>
</html>


<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

这个又是什么code中的问题?或者是因为这个文件permision访问该文件夹?

what is the problem of this code? or is this because of file permision to access the folder?

推荐答案

查找,其中发生错误。

<?php

$msg = '';
$upload_key = 'uploaded_file';

if (isset($_FILES[$upload_key])) {

    try {

        $error = $_FILES[$upload_key]['error'];
        switch ($error) {
            case UPLOAD_ERR_INI_SIZE:
                throw new Exception('Exceeded upload_max_filesize');
            case UPLOAD_ERR_FORM_SIZE:
                throw new Exception('Exceeded MAX_FILE_SIZE');
            case UPLOAD_ERR_PARTIAL:
                throw new Exception('Incomplete file uploaded');
            case UPLOAD_ERR_NO_FILE:
                throw new Exception('No file uploaded');
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new Exception('No tmp directory');
            case UPLOAD_ERR_CANT_WRITE:
                throw new Exception('Can\'t write data');
            case UPLOAD_ERR_EXTENSION:
                throw new Exception('Extension error');
        }

        $finfo    = new finfo(FILEINFO_MIME);
        $name     = $_FILES[$upload_key]['name'];
        $tmp_name = $_FILES[$upload_key]['tmp_name'];
        $size     = $_FILES[$upload_key]['size'];

        if ($size > 350000)
            throw new Exception('Exceeded 350KB limit');
        if (!is_uploaded_file($tmp_name))
            throw new Exception('Not an uploaded file');

        $type = $finfo->file($tmp_name);

        if ($type === false)
            throw new Exception('Failed to get MimeType');
        if ($type !== 'image/jpeg; charset=binary')
            throw new Exception('Only JPEG files available');

        $new_name = dirname(__FILE__).'/upload/'.$name;

        if (is_file($new_name))
            throw new Exception("The file {$new_name} already exists");

        if (!move_uploaded_file($tmp_name,$new_name))
            throw new Exception('Failed to move uploaded file');

        $msg = "File successfully uploaded as {$new_name}";

    } catch (Exception $e) {

        $msg = 'Error: '.$e->getMessage();

    }

}
?>
<!DOCTYPE html>
<html>
<head>
<title>Uploader</title>
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<div><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /></div>
<div>Choose a file to upload: <input name="uploaded_file" type="file" /></div>
<div><input type="submit" value="Upload" /></div>
</form>
<p>
<?php echo $msg.PHP_EOL; ?>
</p>
</body>
</html>

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

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