set_include_path & get_include_path

本文介绍如何使用PHP的set_include_path函数动态地修改包含路径,从而更灵活地管理项目中的文件引用。通过简单的配置,可以避免因文件位置变化而进行大量代码修改的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

set_include_path(get_include_path() . PATH_SEPARATOR . $path); 是包含路径;

比如你有一个文件夹:命名为include,里面有
数据库连接文件:conn.php……,
你这样设置:set_include_path("/include")
那么以后你就直接可以在其他页面中使用
include("conn.php")


PATH_SEPARATOR 表示路径分隔符,linux下是冒号(:),windows下是分号(;)


set_include_path(get_include_path() . PATH_SEPARATOR .
__TYPECHO_ROOT_DIR__ . '/var' . PATH_SEPARATOR .
__TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__);
什意思呢,举个例子:
你的一个页面有这样的语句:
include('/inc/sql.php');
include('/inc/conn.php');

而你突然发现我把这些要包含的文件放在inc目录下不安全,怎么办,要改,我想放到include目录中,好的,这么多页面不累死才怪:有没有好的方法!有!!!!!!!

我在config.inc.php中写着么一句:
set_include_path(get_include_path() .'/include')就这么简单,对,就这么简单!动态的修改!
 
get_include_path()返回包含路径,不正确返回false;

