Link after uploading a file

1
    <?php
$output_dir = "uploads/";


if(isset($_FILES["myfile"]))
{
    //Filter the file types , if you want.
    if ($_FILES["myfile"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        //move the uploaded file to uploads folder;
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);

     echo "Uploaded File :".$_FILES["myfile"]["name"];
    }

}
?>

This is my upload code, I can not get it to give the link after finishing the upload, I tried the code below and I could not get the link ... I'm still learning php so I'd like to know what it is Wrong exactly ...

echo "<a src='$_SERVER["DOCUMENT_ROOT"];".$_FILES["myfile"]["name"].>" Link "<a />";
    
asked by anonymous 06.08.2015 / 02:11

2 answers

0
<?php
    $output_dir = "uploads/";


    if(isset($_FILES["myfile"]))
    {
        //Filter the file types , if you want.
        if ($_FILES["myfile"]["error"] > 0)
        {
          echo "Error: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            //move the uploaded file to uploads folder;
            move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
         $arquivo = $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES["myfile"]["name"];
         echo "Uploaded File :".$_FILES["myfile"]["name"];
         echo "<a href='$arquivo'>Arquivo</a>";
        }

    }
    ?>
    
06.08.2015 / 02:25
0

During the upload process the file will first receive a temporary path $_FILES["myfile"]["tmp_name"] and then $_FILES["myfile"]["name"] . The second is the final destination that can be accessed from the client side.

The second argument of move_uploaded_file() needs exactly this path / path, and you can use it. I suggest you also pass this on a JSON so you can style it on the JS side more easily.

$destino = $output_dir.$_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"], $destino);
echo "Uploaded File :".$destino;
// ou, em JSON: echo '{"path":"'.$destino.'", "message":"Uploaded File"}';
    
06.08.2015 / 10:51