I've try this code to upload an image with php , but not work and i don't know why...
this is my form
<form action="conn/inserimento.php" method="POST" enctype="multipart/form-data">
<input type="file" name="immagine" /><br>
<div id="container-button">
<input type = "submit" value="Pubblica" name ="submit" class="btn-send">
</div>
</form>
this is my script in php
$path="uploads/";
$target_file = $path . basename($_FILES["immagine"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["immagine"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $target_file . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
我看到该代码取自https://www.w3schools.com/php/php_file_upload.asp,如果您向下滚动到“ Complete Upload File PHP Script”,您会注意到move_uploaded_file(...)
正在移动您的从临时文件到目标文件。
Try to add:
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
you can also use this links for upload file.
visit https://www.w3schools.com/php/php_file_upload.asp
visit https://cloudinary.com/blog/file_upload_with_php
visit https://www.tutorialspoint.com/php/php_file_uploading.htm
hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.