How to upload files to the server?

Asked

Viewed 69 times

-1

I have the following html and php code running on my server, however, whenever I try to upload a file, I get the message "There was an error uploading the file, Please Try Again!" and I can’t understand why...

<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>

<?PHP
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "uploads/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);

    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>

My server is Apache/2.4.25 (Debian).

I wonder if you can help me?

  • Apparently your problem is in setting the $path variable. Is this uploads directory on the same level as the upload.php file? Provisionally places error_reporting( E_ALL); at the beginning of the upload.php file to have exact notion of the error generated by move_uploaded_file.

  • @Benilson yes uploads directory is at the same level as upload.php. I put error_reporting( E_ALL); and I was returned the number 22527...

  • @Ivanferrer tried with this example and returned the message "Error sending"...

  • Your error, is when it tries to call move_uploaded_file, for some reason it is not allowed to move from the temporary folder to the upload directory, maybe the problem is in the upload folder that must be without write permission, try writing the folder: chmod(realpath(dirname(__FILE__) . "/upload/"), 0755) , or something like that...

  • Barter $path = "uploads/"; for $path = __DIR__ . "/uploads/";

Show 1 more comment

1 answer

2


TAKE A TEST, use this code. (obviously changes to its structure)

 <?php  

        $foto = $_FILES['uploaded_file']['tmp_name'];

   //FOTO
    $tamanho_permitido = "1024000"; //1 MB
    $pasta = 'uploads';

    if (!empty($foto)){
    $file = getimagesize($foto);

    //TESTA O TAMANHO DO ARQUIVO
    if($_FILES['image']['size'] > $tamanho_permitido){
        echo "Erro - Arquivo muito grande";
        exit();
    }

    //TESTA A EXTENSÃO DO ARQUIVO
    if(!preg_match('/^image\/(?:gif|jpg|jpeg|png)$/i', $file['mime'])){
        echo "Erro - extensão não permitida";
        exit();
    }

    //CAPTURA A EXTENSÃO DO ARQUIVO
    $extensao = str_ireplace("/", "", strchr($file['mime'], "/"));

    //MONTA O CAMINHO DO NOVO DESTINO
    $novoDestino = "{$pasta}/imagem_".uniqid('', true) . '.' . $extensao;  
    move_uploaded_file ($foto , $novoDestino );

} 

        include('conexaobd.php');

        $sqlinsert = "INSERT INTO pacientes (uploaded_file) VALUES ('$novoDestino')";

        mysqli_query($link, $sqlinsert) or die("Não foi possível armazenar no banco");

        $resultado = "Dados Cadastrados com Sucesso!!";

        ?>

Saves the image path in the database, and the image itself in the directory.

Browser other questions tagged

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