0

i wanna upload files to cloud.there are 3 folders in cloud like P1,P2 and P3. I am uploading image(all formats), audio files, video files,ppt,pptx,doc,docx,pdf and text files. i wanna do these things before uploading.

  1. generate filenames as for jpeg image J1_YYYYMMDDHHMMSS_YYYYMMDDHHMMSS.jpg and for second jpeg img as J2_YYYYMMDDHHMMSS_YYYYMMDDHHMMSS.jpg and so on.

so same type of files start with respective letter and number which has to be unique and then the from time followed by end time. (for ppt starts with P, pptx as PX, doc as D, docx as DX, pdf as PF, text as T, audio as A, video as V)

and the first YYYYMMDDHHMMSS is starting period. and second YYYYMMDDHHMMSS is end time.

how will i do this while uploading in PHP.

1 Answers1

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

PHP creates array called $_FILES

Entry this array using name from form in this case $_FILES['var-trigger']

U have beside this second array after $_FILES['var-trigger'] :

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

use if statment and preg_match for type:

 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'] to avoid uploading 2 files it this same time

dir code from Count how many files in directory php

!is_upload_file line is for prevent malicious file using filname like /etc/

Community
  • 1
  • 1
fearis
  • 424
  • 3
  • 15