我想做一个页面有两个功能1.可以实现文件上传2.显示上传的文件
能再对应的给出下在的代码吗,下载上传文件中的内容
PHPä¸ä¼ æ件é常ç®åï¼ä½ éè¦ä¸ä¸ªä¸ä¼ çHTMLæ件ï¼<FORM>ï¼ï¼ä¸ä¸ªä¿åæ件çPHPæ件ï¼ä¿åï¼ï¼ä¸ä¸ªæ¥è¯¢ä¸ä¼ æ件æ¸
åçå·¥å
·ã
çæ£å®ç°å®æ´åè½çä¸ä¼ å管çéè¦ä½¿ç¨æ°æ®åºï¼æçç½ç«ä¿±ä¹é¨éé¢ä¸ä¼ æ件ä¸å¼æ¾å¾å¿«å°±ä¼ äºå ä¸ä¸ªæ件ã
æç®åçæ¹æ³ä¸ç¨æ°æ®åºï¼å¯ä»¥ç¨ä¸ä¸ªPHPæ件å®ç°ï¼è¿ä¸ªæ件ååºæå®ç®å½ä¸çæææ件ï¼ç¶åæä¾ä¸ä¸ªä¸ä¼ çFORMï¼æåæ£æµæ¯å¦æ交äºFORMæ°æ®ï¼æ¯å°±ä¿åæ件å°æå®ä½ç½®ã
PHPæå第39ç« æå ³äºå¤çè¿äºé®é¢ç说æï¼æå¤å¶ç²è´´å¦ä¸ï¼
ä¸ä¸é¡µ V. ç¹ç¹ ä¸ä¸é¡µ
--------------------------------------------------------------------------------
第 39 ç« æ件ä¸ä¼ å¤ç
ç®å½
POST æ¹æ³ä¸ä¼
é误信æ¯è¯´æ
常è§ç¼ºé·
ä¸ä¼ å¤ä¸ªæ件
对 PUT æ¹æ³çæ¯æ
POST æ¹æ³ä¸ä¼
æ¬ç¹æ§å¯ä»¥ä½¿ç¨æ·ä¸ä¼ ææ¬åäºè¿å¶æ件ãç¨ PHP ç认è¯åæ件æä½å½æ°ï¼å¯ä»¥å®å ¨æ§å¶å 许åªäºäººä¸ä¼ 以åæ件ä¸ä¼ åææ ·å¤çã
PHP è½å¤æ¥åä»»ä½æ¥èªç¬¦å RFC-1867 æ åçæµè§å¨ï¼å æ¬ Netscape Navigator 3 åæ´é«çæ¬ï¼æäºè¡¥ä¸ç Microsoft Internet Explorer 3 æè æ´é«çæ¬ï¼ä¸ä¼ çæ件ã
ç¸å ³ç设置: 请åé php.ini ç file_uploadsï¼upload_max_filesizeï¼upload_tmp_dirpost_max_size 以å max_input_time 设置é项ã
请注æ PHP ä¹æ¯æ PUT æ¹æ³çæ件ä¸ä¼ ï¼Netscape Composer å W3C ç Amaya 客æ·ç«¯ä½¿ç¨è¿ç§æ¹æ³ã请åé 对 PUT æ¹æ³çæ¯æ以è·åæ´å¤ä¿¡æ¯ã
ä¾ 39.1. æ件ä¸ä¼ 表å
å¯ä»¥å¦ä¸å»ºç«ä¸ä¸ªç¹æ®ç表åæ¥æ¯ææ件ä¸ä¼ ï¼
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
以ä¸èä¾ä¸ç __URL__ åºè¯¥è¢«æ¢æï¼æåä¸ä¸ªçå®ç PHP æ件ã
MAX_FILE_SIZE éèå段ï¼åä½ä¸ºåèï¼å¿ é¡»æ¾å¨æ件è¾å ¥å段ä¹åï¼å ¶å¼ä¸ºæ¥æ¶æ件çæ大尺寸ãè¿æ¯å¯¹æµè§å¨çä¸ä¸ªå»ºè®®ï¼PHP ä¹ä¼æ£æ¥æ¤é¡¹ãå¨æµè§å¨ç«¯å¯ä»¥ç®åç»è¿æ¤è®¾ç½®ï¼å æ¤ä¸è¦ææç¨æ¤ç¹æ§æ¥é»æ¡å¤§æ件ãå®é ä¸ï¼PHP 设置ä¸çä¸ä¼ æ件æ大å¼æ¯ä¸ä¼å¤±æçãä½æ¯æ好è¿æ¯å¨è¡¨åä¸å ä¸æ¤é¡¹ç®ï¼å 为å®å¯ä»¥é¿å ç¨æ·å¨è±æ¶é´çå¾ ä¸ä¼ 大æ件ä¹åæåç°æ件è¿å¤§ä¸ä¼ 失败ç麻ç¦ã
注æ: è¦ç¡®ä¿æ件ä¸ä¼ 表åçå±æ§æ¯ enctype="multipart/form-data"ï¼å¦åæ件ä¸ä¼ ä¸äºã
å ¨å±åé $_FILES èª PHP 4.1.0 èµ·åå¨ï¼å¨æ´æ©ççæ¬ä¸ç¨ $HTTP_POST_FILES æ¿ä»£ï¼ãæ¤æ°ç»å å«æææä¸ä¼ çæ件信æ¯ã
以ä¸èä¾ä¸ $_FILES æ°ç»çå 容å¦ä¸æ示ãæ们å设æ件ä¸ä¼ å段çå称å¦ä¸ä¾æ示ï¼ä¸º userfileãå称å¯éæå½åã
$_FILES['userfile']['name']
客æ·ç«¯æºå¨æ件çåå称ã
$_FILES['userfile']['type']
æ件ç MIME ç±»åï¼å¦ææµè§å¨æä¾æ¤ä¿¡æ¯çè¯ãä¸ä¸ªä¾åæ¯âimage/gifâãä¸è¿æ¤ MIME ç±»åå¨ PHP 端并ä¸æ£æ¥ï¼å æ¤ä¸è¦æ³å½ç¶è®¤ä¸ºæè¿ä¸ªå¼ã
$_FILES['userfile']['size']
å·²ä¸ä¼ æ件ç大å°ï¼åä½ä¸ºåèã
$_FILES['userfile']['tmp_name']
æ件被ä¸ä¼ åå¨æå¡ç«¯å¨åç临æ¶æ件åã
$_FILES['userfile']['error']
å该æ件ä¸ä¼ ç¸å ³çé误代ç ãæ¤é¡¹ç®æ¯å¨ PHP 4.2.0 çæ¬ä¸å¢å çã
æ件被ä¸ä¼ åï¼é»è®¤å°ä¼è¢«å¨åå°æå¡ç«¯çé»è®¤ä¸´æ¶ç®å½ä¸ï¼é¤é php.ini ä¸ç upload_tmp_dir è®¾ç½®ä¸ºå ¶å®çè·¯å¾ãæå¡ç«¯çé»è®¤ä¸´æ¶ç®å½å¯ä»¥éè¿æ´æ¹ PHP è¿è¡ç¯å¢çç¯å¢åé TMPDIR æ¥éæ°è®¾ç½®ï¼ä½æ¯å¨ PHP èæ¬å é¨éè¿è¿è¡ putenv() å½æ°æ¥è®¾ç½®æ¯ä¸èµ·ä½ç¨çã该ç¯å¢åéä¹å¯ä»¥ç¨æ¥ç¡®è®¤å ¶å®çæä½ä¹æ¯å¨ä¸ä¼ çæ件ä¸è¿è¡çã
ä¾ 39.2. 使æ件ä¸ä¼ çæ
请æ¥é å½æ° is_uploaded_file() å move_uploaded_file() 以è·åè¿ä¸æ¥çä¿¡æ¯ã以ä¸èä¾å¤çç±è¡¨åæä¾çæ件ä¸ä¼ ã
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
æ¥åä¸ä¼ æ件ç PHP èæ¬ä¸ºäºå³å®æ¥ä¸æ¥è¦å¯¹è¯¥æ件è¿è¡åªäºæä½ï¼åºè¯¥å®ç°ä»»ä½é»è¾ä¸å¿ è¦çæ£æ¥ãä¾å¦å¯ä»¥ç¨ $_FILES['userfile']['size'] åéæ¥æé¤è¿å¤§æè¿å°çæ件ï¼ä¹å¯ä»¥éè¿ $_FILES['userfile']['type'] åéæ¥æé¤æ件类ååæç§æ åä¸ç¸ç¬¦åçæ件ï¼ä½åªæè¿ä¸ªå½ä½ä¸ç³»åæ£æ¥ä¸ç第ä¸æ¥ï¼å 为æ¤å¼å®å ¨ç±å®¢æ·ç«¯æ§å¶èå¨ PHP 端并ä¸æ£æ¥ãèª PHP 4.2.0 èµ·ï¼è¿å¯ä»¥éè¿ $_FILES['userfile']['error'] åéæ¥æ ¹æ®ä¸åçé误代ç æ¥è®¡åä¸ä¸æ¥å¦ä½å¤çãä¸ç®¡ææ ·ï¼è¦ä¹å°è¯¥æ件ä»ä¸´æ¶ç®å½ä¸å é¤ï¼è¦ä¹å°å ¶ç§»å¨å°å ¶å®çå°æ¹ã
å¦æ表åä¸æ²¡æéæ©ä¸ä¼ çæ件ï¼å PHP åé $_FILES['userfile']['size'] çå¼å°ä¸º 0ï¼$_FILES['userfile']['tmp_name'] å°ä¸ºç©ºã
å¦æ该æ件没æ被移å¨å°å ¶å®å°æ¹ä¹æ²¡æ被æ¹åï¼å该æ件å°å¨è¡¨å请æ±ç»ææ¶è¢«å é¤ã
ä¾ 39.3. ä¸ä¼ ä¸ç»æ件
PHP ç HTML æ°ç»ç¹æ§çè³æ¯ææ件类åã
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
?>
--------------------------------------------------------------------------------
ä¸ä¸é¡µ ä¸ä¸çº§ ä¸ä¸é¡µ
38. å¤ç XForms èµ·å§é¡µ é误信æ¯è¯´æ
çæ£å®ç°å®æ´åè½çä¸ä¼ å管çéè¦ä½¿ç¨æ°æ®åºï¼æçç½ç«ä¿±ä¹é¨éé¢ä¸ä¼ æ件ä¸å¼æ¾å¾å¿«å°±ä¼ äºå ä¸ä¸ªæ件ã
æç®åçæ¹æ³ä¸ç¨æ°æ®åºï¼å¯ä»¥ç¨ä¸ä¸ªPHPæ件å®ç°ï¼è¿ä¸ªæ件ååºæå®ç®å½ä¸çæææ件ï¼ç¶åæä¾ä¸ä¸ªä¸ä¼ çFORMï¼æåæ£æµæ¯å¦æ交äºFORMæ°æ®ï¼æ¯å°±ä¿åæ件å°æå®ä½ç½®ã
PHPæå第39ç« æå ³äºå¤çè¿äºé®é¢ç说æï¼æå¤å¶ç²è´´å¦ä¸ï¼
ä¸ä¸é¡µ V. ç¹ç¹ ä¸ä¸é¡µ
--------------------------------------------------------------------------------
第 39 ç« æ件ä¸ä¼ å¤ç
ç®å½
POST æ¹æ³ä¸ä¼
é误信æ¯è¯´æ
常è§ç¼ºé·
ä¸ä¼ å¤ä¸ªæ件
对 PUT æ¹æ³çæ¯æ
POST æ¹æ³ä¸ä¼
æ¬ç¹æ§å¯ä»¥ä½¿ç¨æ·ä¸ä¼ ææ¬åäºè¿å¶æ件ãç¨ PHP ç认è¯åæ件æä½å½æ°ï¼å¯ä»¥å®å ¨æ§å¶å 许åªäºäººä¸ä¼ 以åæ件ä¸ä¼ åææ ·å¤çã
PHP è½å¤æ¥åä»»ä½æ¥èªç¬¦å RFC-1867 æ åçæµè§å¨ï¼å æ¬ Netscape Navigator 3 åæ´é«çæ¬ï¼æäºè¡¥ä¸ç Microsoft Internet Explorer 3 æè æ´é«çæ¬ï¼ä¸ä¼ çæ件ã
ç¸å ³ç设置: 请åé php.ini ç file_uploadsï¼upload_max_filesizeï¼upload_tmp_dirpost_max_size 以å max_input_time 设置é项ã
请注æ PHP ä¹æ¯æ PUT æ¹æ³çæ件ä¸ä¼ ï¼Netscape Composer å W3C ç Amaya 客æ·ç«¯ä½¿ç¨è¿ç§æ¹æ³ã请åé 对 PUT æ¹æ³çæ¯æ以è·åæ´å¤ä¿¡æ¯ã
ä¾ 39.1. æ件ä¸ä¼ 表å
å¯ä»¥å¦ä¸å»ºç«ä¸ä¸ªç¹æ®ç表åæ¥æ¯ææ件ä¸ä¼ ï¼
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
以ä¸èä¾ä¸ç __URL__ åºè¯¥è¢«æ¢æï¼æåä¸ä¸ªçå®ç PHP æ件ã
MAX_FILE_SIZE éèå段ï¼åä½ä¸ºåèï¼å¿ é¡»æ¾å¨æ件è¾å ¥å段ä¹åï¼å ¶å¼ä¸ºæ¥æ¶æ件çæ大尺寸ãè¿æ¯å¯¹æµè§å¨çä¸ä¸ªå»ºè®®ï¼PHP ä¹ä¼æ£æ¥æ¤é¡¹ãå¨æµè§å¨ç«¯å¯ä»¥ç®åç»è¿æ¤è®¾ç½®ï¼å æ¤ä¸è¦ææç¨æ¤ç¹æ§æ¥é»æ¡å¤§æ件ãå®é ä¸ï¼PHP 设置ä¸çä¸ä¼ æ件æ大å¼æ¯ä¸ä¼å¤±æçãä½æ¯æ好è¿æ¯å¨è¡¨åä¸å ä¸æ¤é¡¹ç®ï¼å 为å®å¯ä»¥é¿å ç¨æ·å¨è±æ¶é´çå¾ ä¸ä¼ 大æ件ä¹åæåç°æ件è¿å¤§ä¸ä¼ 失败ç麻ç¦ã
注æ: è¦ç¡®ä¿æ件ä¸ä¼ 表åçå±æ§æ¯ enctype="multipart/form-data"ï¼å¦åæ件ä¸ä¼ ä¸äºã
å ¨å±åé $_FILES èª PHP 4.1.0 èµ·åå¨ï¼å¨æ´æ©ççæ¬ä¸ç¨ $HTTP_POST_FILES æ¿ä»£ï¼ãæ¤æ°ç»å å«æææä¸ä¼ çæ件信æ¯ã
以ä¸èä¾ä¸ $_FILES æ°ç»çå 容å¦ä¸æ示ãæ们å设æ件ä¸ä¼ å段çå称å¦ä¸ä¾æ示ï¼ä¸º userfileãå称å¯éæå½åã
$_FILES['userfile']['name']
客æ·ç«¯æºå¨æ件çåå称ã
$_FILES['userfile']['type']
æ件ç MIME ç±»åï¼å¦ææµè§å¨æä¾æ¤ä¿¡æ¯çè¯ãä¸ä¸ªä¾åæ¯âimage/gifâãä¸è¿æ¤ MIME ç±»åå¨ PHP 端并ä¸æ£æ¥ï¼å æ¤ä¸è¦æ³å½ç¶è®¤ä¸ºæè¿ä¸ªå¼ã
$_FILES['userfile']['size']
å·²ä¸ä¼ æ件ç大å°ï¼åä½ä¸ºåèã
$_FILES['userfile']['tmp_name']
æ件被ä¸ä¼ åå¨æå¡ç«¯å¨åç临æ¶æ件åã
$_FILES['userfile']['error']
å该æ件ä¸ä¼ ç¸å ³çé误代ç ãæ¤é¡¹ç®æ¯å¨ PHP 4.2.0 çæ¬ä¸å¢å çã
æ件被ä¸ä¼ åï¼é»è®¤å°ä¼è¢«å¨åå°æå¡ç«¯çé»è®¤ä¸´æ¶ç®å½ä¸ï¼é¤é php.ini ä¸ç upload_tmp_dir è®¾ç½®ä¸ºå ¶å®çè·¯å¾ãæå¡ç«¯çé»è®¤ä¸´æ¶ç®å½å¯ä»¥éè¿æ´æ¹ PHP è¿è¡ç¯å¢çç¯å¢åé TMPDIR æ¥éæ°è®¾ç½®ï¼ä½æ¯å¨ PHP èæ¬å é¨éè¿è¿è¡ putenv() å½æ°æ¥è®¾ç½®æ¯ä¸èµ·ä½ç¨çã该ç¯å¢åéä¹å¯ä»¥ç¨æ¥ç¡®è®¤å ¶å®çæä½ä¹æ¯å¨ä¸ä¼ çæ件ä¸è¿è¡çã
ä¾ 39.2. 使æ件ä¸ä¼ çæ
请æ¥é å½æ° is_uploaded_file() å move_uploaded_file() 以è·åè¿ä¸æ¥çä¿¡æ¯ã以ä¸èä¾å¤çç±è¡¨åæä¾çæ件ä¸ä¼ ã
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
æ¥åä¸ä¼ æ件ç PHP èæ¬ä¸ºäºå³å®æ¥ä¸æ¥è¦å¯¹è¯¥æ件è¿è¡åªäºæä½ï¼åºè¯¥å®ç°ä»»ä½é»è¾ä¸å¿ è¦çæ£æ¥ãä¾å¦å¯ä»¥ç¨ $_FILES['userfile']['size'] åéæ¥æé¤è¿å¤§æè¿å°çæ件ï¼ä¹å¯ä»¥éè¿ $_FILES['userfile']['type'] åéæ¥æé¤æ件类ååæç§æ åä¸ç¸ç¬¦åçæ件ï¼ä½åªæè¿ä¸ªå½ä½ä¸ç³»åæ£æ¥ä¸ç第ä¸æ¥ï¼å 为æ¤å¼å®å ¨ç±å®¢æ·ç«¯æ§å¶èå¨ PHP 端并ä¸æ£æ¥ãèª PHP 4.2.0 èµ·ï¼è¿å¯ä»¥éè¿ $_FILES['userfile']['error'] åéæ¥æ ¹æ®ä¸åçé误代ç æ¥è®¡åä¸ä¸æ¥å¦ä½å¤çãä¸ç®¡ææ ·ï¼è¦ä¹å°è¯¥æ件ä»ä¸´æ¶ç®å½ä¸å é¤ï¼è¦ä¹å°å ¶ç§»å¨å°å ¶å®çå°æ¹ã
å¦æ表åä¸æ²¡æéæ©ä¸ä¼ çæ件ï¼å PHP åé $_FILES['userfile']['size'] çå¼å°ä¸º 0ï¼$_FILES['userfile']['tmp_name'] å°ä¸ºç©ºã
å¦æ该æ件没æ被移å¨å°å ¶å®å°æ¹ä¹æ²¡æ被æ¹åï¼å该æ件å°å¨è¡¨å请æ±ç»ææ¶è¢«å é¤ã
ä¾ 39.3. ä¸ä¼ ä¸ç»æ件
PHP ç HTML æ°ç»ç¹æ§çè³æ¯ææ件类åã
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
?>
--------------------------------------------------------------------------------
ä¸ä¸é¡µ ä¸ä¸çº§ ä¸ä¸é¡µ
38. å¤ç XForms èµ·å§é¡µ é误信æ¯è¯´æ
温馨提示:答案为网友推荐,仅供参考