PHP将上传的文件移动到目录

2024-09-29 00:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将python脚本中的文件发布到PHP,我想将它们存储在一个目录中。我正在扫描整个系统的.rtx文件,并将它们全部发送到PHP。 下面是我的python脚本

 url = 'http://localhost:9090/project/php/new.php'
    driveStr = subprocess.check_output("fsutil fsinfo drives")
    driveStr = driveStr.decode("utf-8")
    driveStr = driveStr.strip().lstrip('Drives: ')
    drives = driveStr.split()
    print(drives)
    mylist = []
    for drive in drives:
    
        if os.path.isdir(drive):
            p = os.walk(drive)
            for i , dirs , files in p :
                for filename in files :
                    if filename.endswith('rtx'):
    
                        file_path = pathlib.PurePath(i, filename)
                        print(file_path)
    
                        mylist.append(file_path)
                  

  upload_file = [
                    ("rtx_file" , (filename , open ( file_path , 'rb' ) , 'text/rtx'))
                ]
                response = requests.post ( url , files = upload_file )

下面是我的PHP脚本,我正在接收这些文件并将它们移动到目录中

    <?php


if(isset($_FILES['files']))
{
    $target_path = "C:\wamp64\www\project\php\upload";
    
    $target_path = $target_path . '/' . basename( $_FILES['files']['name']);
    if(move_uploaded_file($_FILES['files']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['files']['name'])." has been uploaded";
    }
    else{
        echo "There was an error uploading the file, please try again!";
    }
    

    
    
}
else
{
    echo "no file uploaded";
}

?>

我的问题是它没有将这些文件存储到指定的目录中。如果有人有什么想法,请告诉我怎么了。非常感谢。 我按照下面的链接来实施我的计划。 Python file upload to php Script


Tags: 文件path脚本targetiffilesfilenamefile
1条回答
网友
1楼 · 发布于 2024-09-29 00:23:08

在requests.you可以通过这个上传文件。下面是一个例子。您可以阅读更多关于requests upload

upload_file = [
    ("rtx_file",(filename,open(file_path,'rb'),'text/rtx'))
]
requests.post(url,files=upload_file)

如果您还有其他问题,请打印requests-response对象。如果可能,请在php中编写一些日志(不是echo,echo只将输出打印到客户端)

相关问题 更多 >