Gốc > Thế giới Tin học > Lập trình > PHP >
[PHP] Viết 1 trang Upload File
B1. Bạn viết 1 trang HTML có nội dung như sau:
B2. Bạn viết 1 trang upload.php:
Chú ý ở trên: dòng $upfile = '/uploads/'.$userfile_name; là thư mục các file upload được lưu vào, bạn có thể thay đổi nó tùy theo ý mình.
Lê Khắc Thành Đạt @ 13:28 29/06/2011
Số lượt xem: 554
PHP Code:
<html>
<head>
<title>Upload</title>
</head>
<body>
<h1>Upload</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> File:
<input name="userfile" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
<head>
<title>Upload</title>
</head>
<body>
<h1>Upload</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> File:
<input name="userfile" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
B2. Bạn viết 1 trang upload.php:
PHP Code:
<?php
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
if ($userfile_error > 0) {
echo 'Problem: ';
switch ($userfile_error)
{ case 1:
echo 'File exceeded upload_max_filesize';
break;
case 2:
echo 'File exceeded max_file_size';
break;
case 3:
echo 'File only partially uploaded';
break;
case 4:
echo 'No file uploaded';
break;
}
exit;
}
// put the file where we'd like it
$upfile = '/uploads/'.$userfile_name;
// is_uploaded_file and move_uploaded_file
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
} else {
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
exit;
}
echo 'File uploaded successfully<br /><br />';
// show what was uploaded
echo 'Preview of uploaded file contents:<br /><hr />';
echo $contents;
echo '<br /><hr />';
?>
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
if ($userfile_error > 0) {
echo 'Problem: ';
switch ($userfile_error)
{ case 1:
echo 'File exceeded upload_max_filesize';
break;
case 2:
echo 'File exceeded max_file_size';
break;
case 3:
echo 'File only partially uploaded';
break;
case 4:
echo 'No file uploaded';
break;
}
exit;
}
// put the file where we'd like it
$upfile = '/uploads/'.$userfile_name;
// is_uploaded_file and move_uploaded_file
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
} else {
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
exit;
}
echo 'File uploaded successfully<br /><br />';
// show what was uploaded
echo 'Preview of uploaded file contents:<br /><hr />';
echo $contents;
echo '<br /><hr />';
?>
Lê Khắc Thành Đạt @ 13:28 29/06/2011
Số lượt xem: 554
Số lượt thích:
0 người
 
- [PHP] Tự tạo bộ đếm cho website của bạn (29/06/11)
- [PHP] Tìm kiếm trong DB. (29/06/11)
- [PHP] Tạo cho mình 1 form mail liên hệ trên web. (29/06/11)
- [PHP] Tạo 1 mini chat đơn giản. (29/06/11)
- [PHP] Tạo 1 cái Shoutbox đơn giản (29/06/11)
Các ý kiến mới nhất