How to upload files with php the right way?

php 7 file upload
file upload in php example code demo
php file upload script
file upload in php mysql database
move uploaded file in php
php multiple file upload
php $_files empty
php file upload and download tutorial

So I have been working on this school project for about a week now and then came across an instance where I had to implement a user registration form where it is supposed to submit the user info as well as the user profile image whose path is to be stored in the DB as well. The drawback is I can't seem to get the upload script working. I have watched tens of tutorials and read like 15 solutions to similar problems from this very platform but still to no avail. I have also made sure that the php.ini file has the file_upload setting turned on.

HERE IS MY HTML FORM

<form style="padding: 1em;" class="ui form small inverted raised segment" action="signup.php" method="POST" enctype="multipart/form-data">
                   <h4 class="ui dividing header">Background Information</h4>
                    <div class="field">
                        <div class="three fields">
                            <div class="field">
                               <input type="text" name="fname" placeholder="Firstname" required> 
                            </div>
                            <div class="field">
                                <input type="text" name="mname" placeholder="Other name">
                            </div>
                            <div class="field">
                                <input type="text" name="lname" placeholder="Lastname" required>
                            </div>
                        </div>
                    </div>
                    <div class="field">
                        <div class="two fields">
                            <div class="field">
                              <select name="gender">
                                  <option value="">Select Gender</option>
                                  <option value="Male">Male</option>
                                  <option value="Female">Female</option>
                                  <option value="Other">Other</option>
                              </select>                              
                            </div>
                            <div class="field">
                                <input type="date" name="dob" required>
                            </div>
                        </div>
                    </div>
                    <div class="ui dividing header">Contact Information</div>
                    <div class="field">
                        <div class="two fields">
                          <div class="field">
                            <input type="tel" name="phone" placeholder="Mobile phone number" required>
                        </div>  
                        <div class="field">
                            <input type="email" name="email" placeholder="Email address" required/>
                        </div>
                        </div>
                    </div>
                    <div class="field">
                        <div class="three fields">
                            <div class="field">
                              <input type="text" name="area" placeholder="Area/Village" required>  
                            </div>
                            <div class="field">
                                <input type="text" name="trad_auth" placeholder="T/A or STA">
                            </div>
                            <div class="field">
                              <select id="district" name="kasungu">
                                  <option value="Kasungu">Kasungu</option>
                              </select>
                            </div>
                        </div>
                        <div class="ui dividing header">Work Details</div>
                        <div class="field">
                            <div class="two field">
                                <div class="field">
                                    <select id="department" name="department">
                                        <option value="">Select Department</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="ui dividing header">Acc Authentication Details</div>
                        <div class="field">
                          <input type="text" name="activation-code" placeholder="Enter admin authentication code (XXX-XXXX-XXXX)" required/>
                        </div>
                        <div class="field">
                          <div class="two fields">
                            <div class="field">
                              <input type="password" name="pass1" placeholder="Create Password" required/>
                            </div>
                            <div class="field">
                              <input type="password" name="pass2" placeholder="Confirm Password" required/>
                            </div>
                          </div>
                        </div>
                        <div class="ui dividing header">Upload Image</div>
                        <div class="field">
                          <div class="two fields">
                            <div class="field">
                               <input type="file" style="display: none;" id="pic" name="image"/>
                              <a type="button" id="upload" class="ui button fluid negative mini"><i class="icon camera"></i></a>
                            </div>
                          </div>

                        </div>
                         <div class="three fields">
                           <div class="field"></div>
                           <div class="field">
                              <button type="submit" name="submit" class="ui fluid mini positive button">
                                Signup &nbsp;<i class="icon user"></i>
                              </button>
                           </div>
                           <div class="field"></div>
                         </div>
                          <div>
                            <?php
                              if(!empty($errorMsg)){
                                echo $errorMsg;
                              }
                            ?>
                          </div>
                    </div>
                </form>

HERE IS MY PHP FUNCTION WHICH RECEIVES THE FILE['image'] A PARAMETER.

    <?php
