Unable to upload file

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 123
Joined: Tue Mar 01, 2016 3:07 pm

Tue Jul 17, 2018 2:50 pm

Hello.

Could I just briefly enquire?

I've got some code which hopefully will upload a gif, jpg or png file to my website.
However I can't get it to upload, and the folder I'm using on the server remains empty.

I'm unable to figure out where I've gone wrong. The folder on the server has been chmod to 777 and the image file I'm uploading is less then 2mb.

Would anyone please take a quick look, and let me know where I've gone wrong, or could someone write this script from me?

In php I've got:-

Code: Select all

<?php
/*
* 	   Simple file Upload system with PHP.
* 	   Created By Tech Stream
* 	   Original Source at http://techstream.org/Web-Development/PHP/Single-File-Upload-With-PHP
*      This program is free software; you can redistribute it and/or modify
*      it under the terms of the GNU General Public License as published by
*      the Free Software Foundation; either version 2 of the License, or
*      (at your option) any later version.
*
*      This program is distributed in the hope that it will be useful,
*      but WITHOUT ANY WARRANTY; without even the implied warranty of
*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*      GNU General Public License for more details.
*
*/
        if(isset($_FILES['image'])){
            //$_SESSION['user_profile_image']
                $errors= array();
                $file_name = $_FILES['image']['name'];
                $file_size =$_FILES['image']['size'];
                $file_tmp =$_FILES['image']['tmp_name'];
                $file_type=$_FILES['image']['type'];
                $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

                $expensions= array("jpeg","jpg","png","gif");
                if(in_array($file_ext,$expensions)=== false){
                        $errors[]="extension not allowed, please choose a JPEG, GIF or PNG file.";
                }
                if($file_size > 2097152){
                $errors[]='File size must be excately 2 MB';
                }
                if(empty($errors)==true){
                        move_uploaded_file($file_tmp,"image/".$file_name);
                        echo "Success";
                }else{
                        print_r($errors);
                }
        }
?>

And in html I've got:-

Code: Select all

<form action="" method="POST" enctype="multipart/form-data">
  <input type="file" name="image" />
  <input type="submit"/>
 </form>

Thank You.

User avatar
WebguruInfosystems
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Dec 05, 2018 9:47 pm
Location: Y8, Block - EP, Sector V, Salt Lake Kolkata - 700091, INDIA
Contact:

Thu Dec 06, 2018 4:35 am

The code is absolutely right. Please make sure two things:

1. action url of form is right (<form action=””>). If html and php code on same file the it will definitely work, otherwise define action url. And also use print_r($_FILE); die(); just after if(isset($_FILES['image'])){ , so that you will sure that your php code is working.

2. If first point is working then there must be permission issue of image folder, so try to give permission 777 to you folder.

Post Reply