Php...... File Upload

Source: Internet
Author: User
<span id="Label3"></p><p><p>Sometimes we need to upload files to the server, the more common is to register or modify personal information need to upload avatar. Uploading a file requires a form form, and setting the property enctype= "multipart/form-data" is the only way to upload the File.</p></p><p><p>Upload file Page:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">form</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Action</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "chuli.php"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Method</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "post"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">enctype</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "multipart/form-data"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">input</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "file"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "file"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">input</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "submit"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">value</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "upload"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">form</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p><span style="color: #993300;">The meaning of enctype= "multipart/form-data" in the form is to set the MIME encoding of the FORM. By default, This encoding format is application/x-www-form-urlencoded and cannot be used for file uploads;</span><br><span style="color: #993300;">Only use the Multipart/form-data to complete the transfer of file data, the following Operation.</span></p></p><p><p><span style="color: #993300;"></span></p></p><p><p><span style="color: #993300;">The file is received according to the value of the name property, but not by the post method, but by Files.</span></p></p><p><p><span style="color: #993300;">Output: Var_dump ($_files["file"]);</span></p></p><pre class="xdebug-var-dump"><pre class="xdebug-var-dump"><strong>Array</strong> <em>(size=5)</em> <span style="color: #888a85;">=</span> <span style="color: #993300;"><small></small></span> <span style="color: #cc0000;">' 4.png '</span><em>(length=5) <span style="color: #993300;">//file name</span> </em></pre></pre><pre class="xdebug-var-dump"><pre class="xdebug-var-dump"> <span style="color: #888a85;">= =</span><span style="color: #993300;"><small></small></span><span style="color: #cc0000;">' image/png '</span><em><span style="color: #993300;">//file type</span><br></em></pre></pre><pre class="xdebug-var-dump"><pre class="xdebug-var-dump"> <span style="color: #888a85;">=</span><span style="color: #993300;"><small></small></span><span style="color: #cc0000;">' D:\wamp\tmp\php89A0.tmp '</span><em><span style="color: #993300;">//file temporary storage path, If the file is not saved, it will be deleted when the page is closed</span></em></pre></pre><pre class="xdebug-var-dump"><pre class="xdebug-var-dump"><em id="__mceDel"> <span style="color: #888a85;">= =</span></em><em id="__mceDel" style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5;"><span style="color: #993300;"><small></small></span></em><em id="__mceDel" style="line-height: 1.5;"><span style="color: #4e9a06;">0 //error message, 0 for false</span></em></pre></pre><pre class="xdebug-var-dump"><pre class="xdebug-var-dump"><em id="__mceDel"> <span style="color: #888a85;">=</span><span style="color: #993300;"><small></small></span><span style="color: #4e9a06;">4866//file byte size, If you want to display kb, divide</span> by</em></pre></pre><p><p>Upload processing page:</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">var_dump ($_files["file"]);//determine If file upload error</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #800080;"><span style="color: #800080;">$_files</span></span>["file"] ["error"<span style="color: #000000;"><span style="color: #000000;">]){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">limit the type and size of uploaded files//do not allow users to freely upload files</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>((<span style="color: #800080;"><span style="color: #800080;">$_files</span></span>["file"] ["type"]== "image/png" | |<span style="color: #800080;"><span style="color: #800080;">$_files</span></span>["file"] ["type"]== "image/jpeg") &&<span style="color: #800080;"><span style="color: #800080;">$_files</span></span>["file"] ["size"]<=1048576)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">size is less than 1M bytes</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">file Storage path: Two methods//1. add date time before file name</span></span> <span style="color: #800080;"><span style="color: #800080;">$filename</span></span>= "./img/".<span style="color: #008080;"><span style="color: #008080;">Date</span></span>("ymdhisms",<span style="color: #008080;"><span style="color: #008080;"></span> time</span>()).<span style="color: #800080;"><span style="color: #800080;">$_files</span></span>["file"] ["name"<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Create a folder without changing the Filename//$filename = "./img/kaifa/zhangsan/20160523/aa.jpg"; Processing file name Encoding format, Be sure to determine whether the file name is repeated before//file name if there is a garbled Chinese</span></span> <span style="color: #800080;"><span style="color: #800080;">$filename</span></span>=<span style="color: #008080;"><span style="color: #008080;">Iconv</span></span>("UTF-8", "GB2312",<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if file names are duplicated</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #008080;"><span style="color: #008080;">file_exists</span></span>(<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>"the file already exists!" "<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if it is not possible to process the encoding format here, because at this time to determine whether the file name duplicate file name and the uploaded file name is different, so that the upload will be uploaded successfully, but will replace the existing file//upload file, move the file//to move the temporary file to the created file storage path under the /c0></span></span> <span style="color: #008080;"><span style="color: #008080;">Move_uploaded_file</span></span>(<span style="color: #800080;"><span style="color: #800080;">$_files</span></span>["file"] ["tmp_name"],<span style="color: #800080;"><span style="color: #800080;">$filename</span></span><span style="color: #000000;"><span style="color: #000000;">); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>"the uploaded file type is not supported!" "<span style="color: #000000;"><span style="color: #000000;">; }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>"file Upload failed!" "<span style="color: #000000;"><span style="color: #000000;">; }</span></span></pre><p><p><span style="color: #993300;">Attention:</span></p></p><p><p><span style="color: #993300;">1. First to determine whether the upload error, and then limit the file type and size, create a file storage path, determine whether the file is duplicated, and finally save the upload File.</span></p></p><p><p><span style="color: #993300;">2. File storage path, The general can add a date time, can be accurate to milliseconds, if you want to be accurate, you can add a serial number, each upload will add 1, or random number can be, so the file name of the upload will change;</span></p></p><p><p><span style="color: #993300;">If you do not want to change the file name, you need to create a folder method, such as $filename = "./img/kaifa/zhangsan/20160523/aa.jpg";</span></p></p><p><p><span style="color: #993300;">3.php upload file size is limited, if the file is too large, the server is quickly full, so to limit the size</span></p></p><p><p><span style="color: #993300;">4. The Web page can not display the Client's picture, can only display the server side of the picture, so when doing a preview, the picture must have been stored on the server</span></p></p><p><p><span style="color: #993300;">5. When the user selects the file, it has been uploaded to the server, but only in the temporary storage path, Click the Upload button is to save the path to the Database.</span></p></p><p><p>Php...... File Upload</p></p></span>

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.