{"id":3040,"date":"2025-03-19T22:27:59","date_gmt":"2025-03-19T14:27:59","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3040"},"modified":"2025-03-19T22:27:59","modified_gmt":"2025-03-19T14:27:59","slug":"php-%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-%e6%96%87%e4%bb%b6%e5%a4%84%e7%90%86\/","title":{"rendered":"PHP \u6587\u4ef6\u5904\u7406"},"content":{"rendered":"\n<p>PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u5199\u5165\u3001\u5220\u9664\u6587\u4ef6\uff0c\u4ee5\u53ca\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u6587\u4ef6\u5b58\u50a8\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u7528\u6237\u4e0a\u4f20\u6587\u4ef6\u7b49\u4efb\u52a1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 \u6253\u5f00\u6587\u4ef6\uff1a<code>fopen()<\/code><\/strong><\/h2>\n\n\n\n<p><code>fopen()<\/code> \u51fd\u6570\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u6216 URL\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u6307\u9488\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$file = fopen(\"file.txt\", \"\u6a21\u5f0f\");<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6587\u4ef6\u8def\u5f84<\/strong>\uff1a\u6307\u5b9a\u8981\u6253\u5f00\u7684\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u5f0f<\/strong>\uff1a\u6587\u4ef6\u6253\u5f00\u7684\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6a21\u5f0f<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><code>\"r\"<\/code><\/td><td>\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u6307\u9488\u6307\u5411\u6587\u4ef6\u7684\u5f00\u5934\u3002<\/td><\/tr><tr><td><code>\"w\"<\/code><\/td><td>\u53ea\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u6307\u9488\u6307\u5411\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u6587\u4ef6\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a\u3002<\/td><\/tr><tr><td><code>\"a\"<\/code><\/td><td>\u4ee5\u8ffd\u52a0\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u6307\u9488\u6307\u5411\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/td><\/tr><tr><td><code>\"r+\"<\/code><\/td><td>\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u6307\u9488\u6307\u5411\u6587\u4ef6\u7684\u5f00\u5934\u3002<\/td><\/tr><tr><td><code>\"w+\"<\/code><\/td><td>\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u6307\u9488\u6307\u5411\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u6587\u4ef6\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a\u3002<\/td><\/tr><tr><td><code>\"a+\"<\/code><\/td><td>\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u6307\u9488\u6307\u5411\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$file = fopen(\"example.txt\", \"r\");  \/\/ \u6253\u5f00\u6587\u4ef6\u4ee5\u4f9b\u8bfb\u53d6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 \u8bfb\u53d6\u6587\u4ef6\uff1a<code>fread()<\/code> \u548c <code>fgets()<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 <code>fread()<\/code><\/strong><\/h3>\n\n\n\n<p><code>fread()<\/code> \u7528\u4e8e\u4ece\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u6216\u6307\u5b9a\u7684\u5b57\u8282\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$content = fread($file, filesize(\"example.txt\"));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 <code>fgets()<\/code><\/strong><\/h3>\n\n\n\n<p><code>fgets()<\/code> \u6bcf\u6b21\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4e00\u884c\u5185\u5bb9\uff0c\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26\u6216\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while ($line = fgets($file)) {\n    echo $line;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 \u5199\u5165\u6587\u4ef6\uff1a<code>fwrite()<\/code><\/strong><\/h2>\n\n\n\n<p><code>fwrite()<\/code> \u7528\u4e8e\u5411\u6587\u4ef6\u5199\u5165\u6570\u636e\u3002\u5982\u679c\u6587\u4ef6\u6ca1\u6709\u6253\u5f00\u8fdb\u884c\u5199\u5165\uff0c\u5b83\u4f1a\u8fd4\u56de <code>false<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$file = fopen(\"example.txt\", \"w\");\nfwrite($file, \"Hello, PHP!\");\nfclose($file);  \/\/ \u5173\u95ed\u6587\u4ef6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 \u6587\u4ef6\u672b\u5c3e\uff1a<code>feof()<\/code><\/strong><\/h2>\n\n\n\n<p><code>feof()<\/code> \u7528\u4e8e\u68c0\u67e5\u6587\u4ef6\u6307\u9488\u662f\u5426\u5df2\u5230\u8fbe\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (!feof($file)) {\n    $line = fgets($file);\n    echo $line;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5\ufe0f\u20e3 \u5173\u95ed\u6587\u4ef6\uff1a<code>fclose()<\/code><\/strong><\/h2>\n\n\n\n<p><code>fclose()<\/code> \u7528\u4e8e\u5173\u95ed\u6253\u5f00\u7684\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fclose($file);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6\ufe0f\u20e3 \u6587\u4ef6\u64cd\u4f5c\uff1a<code>file_exists()<\/code>, <code>unlink()<\/code>, <code>rename()<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>file_exists()<\/code><\/strong>\uff1a\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if (file_exists(\"example.txt\")) {\n    echo \"\u6587\u4ef6\u5b58\u5728\";\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>unlink()<\/code><\/strong>\uff1a\u5220\u9664\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if (unlink(\"example.txt\")) {\n    echo \"\u6587\u4ef6\u5df2\u5220\u9664\";\n} else {\n    echo \"\u6587\u4ef6\u5220\u9664\u5931\u8d25\";\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>rename()<\/code><\/strong>\uff1a\u91cd\u547d\u540d\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if (rename(\"oldname.txt\", \"newname.txt\")) {\n    echo \"\u6587\u4ef6\u91cd\u547d\u540d\u6210\u529f\";\n} else {\n    echo \"\u6587\u4ef6\u91cd\u547d\u540d\u5931\u8d25\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7\ufe0f\u20e3 \u6587\u4ef6\u4e0a\u4f20\uff1a<code>$_FILES<\/code><\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86 <code>$_FILES<\/code> \u8d85\u5168\u5c40\u6570\u7ec4\uff0c\u7528\u4e8e\u5904\u7406\u7528\u6237\u4e0a\u4f20\u7684\u6587\u4ef6\u3002\u6587\u4ef6\u4e0a\u4f20\u7684\u6d41\u7a0b\u5305\u62ec\u8bbe\u7f6e\u8868\u5355\u3001\u63a5\u6536\u6587\u4ef6\u3001\u4fdd\u5b58\u6587\u4ef6\u7b49\u6b65\u9aa4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u5904\u7406\u6587\u4ef6\u4e0a\u4f20<\/h3>\n\n\n\n<p><strong>HTML \u8868\u5355\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"upload.php\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n    Select file to upload:\n    &lt;input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\"&gt;\n    &lt;input type=\"submit\" value=\"Upload File\" name=\"submit\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p><strong>PHP \u5904\u7406\u4e0a\u4f20\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    $target_dir = \"uploads\/\";\n    $target_file = $target_dir . basename($_FILES&#91;\"fileToUpload\"]&#91;\"name\"]);\n\n    if (move_uploaded_file($_FILES&#91;\"fileToUpload\"]&#91;\"tmp_name\"], $target_file)) {\n        echo \"The file \" . basename($_FILES&#91;\"fileToUpload\"]&#91;\"name\"]) . \" has been uploaded.\";\n    } else {\n        echo \"Sorry, there was an error uploading your file.\";\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8\ufe0f\u20e3 \u6587\u4ef6\u548c\u76ee\u5f55\u6743\u9650\uff1a<code>chmod()<\/code> \u548c <code>chown()<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>chmod()<\/code><\/strong>\uff1a\u66f4\u6539\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u6743\u9650\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod(\"example.txt\", 0644);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>chown()<\/code><\/strong>\uff1a\u66f4\u6539\u6587\u4ef6\u7684\u6240\u6709\u8005\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>chown(\"example.txt\", \"user_name\");<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9\ufe0f\u20e3 \u76ee\u5f55\u64cd\u4f5c\uff1a<code>opendir()<\/code>, <code>readdir()<\/code>, <code>closedir()<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>opendir()<\/code><\/strong>\uff1a\u6253\u5f00\u76ee\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$dir = opendir(\"directory_path\");<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>readdir()<\/code><\/strong>\uff1a\u8bfb\u53d6\u76ee\u5f55\u4e2d\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>while (($file = readdir($dir)) !== false) {\n    echo \"\u6587\u4ef6\u540d\uff1a$file&lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>closedir()<\/code><\/strong>\uff1a\u5173\u95ed\u76ee\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>closedir($dir);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udd1f \u5c0f\u7ed3<\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u4ef6\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u6587\u4ef6\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u3001\u5220\u9664\u3001\u6743\u9650\u7ba1\u7406\u7b49\u64cd\u4f5c\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6587\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u6570\u636e\u5b58\u50a8\u7b49\u4efb\u52a1\u3002\u638c\u63e1\u8fd9\u4e9b\u51fd\u6570\u6709\u52a9\u4e8e\u63d0\u5347\u60a8\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u76f8\u5173\u94fe\u63a5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/ref.filesystem.php\">PHP \u6587\u4ef6\u5904\u7406\u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/features.file-upload.php\">PHP \u6587\u4ef6\u4e0a\u4f20\u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u5199\u5165\u3001\u5220\u9664\u6587\u4ef6\uff0c\u4ee5\u53ca\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\uff0c\u60a8\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3041,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3040"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3040\/revisions"}],"predecessor-version":[{"id":3042,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3040\/revisions\/3042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3041"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}