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


Move uploaded file fails after ajax request

我知道这个问题已经解决过几次了,但没有任何解决方案对我有效,

我有一个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实际上在Joomla3.4网站的JUMI应用程序中)。但是,脚本总是无法上传文件(页面返回"/images/restaurants/1/businesslogo.jpgNot uploaded because error#."(因为我也在返回错误字符串之前返回了target_path)。

有人知道这样做的原因以及如何正确上传脚本吗?我完全陷入了这个问题,因为在我看来,一切都应该可行。

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

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

作为

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

它现在工作:-)