Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error uploading image using php

Tags:

html

php

I am trying to upload image as shown here w3schools But it always shows the error

Undefined index: file 

This is the code

HTML

<form action="upload.php" method="post"  enctype="multipart/form-data">
    <!-- Upload image -->
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload Image" name="submit">
</form>

PHP

<?php
    if(!isset($_POST["submit"])){
        die('Error');
    }
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["file"]["tmp_name"]);
        if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
        } else {
        echo "File is not an image.";
        $uploadOk = 0;
        }
    }
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    if ($_FILES["file"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=   "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been     uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
?>
like image 651
Akshay Avatar asked Mar 31 '26 05:03

Akshay


2 Answers

If you are using WAMP or XAMPP they have file upload limit set.If the file size is greater than 2mb the upload will not work.Try uploading images of size less than 2mb and see if it works. To change file upload limit open your php.ini file and modify this value

upload_max_filesize=2M

Replace 2M with limit you wish to provide eg 6M or 8M

The following link explains about changing the file upload limit PHP change the maximum upload file size

like image 67
AAB Avatar answered Apr 02 '26 18:04

AAB


You can use upload.php like this, It will work.

<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("c:\wamp\www\upload/newupload/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
like image 31
pcs Avatar answered Apr 02 '26 18:04

pcs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!