The bottom line is, there are two fields on it, the first to select the file you want to upload to the server, the second to indicate the link to the file (its location on the server), I do two checks on the parameters received, and perform certain actions, the fact is then, the first check does not want to work ...

here is the code

<?php //определяю дерриторию скрипта $uploaddir = $_SERVER['SCRIPT_FILENAME']; $uploaddir =str_replace('index.php','',$uploaddir); ?> <form enctype='multipart/form-data' action='' method=post> <input type=file name="userfile" size=50><br> <input type="text" name="userfile2" size="50" value="<? echo $uploaddir.'news.txt'; ?>"><br> <input type=submit value="Поехали"> </form> <?php //Вывел форму с двома полями, первое для выбора фала с компа, второе чтобы указать иммя файла на сервере //проверяю первое значение, указан ли файл на с компа, если да, загружаю его на сервер, и вывожу его адрес, прекращаю выполнение скриптиа if (isset($_POST["userfile"])){ $temp=$_FILES['userfile']['name']; $uploadfile = $uploaddir . $temp; move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile); echo $uploadfile.'<br>'; exit; } //проверяю второе значение, указан ли файл на с сервере, если да, загружаю его на сервер, и вывожу его адрес, прекращаю выполнение скриптиа if (isset($_POST["userfile2"])){ $url = $_POST["userfile2"]; echo $url.'<br>'; $contents = file_get_contents($url); exit; } ?> 

The first if (isset ($ _ POST ["userfile"])) check doesn’t give any result, and all the time it outputs me a file that is already on the server ...

where I made a mistake ... thanks in advance!

  • Check for the existence in this folder of the file with the same name, and delete if exists, have not tried? - stck
  • each time I tried to choose a file with a unique name, the fact is that it doesn’t work to check the presence of a parameter in a post request ... that is, if a file is selected, it should display it and stop further script execution - arashvg
  • In the post request, two userfile and userfile2 parameters are passed, if there is the first parameter, perform one action. If it does not exist, check the presence of the second parameter, perform the second action, if there are no parameters, do nothing .... if (isset ($ _ POST ["userfile"]) )) {action exit; } if (isset ($ _ POST ["userfile2"])) {action exit; } - arashvg

1 answer 1

 if (isset($_POST["userfile"]))// Вот это уже неверно. Во-первых, не isset, а !empty надо так как задано поле всегда если оно присутствует в форме, а во-вторых не $_POST, а сразу файлы смотрите. $_FILES['userfile']['error']==0 && !empty($_FILES['userfile']['tmp_name']) 
  • thanks, it worked ... for a long time I would understand this myself !!! - arashvg
  • just +1 to karma to you. I didn’t write, for some reason I sometimes miss such questions, because many answers have already been written :)) - Artem