Link after uploading a file

Asked

Viewed 68 times

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’m not able to make it give the link after finishing the upload , I tried the code below and could not get the link... I’m still learning php so I’d like to know exactly what’s wrong...

echo "<a src='$_SERVER["DOCUMENT_ROOT"];".$_FILES["myfile"]["name"].>" Link "<a />";

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>";
        }

    }
    ?>

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 customer side.

The second argument from move_uploaded_file() needs exactly that camii/path, and you can use it. I also suggest you pass it on a JSON so you can style 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"}';

Browser other questions tagged

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