The $_POST
do not obtain the file, the correct is $_FILES
and the form must have enctype="multipart/form-data"
, before leaving doing anything I recommend you start studying by the documentation https://secure.php.net/manual/en/features.file-upload.post-method.php.
A basic example should look something like this:
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Selecione : <input name="arquivo" type="file">
<input type="submit" value="Enviar">
</form>
<?php
//Salva a foto com nome aleatório dentro da pasta
$pasta = 'pasta-upload';
$arq = $pasta . '/' . uniqid(rand(), true);
$image = false;
if (!chmod($pasta, 0755)) {
echo 'Erro ao mudar as permissões';
} else if (isset($_FILES['arquivo']['tmp_name'])) {
if ($_FILES['arquivo']['error'] !== UPLOAD_ERR_OK) {
echo 'Erro no upload';
} else {
$tmp = $_FILES['arquivo']['tmp_name'];
$image = getimagesize($tmp);
$ext = str_replace(array('image/', 'x-'), '', $image['mime']);
$arq .= '.' . $ext;
if (!$image) {
echo 'Formato invalido';
} else if (!move_uploaded_file($tmp, $arq)) {
echo 'Erro ao mover o arquivo';
} else {
echo '<img src="', $arq, '">';
}
}
}
?>
</body>
</html>
In the example use a folder called pasta-upload
, it must be in the same folder as index.php
The action=""
this empty, this causes the upload to be sent to the same page
First, add
enctype="multipart/form-data"
to the elementform
. Second, your file will be available at$_FILES["arquivo"]
, not in$_POST
. Third, try reading this material, I believe it will help you.– Woss
Show on index, okay, but which page is this upload? and which directory is?
– user60252
the image ta on the desktop
– Manuel Jose