file_put_contents():无法打开流

file_put_contents(): failed to open stream

本文关键字:put contents file      更新时间:2023-09-26

我正在字符串中创建网页上的拖放上载。我编写的代码使用Jquery打开文件,并使用XHR请求将数据发送到php文件。php文件然后会将文件移动到我指定的位置。当我将文件拖放到拖放区域时,我收到了一个200后的请求,我认为这意味着数据将被发送到我的php脚本,但随后我收到了以下错误:

file_put_contents(../../../media/SeagateHDD/Uploads/): failed to open stream

我相信这个错误是因为php找不到位置(../../../media/SeagateHDD/Uploads/),但我已经检查了十次,位置是正确的,没有拼写错误。我还检查了我的权限和所有者的位置,所有者"www data:www data"和权限"0775"甚至tred"0777"都是正确的,但什么都不起作用。如果有人能帮我修复说的人,我将非常伟大。

我的Javascript

$(function(){

var obj = $('#Upload_form');
obj.on('dragover', function(e){
    //prevent browser from open the file when drop off
    e.stopPropagation();
    e.preventDefault();
    $(this).css('border',"3px solid #000000");
});
obj.on('dragleave', function(e){
    //prevent browser from open the file when drop off
    e.stopPropagation();
    e.preventDefault();
    $(this).css('border',"3px dashed #4a4a4a");
});
obj.on('drop',function(e){
    //prevent browser from open the file when drop off
    e.stopPropagation();
    e.preventDefault();
    $(this).css('border',"3px dashed #4a4a4a");
    //retrieve uploaded files data
    var files = e.originalEvent.dataTransfer.files;
    var file = files[0];
    upload(file);
});
function upload(file){
    xhr = new XMLHttpRequest();
    //initiate Request
    xhr.open('POST','../PHP/Drop_Upload.php', true);
    //set headers
    xhr.setRequestHeader('Content-Type', 'multipart/form-data');
    xhr.setRequestHeader('X-File-Name',file.fileName);
    xhr.setRequestHeader('X-File-Size',file.fileSize);
    xhr.setRequestHeader('X-File-Type',file.fileType);
    //send the file
    xhr.send(file);
}
});

我的PHP

$string = file_get_contents('php://input');
$path = '../../../media/SeagateHDD/Uploads/';
if ( !file_exists($path) ) {
  mkdir ($path, 0777);
 }
if (file_put_contents($path,$string)==true){
    echo 'File Uploaded Successfully';
}else{
    echo 'File Failed to Upload';
}

$path中提供的是目录路径。它应该提供文件路径。类似$path='../..//media/SeagateHDD/Uploads/something.txt';

file_put_contents

您可以通过以下方式将相对路径转换为绝对路径:

$path = realpath('../../../media/SeagateHDD/Uploads/');

之后,您必须将文件名附加到要写入的位置,例如:

$path .= "text.txt";