PHP无法正常工作

时间:2013-12-19 09:08:36

标签: javascript php jquery ajax forms

我正在使用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;
?>

这是我的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>

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

<button class="btn btn-primary" onclick='submitForm();'>Save changes</button>

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

<button class="btn btn-primary" id="upload">Upload</button>

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

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

$("#save").click(function () {
    $("#submit").click();
});

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

$('#form').ajaxForm(function (data) {
    console.log(data);
});

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

那么问题是什么?如何解决问题。

感谢。

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

在你的代码中,你将move_uploaded_file逻辑放在最后的其他条件中。

if(){
  ............................
}
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);     
}

由于tmpdir在文件移动之前为空,因此控制转到此块:

  elseif(empty($tmpdir) || $tmpdir == 'none') {
        $errormsg = 'No file was uploaded..';
    } 

在理想情况下将文件移到那里后,应添加以上检查。

在最后一个条件文件被移动到tmpdir,所以在filemove之后检查该目录你会在那里找到它。