Tổng kho

Cho ý kiến chút nhé !

Bạn thấy trang này như thế nào?
Đẹp
Đơn điệu
Bình thường
Ý kiến khác

Thành viên trực tuyến

2 khách và 0 thành viên

Thống kê

  • truy cập   (chi tiết)
    trong hôm nay
  • lượt xem
    trong hôm nay
  • thành viên
  • 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:

    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

    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 />'
    ?>
    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.
    Nhắn tin cho tác giả
    Lê Khắc Thành Đạt @ 13:28 29/06/2011
    Số lượt xem: 554
    Số lượt thích: 0 người
     
    Gửi ý kiến