0 like 0 dislike
34 views
in Education & Reference by (2.9k points) | 34 views

1 Answer

1 like 0 dislike
Best answer

In functions.php

function imgUpload($file_id, $folder="", $types="") {
    if(!$_FILES[$file_id]['name']) return array('','No file specified');

    $file_title = $_FILES[$file_id]['name'];
    //Get file extension
    //$ext_arr = split("\.",basename($file_title));
    $ext_arr = preg_match("/\\.([^\\.]*)$/",$file_title,$exts);
$ext=$exts[1];
    //$ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension

    //Not really uniqe - but for all practical reasons, it is
    //$uniqer = substr(md5(uniqid(rand(),1)),0,5);
    //$file_name = $uniqer . '_' . $file_title;//Get Unique Name
     $file_name = $file_title;//Get same Name

    $all_types = explode(",",strtolower($types));
    if($types) {
        if(in_array($ext,$all_types));
        else {
            $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
            return array('',$result);
        }
    }

    //Where the file must be uploaded to
    if($folder) $folder .= '/';//Add a '/' at the end of the folder
    $uploadfile = $folder . $file_name;

    $result = '';
    //Move the file from the stored location to the new location
    if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
        $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.
        if(!file_exists($folder)) {
            $result .= " : Folder don't exist.";
        } elseif(!is_writable($folder)) {
            $result .= " : Folder not writable.";
        } elseif(!is_writable($uploadfile)) {
            $result .= " : File not writable.";
        }
        $file_name = '';
        
    } else {
        if(!$_FILES[$file_id]['size']) { //Check if the file is made
            @unlink($uploadfile);//Delete the Empty file
            $file_name = '';
            $result = "Empty file found - please use a valid file."; //Show the error message
        } else {
            chmod($uploadfile,0777);//Make it universally writable.
        }
    }

    return array($file_name,$result);
}

In upload.php

$folder="upload/";
$uploaddir = $folder;

if($_FILES['file']['name']) {
  list($file,$error) = imgUpload('file',$uploaddir,'wav');
   if($error){
     print $error;
   }else{
     echo "sucsess";
     print $error;
   } 


}

In index.html

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" /><input type="submit" value="Upload" name="action" />
</form>
by (630 points)
selected by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
2 like 0 dislike
1 answer
asked Aug 7, 2013 in Education & Reference by Tisha | 555 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
asked Jan 4, 2018 in Education & Reference by Sam (1.4k points) | 448 views
0 like 0 dislike
1 answer
asked Aug 1, 2017 in Education & Reference by Krish (820 points) | 192 views
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Jul 7, 2014 in Education & Reference by Bhoomika (50 points) | 14.1k views

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,227 questions
1,373 answers
559 comments
2,099 users