2014-03-28 15 views
0

我想上傳文件到cloud.there中有3個文件夾在雲,如P1,P2和P3。 我上傳圖片(所有格式),音頻文件,視頻文件,ppt,pptx,doc,docx,pdf和文本文件。我想在上傳前做這些事情。添加用戶輸入的時間到文件名並上傳到特定的文件夾使用php

  1. 生成的文件名作爲 爲JPEG圖像J1_YYYYMMDDHHMMSS_YYYYMMDDHHMMSS.jpg和用於第二JPEG IMG作爲J2_YYYYMMDDHHMMSS_YYYYMMDDHHMMSS.jpg等。

所以相同類型的文件的開始與相應的字母並且必須是唯一的號碼,然後從時間,隨後結束時間。 (對於ppt以P開頭,pptx爲PX,doc爲D,docx爲DX,pdf爲PF,文本爲T,音頻爲A,視頻爲V)

並且第一個YYYYMMDDHHMMSS是開始時間段。 和第二個YYYYMMDDHHMMSS是結束時間。

如何在PHP中上傳時執行此操作。

回答

0
<form action="send.php" enctype="multipart/form-data"><input type="file" name="var-trigger"></form> 

PHP創建陣列稱爲$ _FILES

條目在這種情況下$ _FILES [ '無功觸發']

使用名稱從形式這個排列U具有$ _FILES [在此之後第二陣列旁'VAR-觸發']:

$_FILES['var-trigger']['tmp_name'] 
$_FILES['var-trigger']['name'] 
$_FILES['var-trigger']['size'] 
$_FILES['var-trigger']['type'] //text/plain, image/png et cetera 

使用如果statment和的preg_match類型:

if (preg_match('/^image\/gif$/i', $_FILES['var-trigger']['type'])) { $ext='gif';} 
else if (preg_match('/^image\/(x-)?png$/i', $_FILES['var-trigger']['type'])) { $ext='png';} 

if ($ext=='png') {$file_title= 'P'; } 
else if ($ext=='gif') {$file_title= 'G'; } 

<?php 
// integer starts at 0 before counting 
$i = 0; 
$dir = $ext; 
if ($handle = opendir($dir)) { 
    while (($file = readdir($handle)) !== false){ 
     if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
      $i++; 
    } 
} 
// prints out how many were in the directory 
echo "There were $i files"; 
?> 

$filename = 'direction'. $file_title . $i . time() . $SERVER['REMOTE_ADDR'] . $ext; 

if(!is_uploaded_file($_FILES['var-trigger']['tmp_name']) or !copy($_FILES['var-trigger']['tmp_name'],$filename)) { echo 'error'; exit(); } 

$ SERVER [ 'REMOTE_ADDR'],以避免Count how many files in directory php

上傳2個文件就在同一時期

目錄代碼!is_upload_file線是防止惡意文件使用FILNAME喜歡的/ etc/

相關問題