basename( $_FILES[‘uploadedfile’][‘name’])【php】

1. first build an HTML form that lets users select a file to upload:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
  • method=”POST” – Informs the browser that we want to send information to the server using POST.
  • input type=”hidden” name=”MA… – Sets the maximum allowable file size, in bytes, that can be uploaded. safety mechanism
  • input name=”uploadedfile” – uploadedfile is how we will access the file in our PHP script.

2. When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server. It should be moved to different place, otherwise it would be destroyed.

The $_FILES array is where PHP stores all the information about files.

  • uploadedfile – uploadedfile is the reference assigned in HTML form. We will need this to tell the $_FILES array which file we want to play around with.
  • $_FILES[‘uploadedfile’][‘name’] – name contains the original path of the user uploaded file.
  • $_FILES[‘uploadedfile’][‘tmp_name’] – tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.

3. PHP save file: get the temporary file name, choose a permanent name, and choose a place to store the file.

// Where the file is going to be placed, NOTE:target_path should be exists when uploading file
$target_path = "uploads/";

/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
//Now all we have to do is call the move_uploaded_file function, The move_uploaded_file function needs to know:The path of the temporary file; The path where it is to be moved to

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
SATE!!
These few lines of code will allow anyone to upload data to your server. 
via http://www.tizag.com/phpT/fileupload.php
Advertisements

Posted in PHP

Tags: , ,

Permalink 1 Comment

One response to “basename( $_FILES[‘uploadedfile’][‘name’])【php】

  1. Hi there! Do you use Twitter? I’d like to follow you if that
    would be ok. I’m definitely enjoying your blog and look forward to
    new updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: