How do you upload a file to website? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this questionHey guys I want to make a file uploader on my website bu开发者_C百科t i don't know how.
if someone could get me off to a start with a basic code or point me to a resource i would be very thankful.
The PHP Manual has a whole section devoted to this.
The tl;dr of it is that there is a special type of <input>
element you can use in your HTML that tells the browser you expect a file and it takes care of the mess of locating the file, etc. You then POST this to your script in a normal HTML <form>
and it appears inside the $_FILES
superglobal in PHP. The file itself gets sent to a temporary file on the server, and the $_FILES
superglobal contains all sorts of information on the state of the transfer and the location and information of the files transmitted.
Have fun!
You can get a browse button which can upload a file.To do that put this in your html or PHP
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" name="submit"/>
In the PHP which 'll handle the upload
if(isset($_POST['submit']))
{ $dir="yourdirectory";
$tmp=$_FILES['file']['tmp_name'];
$targetname="name you want to give";
}
and use the move_uploaded_file function as
move_uploaded_file($tmp,$dir."/".$targetname);
also you will need to check for failed uploads and other errors.
The PHP documentation has a whole section on handling file uploads, complete with examples. I'd suggest starting there.
I find the php tutorials are a bit hard to follow for beginners. Try the following: http://www.quackit.com/php/tutorial/php_upload_file.cfm
精彩评论