How to set a specific path while file uploading using php

2.1k Views Asked by At

I am new in PHP. i have a code for file uploading and save it in mysql. I want to set a specific path while uploading a file. For example i set the path

C:\xampp\htdocs

and when ever i click on upload button my this directory will be open.

Here is my Code

  <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test123"; // Database name 
   $con=mysql_connect("localhost","root","");
    if(! $con)
    {
    die('Connection Failed'.mysql_error());
    }

    mysql_select_db("test123",$con);

    if(isset($_FILES['files'])){
        $errors= array();
        foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
            $file_name = $key.$_FILES['files']['name'][$key];
            $file_size =$_FILES['files']['size'][$key];
            $file_tmp =$_FILES['files']['tmp_name'][$key];
            $file_type=$_FILES['files']['type'][$key];  
            if($file_size > 2097152){
                $errors[]='File size must be less than 2 MB';
            }       
            $query="INSERT into upload_data (`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$file_name','$file_size','$file_type'); ";
            $desired_dir="user_data";
            if(empty($errors)==true){
                if(is_dir($desired_dir)==false){
                    mkdir("$desired_dir", 0700);        // Create directory if it does not exist
                }
                if(is_dir("$desired_dir/".$file_name)==false){
                    move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                }else{                                  // rename the file if another one exist
                    $new_dir="$desired_dir/".$file_name.time();
                     rename($file_tmp,$new_dir) ;               
                }
             mysql_query($query);           
            }else{
                    print_r($errors);
            }
        }
        if(empty($error)){
            echo "Success";
        }
    }
    ?>


    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple/>
        <input type="submit"/>
    </form>
1

There are 1 best solutions below

9
On

You can do as below :

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="test"; // Database name 
$con=mysql_connect("localhost","root","root");
if(! $con)
{
die('Connection Failed'.mysql_error());
}

mysql_select_db("test",$con);

if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }       
        $query="INSERT into upload_data (`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$file_name','$file_size','$file_type'); ";
        $desired_dir="/home/test/www/uploads/user_datae";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0755);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
        mysql_query($query);           
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}
?>


<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple/>
    <input type="submit"/>
</form>