PHP file upload code not working

Hello
I need help on this code i used trying to upload images on my site, Here is the code

                            <?php
                    if(isset($_POST['add_post'])){
                            //$id = $_POST['id'];
                            $title = $_POST['title'];
                            $date = $_POST['date'];
                            $author = $_POST['author'];
                        
                            $post_image = $_FILES["image"]["name"];
                            $post_image_temp = $_FILES["image"]["tmp_name"];
                            $image = "";
                            $content = $_POST['content'];
                            $tags = $_POST['tags'];
                            $category = $_POST['category'];
                            $comments = 5;
                            $post_status = $_POST['status'];
                        
                        
                        move_uploaded_file($post_image_temp, "../uploads/$image");
                        
                        $query = "INSERT INTO posts(title,date,author,image,content,tags,category,comments,status)";
                        $query .= "VALUES('$title','$date','$author','$post_image','$content','$tags','$category','$comments ',$post_status')";
                        
                        $add_post = mysqli_query($connection, $query);
                         confirm($add_post);
                                }
                            ?>

Here is the screenshot for the error am new to php thou

Are you sure that’s the code that is producing those error messages? I can’t see how it can be. The first error refers to using a variable called post_image on line 3, but your line 3 doesn’t do that. It also shows some messages (the lines that start with “Sorry,”) that the code you posted does not output.

Thus the code and its confusing me the "Sorry message is coming in cause i wanted a user to be notified when the uploading files with unsupported formats or can provide an alternative i can use

I get why you put the messages, so users know what they did wrong, but that isn’t the code that is producing them, or the error messages. Without seeing the actual code it’s hard to help.

Is the code posted all the contents of upload.php?
Is the form submitting to this version - is there another version on the server somewhere?

first of all!
you need to handle errors like error_image = ‘file already exists’;
.lol your file is exists, change image name and try again.

second and importand you need to give a uniq name to your image so you can upload same images with different names.

you can use this one

  $FileName  = $_FILES['store_image']['name'];
    $filetmpname = $_FILES['store_image']['tmp_name'];
    $filesize = $_FILES['store_image']['size'];
    $filetype = $_FILES['store_image']['type'];

    $fileext = explode('.', $FileName);
    $fileactualext = strtolower(end($fileext));
	
    $allowed = array('jpg', 'jpeg', 'png', 'gif');
	
	if (in_array($fileactualext, $allowed)){
        if(isset($_FILES["store_image"]) && $_FILES["store_image"]["error"] == 0){
			if ($filesize < 1000000){
				$filenamenew = uniqid ('', true).".". $fileactualext;
				$filedestination = '../images/stores/'.$filenamenew;
				move_uploaded_file ($filetmpname, $filedestination);
			}else {
		        $store_image_err = "your file too big";
	        }
			
		} else {
		        $store_image_err = "error uploading image";
	    }
		
	} else {
		$store_image_err = "you cant upload files of this type";
	}
	// Validate store_image
    if(empty($_POST["store_image"])){
        $store_image_err = "Please enter store_image";
    }else{
        $store_image = test_input($_POST["store_image"]);
    }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.