(044) 362 48 16 (098) 294 41 60
|
|
|
Basics Upload file According to the HTTP files you can upload two ways:
The most common method is POST, PUT method is almost not used. For browser fronta file should be placed on this page HTML code
<form enctype="multipart/form-data" method="post" action="upload_script.php"> <input type = "hidden" name = "; MAX_FILE_SIZE "value =" 1000 "> Select file: <input name="imya_polya" type="file"> <input type="submit" value="Poslat fayl"> </ form> </ div> Necessary to specify <form enctype="multipart/form-data" method="post"...> and very desirable <input type="hidden" name="MAX_FILE_SIZE" value="XXXX"> prior to input file la. When these conditions are met, you can write a script that will handle uploading files. <? php function doUploadolor = "# 007700"> ($ field_name, $ overwrite = false, $ uniquename= False) ( if (! is_array ($ field_name)) $ field_name nt color = "# 007700"> = Array ($ field_name); #-------- FILE UPLOAD ----------------- / / Location of the files $ storage0 "> =" / home / www / public_html / uploads / ";
/ / Check for future small glitches $ flag = (bool) & nbsp; ini_get ( "safe_mode"); if ($ flag | | (! strstr ( ont> $ _SERVER [ 'SERVER_SOFTWARE'], 'win'))) ( if (getmyuid >()!= Fileowner ($ storage)) ( die ( "Safe mode uncompatibililty. Check owner for '"# 007700 ">. $ Storage" ' "); ) ) / / Allowed to upload extensions (types) of files $ allowed < / font> = array ( 'jpg', 'gif', 'png', 00 "> 'pdf', 'doc', 'txt', 'rtf' );
foreach ($ field_name as $ field_i) (
/ / Maximum file size. In any case, it is & nbsp; can not be / / Greater than upload_max_filesize =?? M in php.ini (2Mb) / / And the post_max_size =?? M (8Mb) $ maxsize 61440 / / 60Kb
/ / Reads the name of an uploaded file $ filename <font color = "# 007700"> = $ _FILES [$ field_i] [ 'name'];
/ / Considered vaem size of an uploaded file $ filename = $ _FILES [$ field_i] [d0000 "> 'size'];
/ / Reads the file extension $ fileext = strtolower 007700"> (substr (strrchr ($ filename, "."), ont color = "# 0000bb"> 1));
/ / Prohibit unauthorized downloading, for example PHP script! if (!"# 0000bb"> in_array ($ fileext, $ allowed)) ( die ( "Invalid file type & quot;); )
/ / Disables uploading too large files if ($ filesize font>> $ maxsize) ( die ( "Too large a file"); )
& nbs p; / / reads the name of a file that is uploaded to a temporary folder / / Upload_tmp_dir = in the php.ini file $ tmpfnamecolor = "# 007700"> = $ _FILES [$ field_i] [ 'tmp_name'])
/ / Correctlyaem file name, removing illegal characters, spaces. $ filename = ereg_replace ( "[^ a-z0-9._] "," ", str_replace ( "", "_", str_replace ( "% 20", "_", strtolower ($ nameor = "# 007700 ">))));
if ($ filename = "") ( die ( "Invalid name file. Only English letters, numbers and'_'!"); )
/ / Full path to the injected file ont> $ filepath = $ storage; if ($ uniquename) ( 000bb "> $ filepath = $ filepath. Time ()." _ "; ) $ filepath = $ filepath. $ filename;
if (is_uploaded_file ($ tmpfname) (
/ / If $ overwrite! = True no checking whether the file already if (! $ overwrite) ( if (file_exists ($ filepath ont>)) ( die ( "File name <b>". $ filename. "< / b> already exists. Rename the file and delete it from the server "); ) & nbsp;)
move_uploaded_file ($ tmpfname, $ filepath) or die ( "Error downloading file:". $ filename); / / ECDoes the user Apache and FTP is different, for example, and nobody pupkin, / / It to have access to FTP (default exhibited 0600) / / Put the & nb sp; 0644 or 0666 if you want to overwrite via FTP @ Chmod ($ filepath 700 ">, 0644); ) ) ) #------------- END FILE UPLOAD ---------- ) > / / Call doUpload ( 'imya_polya');
/ / Upload multiple files battlesSTORAGE doUpload (Array ( 'imya_polya1', 'imya_polya2'));
/ / If you want to overwriteisyvat existing file doUpload ( 'imya_polya', true);
/ / If need to save an existing file / / Will create a new name, n: pic.jpg -> 989181984_pic.jpg doUpload ( 'imya_polya'0 ">, false, true); >
PHP version 3 was not the function move_uploaded_file ()therefore, before the fourth version of ispollzovalas function copy () which is still in the documentation and is very popular. Extremely undesirable to use the copy () files when injected as may be many bugs. copy () will not work when includingyuchennom open_basedir!! In safe_mode = On to use the copy () must be installed on the temporary folder of the same and that the OWNER is the script, ie apload it would be impossible to do if you have multiple users (as it should be in the case of virtual domains). Also copy ()checks the file does not exist, does not return Possible bugs
- file_uploads = Off in php.ini. Downloading files is prohibited
- No rights for $ storage = "/ home / www / public_html / uploads /". Chown Change the userwhich is running the script, or put in a folder chmod 0777
- Target directory is different than the owner of which is running PHP, when safe_mode = On. It is widely distributed case for hosting a ball, when the user Apache for example, www or nobody,while access to FTP, for example, to pupkin. Exit the two:
- Chisel support service for adjusting the same users on the FTP and Apache (for your virtual domain).
- Folder to download to create a script under Apache, and put it 'chmod 0777'. Then you will be able to raBot of the script bypassing safe_mode restrictions and edit files on FTP.
- No rights to upload_tmp_dir =; (setting in php.ini). Put chmod 0777 on this folder.
- Files can be spoiled if Apache is running under some modules, such as mod_charset(Also known as Russian Apache). Turn off it for certain files:
<Files Upload.php> <br> CharsetDisable On <br> </ Files>
- Do not upload large files. The reasons may be several, if not carried out anyOne of the conditions of the file does not begin:
- File size more than $ _POST [ 'MAX_FILE_SIZE']
- File size more than upload_max_filesize = 2M (php.ini)
- File size more than post_max_size = 8M (php.ini)
- Size greater LimitRequestBody (httpd.conf)
Running low on disk space or upload_tmp_dir = or on the target directory
- Execution time has exceeded the max_execution_time = (php.ini)
- Time Script Timeout exceeded 30 (httpd.conf)
- Execution time has exceeded timeout for CGI (Consol IIS)
- The user is sitting behind a proxy which prohibits the transfer of
|
|