简体   繁体   English

如何在php中将文件上传到目标文件夹

[英]how to upload files to destination folder in php

I am trying to upload files to uploads folder with this code: Upload multiple image using AJAX, PHP and jQuery 我正在尝试使用以下代码将文件上传到uploads文件夹: 使用AJAX,PHP和jQuery上传多个图像

In this post, what is the correct way to upload the files to uploads folder 在这篇文章中,将文件uploadsuploads文件夹的正确方法是什么

This is what i have but doesn't upload the files: 这是我有,但不上传文件:

if (isset($_POST["file"])) {
// do php stuff
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name']);
// call `json_encode` on `file` object
$file = json_encode($_POST["file"]);

// return `file` as `json` string
echo $file;
};

The php code is in the same file at the top where also the form and drop area is php代码位于顶部的同一文件中,表单和放置区域也是如此

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      // return json;
    } else {
        echo $_FILES['userfile']['error']; // only just for debuging
        // return json;
    }

your php code is coorect but you have to change in your ajax call 你的PHP代码是coorect但你必须改变你的ajax调用

var formData = new FormData($("#formId")); var formData = new FormData($(“#formId”));

  $.ajax({
        url:url,
        method : 'POST',
        dataType:'json',
        data: formData,
        mimeType: "multipart/form-data",
        contentType: false,
        processData: false,
        success:function(data){

} }); }};

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

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