Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax不会将上载的文件移动到需要的位置_Php_Ajax - Fatal编程技术网

Php Ajax不会将上载的文件移动到需要的位置

Php Ajax不会将上载的文件移动到需要的位置,php,ajax,Php,Ajax,这个概念非常简单,我需要上传一个文件而不刷新页面,所以我使用的是ajax。我以前从来没有上传过ajax,所以我不确定这里缺少了什么 function uploadAttachment(){ var name = document.getElementById("file").files[0].name; var did = <? echo $did ?>; var form_data = new FormData(); var oFReader = n

这个概念非常简单,我需要上传一个文件而不刷新页面,所以我使用的是ajax。我以前从来没有上传过ajax,所以我不确定这里缺少了什么

function uploadAttachment(){
    var name = document.getElementById("file").files[0].name;
    var did = <? echo $did ?>;
    var form_data = new FormData();
    var oFReader = new FileReader();
    oFReader.readAsDataURL(document.getElementById("file").files[0]);
    var f = document.getElementById("file").files[0];
    var fsize = f.size||f.fileSize;
    if(fsize > 2000000)
    {
        alert("File Size is too large. Please make sure your file's size is less than 2MB.");
    }
    else
    {
        form_data.append("file", document.getElementById('file').files[0]);
        $.ajax
        ({
            url:"/assets/ajax/disciplinaries_upload.php?did="+did,
            method:"POST",
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            beforeSend:function()
            {
                $('#uploaded_file').html("<label class='text-success'>File Uploading...</label>");
            },   
            success:function(data)
            {
                $('#uploaded_file').html(data);
            }
        });
    }
};

所以从外观上看,它确实收到了文件,并且知道它需要去哪里。。。它实际上并没有上传到那里。

多亏了@Jay Blanchard和@kerbholz,代码现在可以工作了。我所要做的就是改变

 $location = '/assets/uploads/files/disciplinary_attachments/' . $name; 


看起来可能会发生此问题,因为上载文件大小限制和类型或目标写入路径不符合要求,或者通过http/https的文件上载被安全团队阻止

解决方案:

上传文件大小限制:默认情况下,web服务器可能允许上传高达1MB的文件。您需要根据您的要求更改名为
Max\u upload\u size
的属性

目标写入权限:有时我们可能忘记删除wright保护以上载目标文件夹。因此,请更改权限以禁用目标文件夹的保护

目标路径不令人满意:有时目标路径可能无法清除以进行更改。所以,最好提供完整的路径。 代码snip1:放在index.php中逻辑的顶部

代码snip2:在规程上传.php中更改此
$location

通过http/https上传文件:如果您的安全团队正在启用任何限制,请与他们一样启用。或者把自己列入白名单

最后:代码中的一个小改动

代码剪报:

<?php
//comment this two lines in production
error_reporting(E_ALL); 
ini_set('display_errors', 1);

//define("BASE_PATH", __DIR__); //if both index.php and disciplinaries_upload.php in same location, comment this else keep it in index.php 
$did = $_GET['did'];
if(count($_FILES) > 0)
{
     $test = explode('.', $_FILES["file"]["name"]);
     $fname = reset($test);;
     $ext = end($test);
     $name = $fname . '_' .$did. '.' . $ext;
     $location = BASE_PATH . '/assets/uploads/files/disciplinary_attachments';
     $permition_cod = substr(sprintf('%o', fileperms($location)), -4);
     @chmod($location, "0777");
     if (move_uploaded_file($_FILES["file"]["tmp_name"], $location.'/'.$name)) {
        echo '<label class="text-success">File Uploaded Successfully</label>';
    } else {
        echo '<label style="color: red">File Upload Failed</label><br> Original File Name: '.$_FILES["file"]["name"].'<br> Extension: '.$ext.'<br> Newly Generated Name: '.$name.'<br>File Location: '.$location;
    }
    @chmod($location, $permition_cod);
}
?>


祝你好运…

你检查过服务器的错误日志吗?当你说服务器的错误日志时,你是说通过inspect控制台?如果是这样,我看不到任何错误。服务器有一组错误日志,您应该能够检查这些日志。如果没有,请将以下内容添加到PHP脚本的顶部
error\u reporting(E\u ALL);ini设置(“显示错误”,1)好的,添加后我现在收到了:警告:move_uploads_file(/assets/uploads/files/Districtal_attachments/test_25.txt):无法打开流:在/usr/www/users/keepnxcanc/assets/ajax/Districtaries_upload.php第13行没有这样的文件或目录警告:move_uploads_file():无法将第13行的“/usr/home/keepnxcanc/.tmp/phpwL6Uhj”移动到/usr/www/users/keepnxcanc/assets/ajax/stricalies\u upload.php中的“/assets/uploads/files/discriptional\u attachments/test\u 25.txt”中,同样的信息会反映在错误日志中,因此您应该可以找到它们。检查文件夹的权限,以确保您可以对其进行写入。
 $location = '/assets/uploads/files/disciplinary_attachments/' . $name; 
 $location = '/usr/www/users/keepnxcanc/assets/uploads/files/disciplinary_attachments/' . $name; 
define("BASE_PATH", __DIR__);
$location = BASE_PATH . '/assets/uploads/files/disciplinary_attachments/' . $name;
<?php
//comment this two lines in production
error_reporting(E_ALL); 
ini_set('display_errors', 1);

//define("BASE_PATH", __DIR__); //if both index.php and disciplinaries_upload.php in same location, comment this else keep it in index.php 
$did = $_GET['did'];
if(count($_FILES) > 0)
{
     $test = explode('.', $_FILES["file"]["name"]);
     $fname = reset($test);;
     $ext = end($test);
     $name = $fname . '_' .$did. '.' . $ext;
     $location = BASE_PATH . '/assets/uploads/files/disciplinary_attachments';
     $permition_cod = substr(sprintf('%o', fileperms($location)), -4);
     @chmod($location, "0777");
     if (move_uploaded_file($_FILES["file"]["tmp_name"], $location.'/'.$name)) {
        echo '<label class="text-success">File Uploaded Successfully</label>';
    } else {
        echo '<label style="color: red">File Upload Failed</label><br> Original File Name: '.$_FILES["file"]["name"].'<br> Extension: '.$ext.'<br> Newly Generated Name: '.$name.'<br>File Location: '.$location;
    }
    @chmod($location, $permition_cod);
}
?>