PHP File Upload

What is the best way to upload a file?
Alta_Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
dunxdConnect With a Mentor Commented:
1. Create a web form with enctype set to multipart/form-data
eg <form action="HANDLER.PHP" method="post" enctype="multipart/form-data"

2. Create a form input field like this
<input name="someName" type="file" id="storyPic">

3. Create a file HANDLER.PHP that the form will send the details to

4. In that file add the following line

move_uploaded_file($_FILES['someName']['tmp_name'],"path/to/destination/folder/filename");

That is your file uploaded and moved into the folder you specify, with the filename you specify.  You can use php to create a filename, eg you might like to use $_FILES['someName']['type'] to get the mime type of the uploaded file and generate the correct suffix for whatever filename you create.

Don't expect to be able to see your file on your filesystem until you have used move_uploaded_file to move it, at least if you are using PHP on windows with Apache.  Your PHP script can check to see if the file was uploaded using the is_uploaded_file function before trying to do any work with the file.  move_uploaded_file automatically checks to see if the file was uploaded though.

More details at
http://www.php.net/manual/en/function.move-uploaded-file.php
and
http://www.php.net/manual/en/function.is-uploaded-file.php

Hope that helps.  Be very careful what you do with uploaded files.  If someone runs an uploaded script on your server you could lose everything - and so could other people with sites on the same system!  Don't let people upload anything you or they might run!
0
 
Oliver_DornaufCommented:
ftp with username password. Everything else raise a security problem.
0
 
carchitectCommented:
are you asking about image upload or file uplaod
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Alta_Author Commented:
I want to upload a file with a web page.

0
 
Alta_Author Commented:
I fact I want a php script to upload a file from local disk to a server.
Name of the file on the server should be generated by the php...according to some rules. Name of the file on local system doesn't matter.
0
 
dunxdCommented:
Glad you liked my answer Alta.  Just to repeat, be really really careful what you do with uploaded files.  Pictures are ok, but any scripts can be a nightmare.  Don't under any circumstances let anyone dictate the filename that will be used!

My friends site got blatted because someone else on the server had allowed people to upload scripts - so someone uploaded a file with and eval of rm -rf / which deleted all files that anyone else had uploaded across the server!
0
All Courses

From novice to tech pro — start learning today.