Domanda

I am trying to upload a file via input type file and save the file to a folder and then save the file name in the database.

the code below adds the file path to the file name, which is not what I want because when I call it the output looks like this images/articles/../images/articles/

if($_FILES['image']['error'] == 0){
     $target_path = "../images/articles/";
     $target_path = $target_path . time() . rand(11, 99) . basename( $_FILES['image']['name']);
      move_uploaded_file($_FILES['image']['tmp_name'], $target_path);
      updateDB($_POST['id'], $_POST['title'], $_POST['date'], $_POST['preview'], $_POST['description'], $target_path);
}

I also tried this

move_uploaded_file($_FILES['image']['tmp_name'], $_FILES['image']['name'] . date(‘ymdhis’));

but this adds the date after the extension.

È stato utile?

Soluzione

if($_FILES['image']['error'] == 0){
     $target_path_folder = "../images/articles/";
$fileName =  time() . rand(11, 99) . basename( $_FILES['image']['name']);

     $target_path = $target_path_folder .$fileName;
      move_uploaded_file($_FILES['image']['tmp_name'], $target_path);
      updateDB($_POST['id'], $_POST['title'], $_POST['date'], $_POST['preview'],        $_POST['description'], $fileName);
}

Should help

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top