Php file upload from a fixed directory

I want user must upload file from his/her C drive,they cant upload file from other drive.

My upload code is here.

<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="filename" />
<input type="submit" value="Upload" />
</form>

And

$file_name = $_FILES['fupload']['name'];

$file_type = $_FILES['fupload']['type'];

print "Path: ".$_FILES['fupload']['tmp_name']."<br />";

print "Name: $file_name <br />";

print "Size: ".$_FILES['fupload']['size']."<br />";

print "Type: $file_type <br />";

if(copy($_FILES['fupload']['tmp_name'], "$file_name"))
{
    print "Upload Success";
}
else
{
    print "Error occur";
}

Upvotes: 1

Views: 1560

Answers (4)

This is only possible IE.


<head>
    <script type="text/javascript">
        function CheckAndSubmit () {
            var uploadForm = document.getElementById ("uploadForm");
            var uploadFile = document.getElementById ("uploadFile");
            var fileinfo = document.getElementById ("fileloc");

            if (uploadFile.value.length == 0) {
                alert ("Please specify the path to the file to upload!");
                return;
            }
            //alert(document.uploadForm.uploadFile.value);

            fileinfo.value=uploadFile.value;


            uploadForm.submit ();
        }
    </script>
</head>
<body>
    <?php
        //if(isset($_POST['uploadFile']))
            print_r($_POST);
    ?>
    <form id="uploadForm" name="uploadForm" method="post" enctype="multipart/form-data" action="">
        Select a file: <input type="file" name="uploadFile" id="uploadFile" size="25" />
        <input type="hidden" id="fileloc" name="fileloc" value="" />
        <br /><br />
        <input type="button" value="Upload" onclick="CheckAndSubmit ()"/>
    </form>
</body>

If not shown full path>> 1. open internet Explorer internet option>security>custom level> 2. Then Enable include local directory file path when uploading....

Enjoy...

Upvotes: 0

Gerald Schneider
Gerald Schneider

Reputation: 17797

You can write/use a Java Applet that handles the upload on the client side. There you can check the actual file path. But that won't help you if a user just copies a file to C: before uploading it. I don't see any point in trying this.

Just with HTML/JavaScript/PHP it is not possible.

Upvotes: 0

Siamak Motlagh
Siamak Motlagh

Reputation: 5136

It's not possible to force user upload file from specific drive.

Check out this link from php.net: $_FILES

These are all you can know about the uploaded file:

 [name] => 400.png // file name
 [type] => image/png // file type
 [tmp_name] => /tmp/php5Wx0aJ // file temp name
 [error] => 0 // file uploading error
 [size] => 15726 // file size

For JavaScript you can see this question: Javascript: Listing File and Folder Structure

Upvotes: 1

Ariston Alcantara
Ariston Alcantara

Reputation: 232

You can do what you want if you can get the absolute path of the file. If you can get the absolute path of the file, you can do a simple equality test if the path is not "C:\".

But the built-in input type file will not give you any hint on this as it is for security purposes.

If you can make your own upload form then you can but using the input type file it is impossible.

Upvotes: 0

Related Questions