File upload in PHP giving error
I am building an web app using PHP.I am creating a file upload section in it but i am having problem.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
upload.php:
<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
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.";
}
}
?>
It is working fine for images. but when i try to check what happens if i upload mp4 file. it echo File already exists,Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, there was an error uploading your file. Kindly guide me where i am doing mistake so that this work fine for me without having errors. Thanks in advance.
It is working fine for images. but when i try to check what happens if i upload mp4 file. it echo Sorry, only JPG, JPEG, PNG & GIF files are allowed
This is limitation simply because your code limits it as such.
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
If the file is not of jpg
, png
, jpeg
, or gif
it won't allow the upload.
It echoes file already exists
This is simply because the file is already there (wherever the directory is), due to this check:
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
Sorry, there was an error uploading your file
I cannot see why this is. Your conditional if ($uploadOk == 0)
is set to 0 in all of your other conditionals, and so should be true and should echo "Sorry, your file was not uploaded".
Moving to comments to ask.
Because you have condition that disallow other extensions, so it works according to the code:
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
And apparently you already have such file in your target folder. Check it again.
HTML Code
<form method="POST" enctype="multipart/form-data">
<input id="tags" type="file" name="employer_image" />
<input type="submit" name="submit" value="submit">
</form
UPLOAD.PHP
if (isset( $_POST['submit'] )
{
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES["employer_image"]["name"])) {
$file_name=$_FILES["employer_image"]["name"];
$temp_name=$_FILES["employer_image"]["tmp_name"];
$imgtype=$_FILES["employer_image"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y").'-'.time().$ext;
$target_path = "folder-path".$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
/* write insert query for store in mysql*/
}else{
exit("Error While uploading image on the server");
}
}
}
first select file form your local after on click submit button it's redirect to upload.php file there are first function are white that find the extension for file and it's return your file extension you need to create a folder (where you can store with name) after extension code it's get the file name with dateand time and you can set target path of file where you save images and run your insert query after that. First you check the folder permission where you can move files
Thanks Vivek
链接地址: http://www.djcxy.com/p/69640.html上一篇: 在php中上传图片时出现错误
下一篇: PHP中的文件上传给出错误