douci2015 2015-01-27 22:11
浏览 78

Ajax php文件上传返回undefined错误

I'm trying to upload a form which includes an image using jquery and php - the data is saved, but the image is not uploaded, and the ajax call runs the error function, not the success function.

Even though there is text echoed out of the php file, in jQuery it just seems to be an object who's .text value is undefined...

These are the error messages in the php error log:

[28-Jan-2015 12:56:32 Europe/Berlin] PHP Warning: move_uploaded_file(uploads/mail.jpeg): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/app/php/signuporganization.php on line 133 [28-Jan-2015 12:56:32 Europe/Berlin] PHP Warning: move_uploaded_file(): Unable to move '/Applications/MAMP/tmp/php/phpXQ1WoR' to 'uploads/mail.jpeg' in /Applications/MAMP/htdocs/app/php/signuporganization.php on line 133 [28-Jan-2015 14:35:29 Europe/Berlin] PHP Warning: move_uploaded_file(uploads/mail.jpeg): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/app/php/signuporganization.php on line 133 [28-Jan-2015 14:35:29 Europe/Berlin] PHP Warning: move_uploaded_file(): Unable to move '/Applications/MAMP/tmp/php/phpXKRDO4' to 'uploads/mail.jpeg' in /Applications/MAMP/htdocs/app/php/signuporganization.php on line 133 [28-Jan-2015 14:37:28 Europe/Berlin] PHP Warning: move_uploaded_file(uploads/mail.jpeg): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/app/php/signuporganization.php on line 133 [28-Jan-2015 14:37:28 Europe/Berlin] PHP Warning: move_uploaded_file(): Unable to move '/Applications/MAMP/tmp/php/phpuUFyx6' to 'uploads/mail.jpeg' in /Applications/MAMP/htdocs/app/php/signuporganization.php on line 133

I just can't work out what the problems are. Thanks in advance.

My jQuery code is:

$("#signUpOrganizationForm").submit(function(){
        //var organizationFormData = $(this).serialize();
        //data to be sent to server (serialize doesn't work for files, so instead we are manually created FormData. Alternatively you can use the Jquery Form Plugin.        
            var organizationFormData = new FormData();
        organizationFormData.append( "profilepic", $('#profilepic')[0].files[0]);
        organizationFormData.append( "orgName", $('#orgName').val());
            organizationFormData.append( "Username", $('#Username').val());
            organizationFormData.append( "Password", $('#Password').val());

        
        //e.preventDefault();
        
        $.ajax({
            url: "php/signuporganization.php", //Relative or absolute path to response.php file
            type: "POST",
            data: organizationFormData,
            contentType: false,
            processData: false,
            dataType:'json',
            success: function(data) {
            //  data = JSON.parse(data);
                console.log(data);
                alert('Hello we are here and data return = ' + data);
                localStorage.id = data;
                console.log('localStorage = ' + localStorage.id);
                $.mobile.changePage($('#home-organization'),'pop');
                },
            error: function(data, status, error) {
                //data = JSON.parse(data)
                alert('error:' + data.text);    
            }
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

And the php file is:

<?php

$profilepic = $_FILE[ 'profilepic' ];
$orgName = $_POST[ 'orgName' ];
$name = $_POST[ 'Username' ];
$password = $_POST[ 'Password' ];
$Description = $_POST[ 'Description' ];
$today = date("Ymd");

getPageData($orgName, $name, $password, $today, $Description);
uploadprofilepic($profilepic);

function connectToDB() {

    $mysqliLink = new mysqli('localhost', 'root', 'root', 'giving');

    if(mysqli_connect_errno()){
        echo('not connected');
        exit();
    } else {
    }
    
    return $mysqliLink;
}

function getPageData($orgName, $name, $password, $today, $Description) {
        $mysqliLink = connectToDB();
        $Id = '';
        $query1 = "INSERT INTO users (username,password,type) VALUES ('$name','$password', 'organization')";
        $query2 = "SELECT id FROM users WHERE username='$name'";
        $query3 = "INSERT INTO Organization (Id,Name,SignUpDate,Description,LastActive) VALUES ('$Id', '$orgName', '$today', '$Description','$today')";
        
        if ($result = $mysqliLink->query($query1)) {
                if ($result2 = $mysqliLink->query($query2)) {
                    
            /* fetch object array */
                     while ($obj = $result2->fetch_object()) {
                        $Id=$obj->id;                
                        //$Id =intval($Id);
                      }
                     if ($result3 = $mysqliLink->query($query3)) {
                           echo($Id);
        
        /* free result set */
        //$result->close();
        //$result2->close();
        //$result3->close();
                    }
                }
        }
        /* close connection */
        $mysqliLink->close();

        //$result = $mysqliLink->query("SELECT * FROM 'users' WHERE username='talsegel'");
        //while ($obj = $result->fetch_object()) {
       // echo($obj->password);
        //}
       // alert($DBpassword);


       //  echo('hi' + $obj->password);
    }
    
function uploadprofilepic($profilepic){
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["profilepic"]["name"]);
    echo($target_file);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
      $check = getimagesize($_FILES["profilepic"]["tmp_name"]);
      if($check !== false) {
        echo ("File is an image - " . $check["mime"] . ".");
        $uploadOk = 1;
      } else {
          echo ("File is not an image.");
          $uploadOk = 0;
      }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
      echo ("Sorry, file already exists.");
      $uploadOk = 0;
    }

    // Check file size
    if ($_FILES["profilepic"]["size"] > 500000) {
      echo ("Sorry, your file is too large.");
      $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
      echo ("Sorry, only JPG, JPEG, PNG & GIF files are allowed.");
      $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
      echo ("Sorry, your file was not uploaded.");
    // if everything is ok, try to upload file
    } else {
    if (move_uploaded_file($_FILES["profilepic"]["tmp_name"], $target_file)) {
          echo ("The file ". basename( $_FILES["profilepic"]["name"]). " has been uploaded.");
    } else {
        echo ("Sorry, there was an error uploading your file.");
    }
  }
 }


?>

</div>
  • 写回答

2条回答 默认 最新

  • dtx3006 2015-01-27 22:18
    关注

    I think it should be $_FILES not $_File in your php script on the first line where you are getting the profilepic.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了