【问题标题】:Move uploaded file fails after ajax requestajax请求后移动上传的文件失败
【发布时间】:2016-09-14 00:12:41
【问题描述】:

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

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

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 调用,该文件应该将该图像文件保存到运行上述函数的同一网络服务器上的目录中。

该文件中的 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 dump 显示文件错误变量的值为 0,并且报告文件大小与原始文件的大小顺序相同,并且具有 tmp 名称。所以换句话说,该文件存在于 TMP 目录中。

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

有谁知道这是什么原因以及如何让脚本正确上传?我完全陷入了这个问题,因为据我所知,一切都应该正常工作。

【问题讨论】:

  • 您是否可能正在尝试访问位于 Web 根目录之上的文件夹? open_basedir 问题?
  • 图片目录在网站根目录下(即网站的子目录)

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


【解决方案1】:

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

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

作为

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

它现在可以工作了:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2023-03-29
    • 2012-03-18
    相关资源
    最近更新 更多