I create a field for uploading files, here is a page for downloading images:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Π’Π΅Π³ INPUT, Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ accept</title> </head> <body> <form action="handler.php"> <p><strong>Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ JPEG, PNG ΠΈΠ»ΠΈ GIF</strong></p> <p><input type="file" name="img" accept="image/jpeg,image/png,image/gif"> <input type="submit" value="ΠžΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ"></p> </form> </body> </html> 

Here is handler.php:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Π’Π΅Π³ INPUT, Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ accept</title> </head> <body> <? if (empty($_FILES['img']['tmp_name'])) die('НСдопустимый Ρ‚ΠΈΠΏ Ρ„Π°ΠΉΠ»Π°!'); $tmp = $_FILES['img']['tmp_name']; $fname = $_FILES['img']['name']; if (!move_uploaded_file($tmp, 'wp-content/uploads/'.$fname)) die('НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅ΡΡ‚ΠΈΡ‚ΡŒ Ρ„Π°ΠΉΠ» ΠΈΠ· Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ΠΏΠ°ΠΏΠΊΠΈ'); echo '<img src="/wp-content/uploads/'.$fname.'" />'; // ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½Π° ?> </body> </html> 

When downloading any files (I tried to download pictures and archives), he writes that the file type is invalid, although I allowed uploading pictures:

 <input type="file" name="img" accept="image/jpeg,image/png,image/gif"> 

What is the problem then?

  • Check php.ini do you have file upload option enabled? Namely upload_tmp_dir = - new_russian_man
  • New_russian_man, must be enabled, because Without restrictions on the type of file download, everything works fine. - nick777

2 answers 2

The form is not specified: method="post" enctype="multipart/form-data" . Naturally, get does not want to send the file. =)
And accept in input is for the convenience of the user: some browsers limit the file selection to accept.

  • ling, and then how to allow users to download only the specific type of files? - nick777
  • Check on the server $_FILES['img']['type'] . mimetypes can be unzipped from this class: phpclasses.org/browse/file/2743.html . As I understand it, you are interested in everything that begins with image. - ling
  • Just $_FILES['img']['type'] that the type from $_FILES['img']['type'] is transmitted from the client and may not correspond to the actual file type. If you want to know the actual file type, you need to get it directly from the downloaded file, for example, using [fileinfo] [1]. [1]: php.net/fileinfo - Ilya Pirogov
  • Well, you can find out the type of file: [code] function getExtension1 ($ filename) {return end (explode (".", $ Filename)); } [/ code] So I inserted this fragment in handler.php and that's it? Do not need anything else? - nick777
  • Well, also an option. $ext = getExtension1($_FILES['img']['name']); , and then - a bunch of checks and, if the file extension is not, the output from the script. - ling

Corrected the code, take into account your wishes.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" /> <title>Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ</title> </head> <body> <strong><label style="color:#36C;">ВрСбования ΠΊ Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌΡ‹ΠΌ изобраТСниям</label></strong><br /> 1. ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΎΠ±ΡŠΡ‘ΠΌ Ρ„Π°ΠΉΠ»Π° - 2,9 ΠΌΠ‘Π°ΠΉΡ‚.<br /> 2. Π€ΠΎΡ€ΠΌΠ°Ρ‚ изобраТСния - JPG.<br /> 3. Π Π°Π·Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅(Ρ€Π°Π·ΠΌΠ΅Ρ€) ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Π»ΡŽΠ±Ρ‹ΠΌ,<br /> ΠΎΠ΄Π½Π°ΠΊΠΎ слишком большоС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ испортит<br /> вСсь Π²ΠΈΠ΄. <form enctype="multipart/form-data" action="?l=down" method="post" /> <input type="hidden" name="MAX_FILE_SIZE" value="3000000" /> НаТмитС "ΠžΠ±Π·ΠΎΡ€" ΠΈΠ»ΠΈ Π½Π° ΠΏΠΎΠ»Π΅ Π½ΠΈΠΆΠ΅ : <br /> <input name="userfile" type="file" /> <input type="submit" value="Π—Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ" /> </form> <?php if ($_GET['l'] == 'down' ){ $fn = $_FILES['name']; $ft = $_FILES['userfile']; $orn = $_FILES['userfile']['name']; $ext = substr($_FILES['userfile']['name'], -3); $allowTypes = array ('jpg', 'jpeg', 'png', 'gif', 'txt'); if (false !== array_search($ext, $allowTypes)) { $uploaddir = './wp-content/uploads/'; $uploadfile = $uploaddir.$_FILES['userfile']['name']; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo 'Π€Π°ΠΉΠ» Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½, ссылка Π½ΠΈΠΆΠ΅,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?">Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ Π΅Ρ‰Ρ‘</a><br />'.'<input size="40" type="text" value="'.substr($uploadfile, 1).'" /><br />'; echo '<input type="button" onclick="document.getElementById(\'dimg1\').innerHTML=\'<img src=.'.substr($uploadfile, 1).' />\'" value="ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅" /><br />'; echo '<div id="dimg1"></div>'; } else { echo 'Π€Π°ΠΉΠ» Π½Π΅ Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½'; } } else { echo 'Π’ΠΈΠΏ Ρ„Π°ΠΉΠ»Π° Π½Π΅ являСтся допустимым для Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ'; } } # ?> </body> </html> 
  • Andrew, if you download, for example, a zip archive, it will successfully load, only the extension will change to .jpg I’m interested in this option so that when uploading images, everything happens fine, and if a person tried to download the archive, a message was displayed that the file type the file itself is not allowed and would not be uploaded to the server. - nick777
  • Logically, for college I did not take this into account, now I will see. What types of images should I allow to upload? - Andrey Arshinov
  • Andrew, jpg, jpeg, png, gif, well, and you can still txt let, and the creation of unique names is not necessary. - nick777
  • although I still need to edit it, I’m not checking the file type correctly, I’ll finish it a little later. - Andrey Arshinov
  • Andrew, well, thank you very much for your help! - nick777