PHP无法正常工作

Moh*_*qui 0 javascript php forms ajax jquery

我正在使用AJAX表单通过ajax在我的网站上传个人资料图片,但问题是我无法做到这一点.我已经创建了一个php页面,表单将通过ajax发布.看一看:

<?php
$file = $_FILES["file"];
$filename = $_FILES["file"]["name"];
$tempdir = $_FILES["file"]["tmp_name"];
$error = $_FILES["file"]["error"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];

$maxsize = 524288;
$allowedtypes = array("image/png", "image/jpg", "image/jpeg", "image/bmp");

$errormsg = "";
if(!empty($error))
{
    switch($error)
    {
        case '1':
            $errormsg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
            break;
        case '2':
            $errormsg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
            break;
        case '3':
            $errormsg = 'The uploaded file was only partially uploaded';
            break;
        case '4':
            $errormsg = 'No file was uploaded.';
            break;
        case '6':
            $errormsg = 'Missing a temporary folder';
            break;
        case '7':
            $errormsg = 'Failed to write file to disk';
            break;
        case '8':
            $errormsg = 'File upload stopped by extension';
            break;
        default:
            $errormsg = 'No error code avaiable';
    }
} elseif(empty($tmpdir) || $tmpdir == 'none') {
    $errormsg = 'No file was uploaded..';
} elseif(!in_array($type, $allowedtypes) || $size > $maxsize) {
    $errormsg = 'Either image type not supported or size is extending 512 KB';
} else {
    $filename = $_SESSION["username"];
    $filename_array = explode(".",$_POST["filename"]);
    $extension = array_pop($filename_array);
    $path = 'folder/' . Utilities::GetDefaultPicturePath() . $filename . "." . $extension;
    $location = $path . $filename; 
    move_uploaded_file($tmpdir, $location); 
    //for security reason, we force to remove all uploaded file
    @unlink($file);     
}

echo $errormsg;
?>
Run Code Online (Sandbox Code Playgroud)

这是我的AJAX:

<form action="admin/upload_profile_picture.php" method="post" class="hide" enctype="multipart/form-data" id="form">
    <input type="file" id="file" class="hide" name="file" />
    <input type="submit" name="submit" id="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

并且当表单被隐藏时,通过另一个按钮调用提交:

<button class="btn btn-primary" onclick='submitForm();'>Save changes</button>
Run Code Online (Sandbox Code Playgroud)

文件框通过以下方式打开:

<button class="btn btn-primary" id="upload">Upload</button>
Run Code Online (Sandbox Code Playgroud)

并通过此脚本调用这两个按钮的点击次数:

$("#upload").click(function () {
    $("#file").click();
});

$("#save").click(function () {
    $("#submit").click();
});
Run Code Online (Sandbox Code Playgroud)

最后,ajaxForm()jquery插件脚本在这里:

$('#form').ajaxForm(function (data) {
    console.log(data);
});
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在记录返回的数据.我总是提交表单是"没有文件上传..".正如您在我的PHP页面上看到的那样,只有当文件的临时目录为空时才会打印该错误设置为"无".

那么问题是什么,我该如何解决这个问题.

谢谢.

小智 5

您将结果分配给第4行中的$ tempdir,然后检查下面的$ tmpdir