2010-09-12 36 views
8

Có ai cho tôi liên kết/mã hoặc tuts ngắn để tải lên tệp php không? Tôi sẽ linke để tải lên tập tin và gửi tên tập tin vào cơ sở dữ liệu. Cảm ơnHướng dẫn tải lên tệp php nhanh

+1

Không hướng dẫn người dùng thân thiện nhất có, nhưng các tài liệu chính thức có một ví dụ đầy đủ: http://www.php.net/manual/en/features.file -upload.post-method.php –

Trả lời

6

official docs, bao gồm cả nhận xét của người dùng ở phía dưới, rất tốt.

0
<?php 
if (isset($_FILES['file'])) 
{ 
    $file = $_FILES['file']; 

    // File Properties 
    $file_name = $file['name']; 
    $file_tmp = $file['tmp_name']; 
    $file_size = $file['size']; 
    $file_error = $file['error']; 

    // Work out the file extension 
    $file_ext = explode('.', $file_name); 
    $file_ext = strtolower(end($file_ext)); 

    $allowed = array('png', 'jpg'); 

    //filename 
    $id = 'uploads/Test'; 

    if (!file_exists($id)) 
    { 
     mkdir($id, 0777, true); 
    } 

    if (in_array($file_ext, $allowed)) { 
     if ($file_error === 0) { 
      if ($file_size <= 2097152) { 

       $file_name_new = uniqid('', true) . '.' . $file_ext; 
       $file_destination = $id .'/'. $file_name_new; 

       if (move_uploaded_file($file_tmp, $file_destination)) { 
        echo $file_destination; 
       } 
      } 
     } 
    } 
} 

    ?> 
Các vấn đề liên quan