Move an added file in the input file to a folder and pick up the path(url)

Asked

Viewed 437 times

1

I have an input type file where I would like to move the file to a folder and take the path of that file to save to the database, using php, if possible I wanted to create a month and year hierarchy when creating the upload folders(but it is optional).

I tried that, but this giving error:

move_uploaded_file($_FILES['arquivo']['tmp_name'], '/uploads/'.basename($_FILES['arquivo']['name']));
$link = __DIR__ .'/uploads/'.$_FILES['arquivo']['name'];

print_r($link);
  • In $_FILES['name_do_seu_input'] has all the file data, including its location on the disk. With this you can move it anywhere with (move_uploaded_file](http://php.net/manual/en/function.move-uploaded-file.php).

  • I changed the question with the code I tried, but it’s giving error: Notice: Undefined index: file in C: wamp64 www project Intermediate Publishers.php on line 7

  • How’s the form you’re using to upload the file? And if you give a print_r($_FILES), comes what?

  • vlw, but I’ve already solved, I put the answer in the question.

  • Victor, put the answer in the answer :) I mean, here we use the question area only for the same question, and the area below for the answers. You can post your answer downstairs, and even mark it as you like. It’s okay to answer your own question.

1 answer

0


If I can understand well what you want, just check if the dir exists, done you create or do not create a directory, and will have at the end the complete path.

$diretorio = date('m-Y') . '/';
$arquivo   = $_FILES['arquivo']['name'];

if(!is_dir($diretorio)){
   mkdir($diretorio, 0700)
}

move_uploaded_file($_FILES['arquivo']['tmp_name'], $diretorio . basename($arquivo));

$link = __DIR__ .$diretorio.$arquivo

print_r($link);
  • I didn’t exactly use your code, but it helped a lot, I’ll edit and put the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.