K
K
Kevin Mitnick2016-06-26 16:43:43
PHP
Kevin Mitnick, 2016-06-26 16:43:43

Why files are not uploaded to the server (PHP)?

There is a clean OS, ubuntu 14.04. It costs apache + php5. The site has the usual form:

<form name="contact-from" id="contact-form" action="" method="post" enctype="multipart/form-data">
<input style="display: flex;align-items: center;height: auto;padding: 15px 10px;" name="photo" type="file" class="input-full main-input required,email" placeholder="Фотография">
<button class="button-primary pull-right" type="submit" style="border:none;">
</form>

The server has a handler:
$_FILES['photo']['name'] = strtolower($_FILES['photo']['name']);
$_FILES['photo']['tmp_name'] = strtolower($_FILES['photo']['tmp_name']);
$allow = array("jpg", "jpeg", "png", "gif");
$ext = substr($_FILES['photo']['name'], stripos($_FILES['photo']['name'], ".") + 1);
if(array_search($ext, $allow) !== false){
  $fname = md5(date("d.m.Y H:i:s") . rand(0, 100000) . $_FILES['photo']['name']);
    if(move_uploaded_file(realpath(dirname($_FILES['photo']['tmp_name'])), DIR . "/uploads/feedbacks/" . $fname . "." . $ext)){
      $photo = $fname . "." . $ext;
    }
}

The file arrives at the server, there are no errors in the file at the input, after "move_uploaded_file" there are no errors either. In this case, the file is not loaded into the correct directory. The rights to all temporary folders will already be set to 777, the rights to the folder into which files are uploaded, set to 777. In php.ini, uploads are allowed, the maximum file size does not exceed, the maximum size of the post request does not exceed.
Tried to upload like this:
$file = file_get_contents($_FILES['photo']['tmp_name']);
file_put_contents(DIR . "/uploads/feedbacks/" . $fname . "." . $ext, $file);

Gives an error, writes that there is no such file in the temporary directory. At the same time, very rarely (maybe 1 out of 50, or maybe less often) the file is loaded. I already broke my whole head, after changing php.ini I rebooted Apache. Groups of users tried to change everywhere at folders. What could be the problem ?
UPD: I wrote "sleep(60)" in the script at the end and looked at whether the file is created there at all or not, and so, it is created, but with rights 600, the user and group "www-data" are on the file.
UPD2: I installed it in Apache to run from the user who owns the directory with the temporary directory, I also indicated the group I needed. It did not give a result.
UPD3: I downloaded a temporary file that came to the server, uploaded it to a temporary directory, set the rights, as in the original, set the owner and group,

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kevin Mitnick, 2016-06-27
@volfing

In general, it turned out very stupid.

$_FILES["photo"]["tmp_name"] = strtolower($_FILES["photo"]["tmp_name"]);

T
ThunderCat, 2016-06-26
@ThunderCat

check what rights the apache has, and what group it has, maybe the apache does not have the necessary rights, although the folder has rights 777, xs, check it anyway.
Turn on the output of errors and warnings, if not enabled. Perhaps there are errors, but they are suppressed.
Try to just transfer the file, do not change its name, transfer at least what has come.
So far, smarter ideas have not come.
UPD: specify the folder with an absolute path from document_root.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question