Codice PHP:
<?php
if ($_FILES['photo']['size'] != 0){
$dir= "imageupload/";
echo '<script type="text/javascript">alert("ENTRATO");</script>';
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
echo '<script type="text/javascript">alert("ENTRATO2");</script>';
// Check if file was uploaded without errors
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $_FILES["photo"]["name"])){
echo $_FILES["photo"]["name"] . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "$dir" . $_FILES["photo"]["name"]);
echo '<script type="text/javascript">alert("OK");</script>';
}
} else{
echo '<script type="text/javascript">alert("ERRORE");</script>';
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
}
else{
echo '<script type="text/javascript">alert("VUOTO");</script>';
}
?>
Codice HTML:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="form" method="post" enctype="multipart/form-data" novalidate class="box">
<div class="box__input">
<svg class="box__icon" xmlns="http://www.w3.org/2000/svg" width="50" height="43" viewBox="0 0 50 43"><path d="M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0 .9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5 1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4 0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4 0s-.7 1.7 0 2.4l10 11.6z"/></svg>
<input type="file" name="photo" id="file" class="box__file" data-multiple-caption="{count} files selected" multiple />
<label for="file"><strong>Sciegli un immagine</strong><span class="box__dragndrop"> o trascinala qui</span>.</label>
<button type="submit" class="box__button">Upload</button>
</div>
<div class="box__uploading">Uploading…</div>
<div class="box__success">Done! <a href="<?php echo $_SERVER['PHP_SELF']; ?>" class="box__restart" role="button">Upload more?</a></div>
<div class="box__error">Error! <span></span>. <a href="<?php echo $_SERVER['PHP_SELF']; ?>" class="box__restart" role="button">Try again!</a></div>
</form>
<script type='text/javascript'>//<![CDATA[
document.getElementById("file").onchange = function() {
document.getElementById("form").submit();
}
Il problema è che non mi uploada l'immagine.