Hello! There is such a PHP code. He should, when trying to upload a picture to the server, actually upload the picture to the server. The problem is that physically the picture does not appear there. The path is written in the templates, all is well. But the picture itself is not. What could be the problem? If it is not in the code, then where to look for it?

$uploaddir = $global_vars['mainpatch'].'/templates/userpic/'; $uploadfile = $uploaddir . basename($_FILES['photo_of_teacher1']['name']); if (move_uploaded_file($_FILES['photo_of_teacher1']['tmp_name'], $uploadfile)) { $image_success = basename($_FILES['photo_of_teacher1']['name']); } else { $image_suck = "Ошибка загрузки изображения"; } print_r($_FILES); print "</pre>"; 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

1) It is necessary to try on the simplest example, create an index.html file with the form:

  <form action="server.php" method="post" enctype="multipart/form-data"> <input type="file" name="filename" /> <input type="submit" /> </form> 

Note the following:

  • form submission method must be POST
  • The form must have an enctype attribute and the value must have a multipart / form-data
  • an input with file type must also have a name attribute, the value of which will be used on the server.

After that, create 1 more server.php file in the same folder, and paste the following code into it:

 <?php if(copy($_FILES["filename"]["tmp_name"],"c:/xampp/htdocs/files/".$_FILES["filename"]["name"])) { echo("файл успешно загружен"); } else { echo("Ошибка загрузки файла"); } ?> 

Replace in the copy function, the path to your own, and note that in $ _FILES we pass the name that we specified in index.html in the input in the name attribute in with the file sent. It should also be noted that in the case of downloading files with the same name at different times, only the last file will remain in the download directory, the previous one will be overwritten. Therefore, as an option, you can change the file name when loading, replacing:

 copy($_FILES["filename"]["tmp_name"],"c:/xampp/htdocs/files/".$_FILES["filename"]["name"]) 

on:

 copy($_FILES["filename"]["tmp_name"],"c:/xampp/htdocs/files/".uniqid().$_FILES["filename"]["name"]) 

Try downloading a file weighing up to 1 MB.

The example is fully working, if it works on your server, then the environment is OK and look for an error in the code, if it does not work then you need to look for an error in the environment.

2) The second thing to check is the settings in php.ini:

max_file_uploads - maximum upload file size

post_max_size - the maximum size of data that can be transmitted via a post request

If the weight of the picture exceeds these two parameters, then you need to increase them.

  • No, I tried different - from small weight to large. Does not help (( - Artur Mark
  • Added point 2, in it an example, copy it yourself, I have so successfully loads pictures. And by the way small is how much by weight? - heff
  • From 4 kilobytes to 5 megabytes ... Inserted yours - the story is exactly the same! The page reloads, by clicking on the image indicates its correct location, but the picture itself is not there. But the correct path and her name is. Such a paradox)) Apparently, the problem is not in this code - Artur Mark
  • added point 3, check it out - heff
  • After installing enctype, the name of the image disappeared altogether (i.e. the path is in the address bar, but the file name itself is not). Otherwise, everything is the same - the form is standard, the POST method is Artur Mark

The function for saving images to the folder, the path to which is specified in $ path;

  public function saveImg() { mkdir( $path, 0777 ); $i = 0; $is_saved = false; $errors = $_FILES['photos']['error']; foreach ( $errors as $key => $error ) { if ( $error === UPLOAD_ERR_OK ) { $tmp_name = $_FILES['photos']['tmp_name'][$key]; move_uploaded_file( $tmp_name, $path . $i++ . '.jpg' ); if ( !$is_saved ) $is_saved = true; } else { if ( $is_saved ) $is_saved = false; break; } } return $is_saved; } 
  • I'll wait, thanks) With try-catch in any way The error is not here, because the code runs on. Now I’ll try to figure out other pieces of code ... - Artur Mark
  • Well, what happened? - Antosha Shmonoff