0

I made a PHP file uploader and I need to upload a file in a specific page of a webpage. I use Dreamweaver CS3 to make my websites and PHP files.

But there is a catch, my Manager doesn't know how to upload his Policies in a specific webpage. All policies are in PDF format. For that he wants me to make a PHP File Uploader to upload his files in front hand, for Example: There is a new policy which needs to be uploaded in a specific he/she would need to click on the choose file button, upload and choose the file, rename the following file and upload it in the webpage with ease.

It is easy to upload the Policies using Dreamweaver but my manager doesn't know how to upload files using that kind of method.

I need to make a php uploader, So i can upload my policies and post it in a specific page. How do you create a code for that.

EDIT

I have a html file by the name of uploads.html:

<form action="index1.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>

and This is the php code (index1.php):

     <?php 
 if(isset($_POST['submit'])){
    $name       = $_FILES['file']['name'];  
    $temp_name  = $_FILES['file']['tmp_name'];  
    if(isset($name)){
        if(!empty($name)){      
            $location = '../uploads/';      
            if(move_uploaded_file($temp_name, $location.$name)){
                echo 'uploaded';
            }
        }       
    }  else {
        echo 'please uploaded';
    }
}
?>

EDIT

The code for this uploader is for upload file in a specific folder, I need to upload my policies in a specific Webpage or any website. How can I create a code for this? I am sorry but I am new to PHP

I dont have enough reputation to post images so I will just insert the error statements here.

There are the following errors in my php file:

Warning: move_uploaded_file(../uploads/apache_pb2.gif): failed to open stream: No such file or directory in C:\xampp\htdocs\index1.php on line 8

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpB959.tmp' to '../uploads/apache_pb2.gif' in C:\xampp\htdocs\index1.php on line 8

4 Answers 4

1

You are using a wrong function to perform upload operation.

Here you can find an answer to your problem:

http://php.net/manual/en/function.move-uploaded-file.php

There is an example of uploading a file. You can find more info in the users comments

Sign up to request clarification or add additional context in comments.

4 Comments

And also look at the permissions.
Yes of course. And also look if file is with correct mime type if needed.
How can you change your permissions? I Use Xampp in order to view my php files.
You can use chmod php function. If you are a Windows user, maybe you don't need to change them (this way of thinking is incorrect). Here I have found a good explaining of the permissions in windows enviroment. Check it out if you like stackoverflow.com/questions/9349469/…
0

Replace below line

    copy( $_FILES['file']['name'], "/var/www/html" ) or 

with

    move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html"  ) or

3 Comments

This is the error shows up: Fatal error: Call to undefined function move_upload_file() in C:\xampp\htdocs\php\file_uploader.php on line 4
I have updated the code ... please try updated code. By mistake I written wrong function name
More errors pop up: Warning: move_uploaded_file(/var/www/html): failed to open stream: No such file or directory in C:\xampp\htdocs\php\file_uploader.php on line 4 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php26F7.tmp' to '/var/www/html' in C:\xampp\htdocs\php\file_uploader.php on line 4 Could not copy file!
0

$_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server

so You have to change Your code

move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html/".$_FILES['file']['name'] )

6 Comments

So it should be: <?php if( $_FILES['file']['name'] != "" ) { copy( $_FILES['file']['tmp_name'], "/var/www/html/".$_FILES['file']['name'] ) or die( "Could not copy file!"); } ?????
Yes, You could try this, or you could try code from other answers with move_uploaded_file
There seems to be an error: Warning: copy(/var/www/html/Jellyfish.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\php\file_uploader.php on line 1 Could not copy file!
My bad, You needed to move not copy file, so in this case You could use rename function instead of copy, but for uploaded files it is safer to use move_uploaded_file. I updated my answer.
Warning: move_uploaded_file(/var/www/html/Desert.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\php\file_uploader.php on line 1 Well I am getting this error: This thing is persistant Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php2203.tmp' to '/var/www/html/Desert.jpg' in C:\xampp\htdocs\php\file_uploader.php on line 1 Could not copy file!
|
0
$target_dir = $_SERVER['DOCUMENT_ROOT'] . URL_SUB_FOLDER . "/file/";
// move to folder file
$file = basename($_FILES["file"]["name"]);
$target_file0 = $target_dir . $file;
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file0)){
    // do something.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.