#ifndef __QFTP_CLIENT_H__ #define __QFTP_CLIENT_H__ // make sure to add qftp depending on #include <QTimer> #include <QFile> #include <QUrl> #include <QMap> #include <QVariant> #include <QMutex> #include <QBuffer> #include <QSet> #include "qftp.h" struct RemoteDir; struct SingleCmd; class QFtpClient : public QFtp { Q_OBJECT public: enum QFTP_CMD { QC_NONE, QC_CD, QC_LIST, QC_RENAME, QC_PUT_FILE, QC_PUT_FILES, QC_PUT_TREE, QC_GET_BUFFER, QC_GET_FILE, QC_GET_FILES, QC_GET_TREE, QC_RM_FILE, QC_RM_TREE, QC_MKD, QC_MKDS, }; enum record_type { rt_success, rt_executing, rt_total, rt_count, }; public: QFtpClient(QString serverip = "", QString username = "", QString password = "", qint16 port = 21, QObject* parent = 0); ~QFtpClient(); void setIP(const QString& ip, const qint16 port = 21); void setUser(const QString& username, const QString& password); void close_host(); bool sync_connect(int msec = 3000); // 现存缺陷: 不能创建含中文的目录/文件 // 绝对路径操作接口 // 上传文件/目录 bool put_files(const QList< QPair<QString, QString> >& vfiles, int msec = 30000); //list< pir<localfile, remotefile> > bool put_file(const QString& localabsfile, const QString& remoteabsfile, int msec = 3000); bool put_tree(const QString& localabspath, const QString& remoteabspath, const QStringList& exceptabspath = {}, int msec = 30000); // 下载文件/目录 bool get_buffer(const QString& remoteabsfile, int msec = 3000); bool get_file(const QString& remoteabsfile, const QString& localabsfile, int msec = 3000); bool get_files(const QList< QPair<QString, QString> >& vfiles, int msec = 30000); //list< pir<remotefile, localfile> > bool get_tree(const QString& remoteabspath, const QString& localabspath, const QStringList& exceptabspath = {}, int msec = 30000); // 删除文件/目录 bool rm_file(const QString& remotefile, int msec = 3000); bool rm_tree(const QString& dir, int msec = 30000); // 新建目录 bool mk_dir(const QString& dir, int msec = 3000); bool mk_dir(const QStringList& dirlist, int msec = 3000); // 重命名 bool _rename(const QString& remotefilepath, const QString& newname, int msec = 3000); // 显示列表 bool _list(const QString& remotepath, int msec = 3000); // 当前目录操作接口 bool pwd_put_file(const QString& localabsfile, int msec = 3000); bool pwd_get_file(const QString& remotefile, const QUrl& localurl, int msec = 3000); bool pwd_rm_file(const QString& remotefile, int msec = 3000); bool pwd_rm_tree(const QString& remotedir, int msec = 10000); bool pwd_mk_dir(const QString& remotedir, int msec = 3000); bool pwd_rename(const QString& remotefile, const QString& newname, int msec = 3000); bool pwd_list(int msec = 3000); bool pwd_cd(const QString& dirname, bool done_list = false, int msec = 3000); // 是否被占用 bool is_busy()const; // 是否已连接 bool is_connect()const; QString pwd(); signals: void signal_state_changed(int); void signal_command_finish(int cmdid, QVariantList params, QString errorstring = ""); void signal_ftp_log(const QString&, bool error = false); void signal_list(const QList<QUrlInfo>&); void signal_get_buffer(const QByteArray&); void signal_done(int cmdid, int ok_num, int exec_num, int total_num); private: bool inner_put_file(const QString& localabsfile, const QString& remoteabsfile, int msec = 3000); bool inner_put_files(QVariantList& files, int msec = 30000); bool inner_put_tree(const QString& localabspath, const QString& remoteabspath, const QStringList& vExcept = {}, int msec = 30000); bool inner_get_buffer(const QString& remoteabsfile, int msec = 3000); bool inner_get_file(const QString& remoteabsfile, const QString& localabsfile, int msec = 3000); bool inner_get_files(QVariantList& files, int msec = 30000); bool inner_get_tree(const QString& remoteabspath, const QString& localabspath, const QStringList& vExcept = {}, int msec = 30000); bool inner_rm_file(const QString& remotefile, int msec = 3000); bool inner_rm_tree(const QString& dir, int msec = 30000); bool inner_mk_dir(const QString& dir, int msec = 3000); bool inner_mk_dir(const QStringList& dirlist, int msec = 3000); bool inner_rename(const QString& remotefilepath, const QString& newname, int msec = 3000); bool inner_list(const QString& remotepath = "", int msec = 3000); bool inner_cd(const QString& dirname, bool done_list = false, int msec = 3000); // done_list - 切换完成后, 是否需要list(用于ui显示) void connect_host(); void connect_result(QString errstr); void login_result(QString errstr); void close_result(QString errstr); void list_result(const SingleCmd& smd); void get_result(const SingleCmd& smd, QString errstr); bool has_cmd(); bool next_cmd(); bool next_cmd_util_done(int msec); void cd_result(const SingleCmd& smd, QString errstr); void put_result(const SingleCmd& smd, QString errstr); void mkd_result(const SingleCmd& smd, QString errstr); void rmd_result(const SingleCmd& smd, QString errstr); void rm_result(const SingleCmd& smd, QString errstr); void rename_result(const SingleCmd& smd, QString errstr); void build_pwd_cd_cmd(const QString& dirname, bool done_list = false); // 当前目录cd void build_cd_cmd(const QString& remotepath); // 绝对路径cd void build_cd_cmd(const QString& remotepath, QList<SingleCmd>& l); void build_mkdir_cmd(const QString& remotepath); void build_rmdir_cmd(const QString& remotepath); void build_rm_cmd(const QString& remotepath); void build_put_cmd(const QString& localfile, const QString& remotefile); void build_puts_cmd(const QVariantList&); void build_putree_cmd(const QString& localpath, const QString& remotepath, const QStringList& vExcept = {}); void build_get_buffer_cmd(const QString& remotefile); void build_get_cmd(const QString& remotefile, const QString& localfile); void build_gets_cmd(const QVariantList&); void build_getree_cmd(const QString& remotepath, const QString& localpath, const QStringList& vExcept = {}); void build_rename_cmd(const QString& remotefilepath, const QString& newname); void build_list_cmd(const QString& remotepath); void clear_cmd(); void reset_record(); void inc_record(record_type, int = 1); private slots: void slot_state_changed(int); void slot_timer_connect(); void slot_finish(int, bool); void slot_listinfo(const QUrlInfo&); void slot_inner_cmd(int cmdid, QVariantList params, QString errorstring); protected: QUrl m_url; QTimer* m_pConnect = Q_NULLPTR; QPair<QString, QString> m_CurrentPath; // local/remote path // download member QList< QPair<QString, QString> > m_PendingPath; // local/remote path QBuffer* m_buffer = 0; // rmdir member QList<SingleCmd> m_rmlist; // cmd member QList<SingleCmd> m_lCmd; QMap<int, SingleCmd> m_mapCmd; // list member QFtp::Command m_listcmd = QFtp::Command::None; QFtp::Command m_prevlistcmd = QFtp::Command::None; QList<QUrlInfo> m_listresult; QStringList m_PathSection = {}; QFTP_CMD m_cmd = QC_NONE; int success_count = 0, execute_count = 0, total_count = 0; QSet<QString> m_vExcept; }; #endif 这是QFtpclient.h
07-11
<think>我们有一个QFtpClient类,它封装了FTP操作。现在需要实现一个简单的上传功能:将本地生成的CSV文件上传到FTP服务器的指定路径。 我们将使用QFtpClient提供的put_file方法进行同步上传,并处理错误情况。 步骤: 1. 获取FTP配置(主机、用户名、密码、端口、远程路径) 2. 创建QFtpClient实例 3. 同步连接FTP服务器 4. 上传文件(使用绝对路径) 5. 处理上传结果 6. 清理临时文件 注意:由于QFtpClient的put_file是同步操作(有超时参数),我们可以直接调用。 以下代码示例:</think>以下是基于您提供的 `QFtpClient.h` 头文件的完整实现方案,我将展示如何高效地使用这个 FTP 客户端类进行文件上传: ```cpp #include "QFtpClient.h" #include "SysConfig.h" #include <QDir> #include <QFileInfo> #include <QtConcurrent> void CtKoe::slotfromplc_dischargePid(QString slotname, QString pid) { // 1. 获取面板数据并生成CSV AoiResultRowData rowData = getPanelData(pid); QString csvFile = makePanelCsv(rowData); if (csvFile.isEmpty()) { DISCHARGE_ERROR(tr("Failed to generate CSV for panel %1").arg(pid)); return; } // 2. 获取FTP配置 QString ftpHost = SysConfig::get("FTP_Host"); QString ftpUser = SysConfig::get("FTP_User"); QString ftpPass = SysConfig::get("FTP_Pass"); QString ftpPath = SysConfig::get("FTP_Path"); QString portStr = SysConfig::get("FTP_Port"); int ftpPort = portStr.isEmpty() ? 21 : portStr.toInt(); // 3. 验证配置 if (ftpHost.isEmpty() || ftpUser.isEmpty() || ftpPass.isEmpty() || ftpPath.isEmpty()) { DISCHARGE_ERROR(tr("FTP configuration incomplete, skip upload")); QFile::remove(csvFile); return; } // 4. 创建FTP客户端 QFtpClient* ftpClient = new QFtpClient(ftpHost, ftpUser, ftpPass, ftpPort); // 5. 连接信号槽处理上传结果 QObject::connect(ftpClient, &QFtpClient::signal_command_finish, [=](int cmdid, QVariantList params, QString error) { if (cmdid == QFtp::Put) { if (error.isEmpty()) { DISCHARGE_INFO(tr("CSV uploaded successfully")); } else { DISCHARGE_ERROR(tr("FTP upload failed: %1").arg(error)); } // 清理资源 QFile::remove(csvFile); ftpClient->deleteLater(); } } ); QObject::connect(ftpClient, &QFtpClient::signal_ftp_log, [](const QString& log, bool error) { if (error) { DISCHARGE_ERROR(log); } else { DISCHARGE_DEBUG(log); } } ); // 6. 构建远程路径并启动上传 QFileInfo fi(csvFile); QString remoteFile = QDir::cleanPath(ftpPath) + "/" + fi.fileName(); if (!ftpClient->put_file(csvFile, remoteFile)) { DISCHARGE_ERROR(tr("Failed to start FTP upload")); QFile::remove(csvFile); ftpClient->deleteLater(); } } ``` ### 代码详解: 1. **生成CSV文件**: ```cpp QString csvFile = makePanelCsv(rowData); ``` 调用业务逻辑生成CSV文件 2. **获取FTP配置**: ```cpp QString ftpHost = SysConfig::get("FTP_Host"); // ...其他配置... ``` 从系统配置中获取FTP连接参数 3. **配置验证**: ```cpp if (ftpHost.isEmpty() || ...) { DISCHARGE_ERROR(...); return; } ``` 确保所有必要的FTP参数都已配置 4. **创建FTP客户端**: ```cpp QFtpClient* ftpClient = new QFtpClient(ftpHost, ftpUser, ftpPass, ftpPort); ``` 动态创建FTP客户端对象,使用配置参数初始化 5. **信号连接**: ```cpp QObject::connect(ftpClient, &QFtpClient::signal_command_finish, [=](int cmdid, ...) { // 处理上传结果 } ); ``` 连接`signal_command_finish`信号处理上传结果: - 成功时记录日志 - 失败时记录错误信息 - 无论成功与否都删除临时文件 6. **日志处理**: ```cpp QObject::connect(ftpClient, &QFtpClient::signal_ftp_log, [](const QString& log, bool error) { // 转发FTP日志 } ); ``` 将FTP客户端的日志转发到系统日志 7. **文件上传**: ```cpp ftpClient->put_file(csvFile, remoteFile) ``` 调用`QFtpClient::put_file()`方法启动异步上传 ### 关键设计要点: 1. **异步操作**: - 利用`QFtpClient`的异步特性,不阻塞主线程 - 通过信号槽机制处理操作结果 2. **资源管理**: ```cpp ftpClient->deleteLater(); ``` 使用`deleteLater()`确保FTP客户端在事件循环中安全删除 3. **路径处理**: ```cpp QString remoteFile = QDir::cleanPath(ftpPath) + "/" + fi.fileName(); ``` 使用`QDir::cleanPath()`确保远程路径格式正确 4. **错误处理**: - 检查启动上传是否成功 - 在信号槽中处理上传结果 - 确保临时文件在任何情况下都会被删除 ### 同步版本实现: 如果需要同步操作(例如在后台线程中): ```cpp void uploadCsvFile(const QString& csvFile, const QString& ftpHost, const QString& ftpUser, const QString& ftpPass, const QString& ftpPath, int ftpPort) { QFtpClient ftpClient(ftpHost, ftpUser, ftpPass, ftpPort); // 同步连接(5秒超时) if (!ftpClient.sync_connect(5000)) { DISCHARGE_ERROR(tr("FTP connection failed")); return; } // 构建远程路径 QFileInfo fi(csvFile); QString remoteFile = QDir::cleanPath(ftpPath) + "/" + fi.fileName(); // 同步上传(30秒超时) if (ftpClient.put_file(csvFile, remoteFile, 30000)) { DISCHARGE_INFO(tr("CSV uploaded successfully")); } else { DISCHARGE_ERROR(tr("FTP upload failed")); } } void CtKoe::slotfromplc_dischargePid(QString slotname, QString pid) { // ...生成CSV文件... // 获取FTP配置... // 在后台线程执行同步上传 QtConcurrent::run([=]() { uploadCsvFile(csvFile, ftpHost, ftpUser, ftpPass, ftpPath, ftpPort); QFile::remove(csvFile); }); } ``` ### QFtpClient 使用最佳实践: 1. **连接管理**: - 每次操作创建新的QFtpClient实例 - 操作完成后及时销毁 - 避免长期保持连接 2. **错误处理**: - 监听`signal_command_finish`信号 - 检查QFtp::Command类型 - 使用`errorString()`获取详细错误 3. **路径规范**: - 使用`QDir::cleanPath()`规范化路径 - 避免路径中的特殊字符 - 处理路径分隔符差异(Windows/Linux) 4. **资源清理**: - 使用`deleteLater()`销毁QFtpClient - 在lambda捕获中管理临时文件 - 确保异常情况下的资源释放
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值