function uploadFile($file){
  $newName;
  if(isset($file)){    
    $file_name = $file['name'];
    $file_tmp_loc = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    $ext  = strtolower(end(explode('.', $file_name)));
    $allowed = array('jpg','jpeg','png','gif');

    if(in_array($allowed, $allowed)){
      if($file_error == 0){
        if($file_size > 3000000){
          $file_name_new = uniqid('',true).".".$ext;
          $destination = '../images/users/';
          $destination = $destination.$file_name_new;

          if(move_uploaded_file($file_tmp_loc, $destination)){
            $newName = $destination;
          }else{
            echo "<h1>Failed to move uploaded file</h1>";
          }
        }
      }else{
        echo "file upload failed. ".$file['error'];
      }
    }else{
      echo "Type not allowed!";    }
  }
}
   return $newName;  
?>

In my other PHP script, I am calling the function like this:

<?php
$fileName = uploadFile($_FILE['image']);
?>

an then stores the returned new name as its reference in the MySQL DB

You have to use the $_FILES when you receiving file and in first array mean $_FILES['the file name which you have mentioned in Input files by Name like name='abc' '] and second array is all about file name, size, error,location. you can also see this by using print_r($_FILES['image']);

<input type="file" style="display: none;" id="pic" name="image"/>

        $file_name = $_FILES['image']['name'];
        $file_tmp_loc = $_FILES['image']['tmp_name'];
        $file_size = $_FILES['image']['size'];
        $file_error = $_FILES['image']['error'];

So correct it!

Cheer you!

PHP - File Uploading, is being submitted and generates a message if true. Without the requirements above, the file upload will not work. Other things to notice: The type="file" attribute of the <input> tag shows the input field as a file-select control, with a "Browse" button next to the input control. The form above sends data to a file called "upload.php", which we will create next.

This is running code.

$image_file = $_FILES['school_logo']['name'];
$tmp=$_FILES["school_logo"]["tmp_name"];
$ext = pathinfo($image_file ,PATHINFO_EXTENSION);
$folder="images/school_logo/";
$logos='logo'.'-'.uniqid(date('dmy')).rand().'.'.$ext;
$test=move_uploaded_file($tmp,$folder.$logos);

I hope this code is helpful for you.

PHP $_FILES, $_ENV, $_COOKIE, $_SESSION, PHP - File Uploading - A PHP script can be used with a HTML form to allow The full path to the selected file appears in the text filed then the user clicks the  7. Create the browse button and file box. This code will create the browse button and the box that contains the file to be uploaded. Type the following code to create this part of the upload form: <input type="file" name="uploaded_file"> <input type="submit" value="Upload">.

As I have seen your input field name for the file is image. So you should pass a file object to called function $_FILES.

<?php
$fileName = uploadFile($_FILES);
?>

See your input file field

<input type="file" style="display: none;" id="pic" name="image"/>

and change code in where you are trying to upload the image as:

$file_name = $file['image']['name'];

Hope it will help you.

The most recent version of PHP: The Right Way is also available in PDF, Uploaded and downloaded files, session values, cookie data, and  Alternatively, put them in a file called settings.php and include it in the script that performs the actual uploading. Now upload your images and other files to Cloudinary’s cloud platform: To upload a local file, e.g.,my_image.jpg, type:

In this article, I'll explain the basics of file upload in PHP. 60 (60 seconds) is a good value for most apps. you use the multipart/form-data value for the enctype attribute, it allows you to upload files using the POST method. In this tutorial we will learn how to upload files on remote server using a Simple HTML form and PHP. You can upload any kind of file like images, videos, ZIP files, Microsoft Office documents, PDFs, as well as executables files and a wide range of other file types. Step 1: Creating an HTML form to upload the file. The following example will create a simple HTML form that can be used to upload files.

In this tutorial you will learn how to upload files like images, word and PDF <​form action="upload-manager.php" method="post" enctype="multipart/form-data"​> like file type and file size to ensure that users upload the correct file type and​  Additional Considerations & Tips for uploading files with PHP. PHP maximum upload size: php.ini settings will cause PHP to throw warings/errors when the limits are exceeded. These limit settings that should be adjusted are upload_max_filesize and post_max_size

<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> If you still get strange results when downloading (especially in IE), make sure that the PHP output compression is disabled, as well as any server compression (sometimes the server inadvertently applies compression on the output produced by the PHP script). 4. Handling large file sizes. readfile() is a simple way to

Comments
  • And your specific question is what? What is the issue with that code? What exactly is not working as expected? What actually happens if you use it? Do you get an error? Which? What does your http servers error log file contain?
  • Stop trying to solve all your problems at the same time. Separate out the different bits of functionality and get them working in isolation