2010-09-12 92 views
8

誰能給我一個PHP文件上傳的鏈接/碼或短TUTS?我會linke上傳文件併發送文件名到數據庫。由於快速PHP文件上傳指南

+1

不是最人性化的教程有,但官方文檔有一個完整的例子:http://www.php.net/manual/en/features.file -upload.post-method.php – 2010-09-12 21:51:54

回答

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; 
       } 
      } 
     } 
    } 
} 

    ?>