PHP File Upload Summary (garbled, failed to move, permission, show image)

Source: Internet
Author: User
Tags php file upload linux mint

LAMP environment:

Linux Mint 16 32 bits xfce

Apache 2.4.6 Ubuntu

Php 5.5.3

The default www is/var/www. I connected to/home/tony/www with a symbol

Then modify sudo chmod 777 www

Upload page code:

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
</Head>
<Body>

<Form action = "upload_file.php" method = "post"
Enctype = "multipart/form-data">
<Label for = "file"> Filename: </label>
<Input type = "file" name = "file" id = "file"/>
<Br/>
<Input type = "submit" name = "submit" value = "Submit"/>
</Form>

</Body>
</Html>

This Code requires the header to specify the character set. The following code is the same

<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
</Head>
<? Php
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Error:". $ _ FILES ["file"] ["error"]. "<br/> ";
}
Else
{
Echo "Upload:". $ _ FILES ["file"] ["name"]. "<br/> ";
Echo "Type:". $ _ FILES ["file"] ["type"]. "<br/> ";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb <br/> ";
Echo "Stored in:". $ _ FILES ["file"] ["tmp_name"]. "<br/> ";
}
If (is_uploaded_file ($ _ FILES ["file"] ["tmp_name"]) {
Echo "legal uploaded file <br> ";
} Else echo "illegai uploaded file <br> ";
$ Src_path = $ _ FILES ["file"] ["tmp_name"];
$ Des_path = '/home/tony/www/upload/'. "a.jpg ";
// $ Des_path = '/home/tony/www/upload/'. $ _ FILES ["file"] ["name"];
Echo $ src_path. "<br/> ";
Echo $ des_path. "<br/> ";
If (file_exists ($ src_path )){
Echo "file exists. <br/> ";
}
If (move_uploaded_file ($ src_path, $ des_path )){
Echo "Stored in:". "<br/> ";
} Else echo "<br> move failed .";
Printf ("?>

The code in this section is redundant and not beautiful. But some problems are explained.

1. Specify the character set. Otherwise, there may be problems in different browsers or systems. Do not use the AddDefaultCharset function in the configuration file of apache2. Change it to adddefacharcharset Off if it has been changed. Restart apache2.

2/The temporary file will disappear after php Execution, which is difficult to see with the naked eye. You can get a while (1) at the end );

3/why can't I use other functions to transfer uploaded files? Now that I have all the paths. This http upload mechanism ensures certain security. If you can verify the upload security, other functions can also be competent.

4/I always failed to move the file. After several investigations, it was because the new path had no permissions, and linux security had caused the fault. I tried to use chmod-R 777 www and found that the upload under it did not obtain the 777 permission... Is this a bug? Modify the upload permission again.

The absolute path cannot be used to insert an image. The relative path cannot be used until now. Why?

I should add some file type detection, file header detection, and operation prohibited restrictions to php for upload and processing. These security or function restrictions will be added when they are available.

PHP details: click here
PHP: click here

Recommended reading:

Practical Production Environment-LNMP architecture compilation and installation + SSL encryption implementation

LNMP full-featured compilation and installation for CentOS 6.3 notes

Install LNMP in CentOS 6.3 (PHP 5.4, MyySQL5.6)

Nginx startup failure occurs during LNMP deployment.

Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)

Detailed php hd scanning PDF + CD source code + full set of teaching videos

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.