我正在尝试使用client-compress.js库压缩此文件后发送该文件,并使用php捕获信息。但PHP说“未定义的文件”

时间:2019-02-10 20:34:07

标签: javascript php ajax

此代码用于压缩图像文件,并使用AJAX将其发送到PHP页面 我也不知道怎么了请问有人可以帮助我吗 解决此问题。

我想先在客户端上压缩图像

const options = {
  targetSize: 0.15,
  quality: 0.75,
  maxWidth: 1600,
  maxHeight: 1600
}
const compress = new Compress(options)
const preview = document.getElementById("preview")
const upload = document.getElementById("upload")
upload.addEventListener("change", (evt) => {
    const files = [...evt.target.files]
    compress.compress(files).then((conversions) => {
      const { photo, info } = conversions[0]

    var fd = new FormData();
    fd.append('file', photo);
    console.log('fd: ' + fd);
    $.ajax({
        url:'upload.php?name=' + photo.name,
        type:'post',
        data:fd,
        contentType: false,
        processData: false,
        success:function(response){
            if(response != 0){
                $(".preview img").attr('src', response);
                $('.preview img').show();
            }else{
                alert('File not uploaded');
            }
        }
    }); 
    })

  },
  false
)

此用于捕获文件并上传到目录     

/* Getting file name */
$filename = $_GET['name'];

/* Location */
$location = "upload/".$filename;
$uploadOk = 1;
$imageFileType = pathinfo($location,PATHINFO_EXTENSION);

/* Valid extensions */
$valid_extensions = array("jpg","jpeg","png");

/* Check file extension */
if(!in_array(strtolower($imageFileType), $valid_extensions)) {
   $uploadOk = 0;
}

if($uploadOk == 0){
   echo 0;
}else{
   /* Upload file */
   if(copy($_FILES['file']['tmp_name'],$location)){
     echo $location;
   }else{
     echo 0;
   }
}

0 个答案:

没有答案