ajax请求后移动上传的文件失败

时间:2016-05-17 18:33:36

标签: php ajax file-upload move-semantics form-data

我知道这个问题已经解决了几次,但没有解决方案适合我,

我有一个javascript函数,它可以提取一个引用的文件,如下所示

function imagePreload(str)
{
var timestamp = new Date().getTime();
str = str + "&timestamp=" + timestamp;
var key = [];
var value = [];
var queriesarray = str.split('&');
for(i = 0; i < queriesarray.length; i++)
{
    var pair = queriesarray[i].split('=');
    key[i]= pair[0];
    value[i]= pair[1];
}
for(i = 0; i < queriesarray.length; i++)
{
    if (key[i]=="menu_id") {var menuid = value[i];}
    if (key[i]=="menucategories_id") {var catid = value[i];}
}
for(i = 0; i < queriesarray.length; i++)
{
    if (value[i]=="business") {var fileurlfield = "uploadbizimageid";}
    if (value[i]=="category") {var fileurlfield = "uploadcatimageid" + catid;}
    if (value[i]=="item") {var fileurlfield = "uploaditemimageid" + menuid;}
}

var fileInput = document.getElementById(fileurlfield);

var file = fileInput.files[0];
var imageType = /image.*/;

if (file.type.match(imageType)) {
    var reader = new FileReader();
    reader.onload = function(e) {
        var img = new Image();
        img.src = reader.result;
    }
    reader.readAsDataURL(file); 

} else {
    alert("File not supported!");
}
document.getElementById("maskid").style.display = "block";
document.getElementById("imageuploadcontainerid").style.display = "block";

var filetosend = new FormData();
filetosend.append( 'image', file);

$.ajax({
        url: "index.php?option=com_jumi&fileid=13&format=raw&" + encodeURI(str),
        type: "POST",
        data: filetosend,
        processData: false,
        contentType: false,
        error: function (jqXHR, textStatus, errorThrown) {
            alert("AJAX error: " + textStatus + ' : ' + errorThrown);
        },
        success: function(html) {alert("Orwight!");
        document.getElementById('imageuploadcontainerid').innerHTML = html;
        }
});
}   

正如您所看到的那样,它旨在对一个php文件进行AJAX调用,该文件应该将该图像文件保存到运行上述函数的同一个Web服务器上的目录中。

该文件中的php看起来像这样。

$rest_id = $_GET['rest_id'];
$menu_id = $_GET['menu_id'];
$menucategories_id = $_GET['menucategories_id'];
$imagetype = $_GET['imagetype']; 

if($imagetype=="business")
{
    $db    = &JFactory::getDBO();
    $db->setQuery("SELECT * FROM g56s_restaurants WHERE rest_id = '$rest_id'");
    $det = $db->loadObject();
    $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    $target_path = "/images/restaurants/".$rest_id."/";
    $target_path = $target_path ."businesslogo.".$ext.""; 
    echo $target_path;

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {    
        echo "The file ".basename( $_FILES['image']['name'])." has been uploaded";
    } else { 
        echo "Not uploaded because of error #".$_FILES["file"]["error"];
    }
}

每当我调用他的脚本时,上传失败并且没有报告错误(即没有错误编号)。 var转储显示文件错误变量的值为0,并且报告的文件大小与原始文件的顺序相同,并且它具有tmp名称。换句话说,文件就在TMP目录中。

正在写入的目录中的目录权限是777.没有跨域问题(我猜没有CORS问题)因为脚本是从同一个网站调用的(PHP实际上是在Joomla 3.4中的JUMI应用程序中)网站)。但是,脚本总是无法上传文件(页面返回“/images/restaurants/1/businesslogo.jpg由于错误#。而未上传”。(因为我还在错误字符串回显之前回显了target_path)。

有谁知道这个的原因以及如何让脚本正确上传?我完全坚持这个问题,因为据我所知,一切都应该有效。

1 个答案:

答案 0 :(得分:1)

我比我想象的更快地解决了这个问题,事实证明我还必须在目标路径中指定文档根目录,所以我修改了

 $target_path = "/images/restaurants/".$rest_id."/";

作为

$target_path = $_SERVER['DOCUMENT_ROOT']."/images/restaurants/".$rest_id."/";

它现在有效: - )